From 12b6791e8b2dcc8a341be9423c8643f4be3e5e27 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 17 Feb 2016 19:07:20 +0100 Subject: [PATCH] Changed trigger constructor. No longer needs model AND journal, now only model. Wait for it... --- app/Rules/Processor.php | 6 +- app/Rules/Triggers/AmountExactly.php | 16 ++--- app/Rules/Triggers/AmountLess.php | 17 ++--- app/Rules/Triggers/AmountMore.php | 17 ++--- app/Rules/Triggers/DescriptionContains.php | 17 ++--- app/Rules/Triggers/DescriptionEnds.php | 17 ++--- app/Rules/Triggers/DescriptionIs.php | 49 +++++++------- app/Rules/Triggers/DescriptionStarts.php | 53 +++++++-------- app/Rules/Triggers/FromAccountContains.php | 57 ++++++++-------- app/Rules/Triggers/FromAccountEnds.php | 77 +++++++++++----------- app/Rules/Triggers/FromAccountIs.php | 17 ++--- app/Rules/Triggers/FromAccountStarts.php | 17 ++--- app/Rules/Triggers/ToAccountContains.php | 58 ++++++++-------- app/Rules/Triggers/ToAccountEnds.php | 77 +++++++++++----------- app/Rules/Triggers/ToAccountIs.php | 49 +++++++------- app/Rules/Triggers/ToAccountStarts.php | 53 +++++++-------- app/Rules/Triggers/TransactionType.php | 47 ++++++------- app/Rules/Triggers/TriggerFactory.php | 11 ++-- app/Rules/Triggers/TriggerInterface.php | 9 +-- app/Rules/Triggers/UserAction.php | 15 +++-- 20 files changed, 347 insertions(+), 332 deletions(-) diff --git a/app/Rules/Processor.php b/app/Rules/Processor.php index 05022e9ec2..64431de106 100644 --- a/app/Rules/Processor.php +++ b/app/Rules/Processor.php @@ -112,9 +112,9 @@ class Processor foreach ($this->triggers as $trigger) { $foundTriggers++; - /** @var TriggerInterface $triggerClass */ - $triggerClass = TriggerFactory::getTrigger($trigger, $this->journal); - if ($triggerClass->triggered()) { + /** @var TriggerInterface $triggerObject */ + $triggerObject = TriggerFactory::getTrigger($trigger); + if ($triggerObject->triggered()) { $hitTriggers++; } if ($trigger->stop_processing) { diff --git a/app/Rules/Triggers/AmountExactly.php b/app/Rules/Triggers/AmountExactly.php index 2ea3afdc3a..b07b8a5368 100644 --- a/app/Rules/Triggers/AmountExactly.php +++ b/app/Rules/Triggers/AmountExactly.php @@ -22,21 +22,19 @@ use Log; */ class AmountExactly implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -65,11 +63,13 @@ class AmountExactly implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $amount = $this->journal->amount_positive; + $amount = $journal->amount_positive; $compare = $this->trigger->trigger_value; $result = bccomp($amount, $compare, 4); if ($result === 0) { diff --git a/app/Rules/Triggers/AmountLess.php b/app/Rules/Triggers/AmountLess.php index 991d6365a3..87271fdd9f 100644 --- a/app/Rules/Triggers/AmountLess.php +++ b/app/Rules/Triggers/AmountLess.php @@ -22,21 +22,20 @@ use Log; */ class AmountLess implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -65,11 +64,13 @@ class AmountLess implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $amount = $this->journal->amount_positive; + $amount = $journal->amount_positive; $compare = $this->trigger->trigger_value; $result = bccomp($amount, $compare, 4); if ($result === -1) { diff --git a/app/Rules/Triggers/AmountMore.php b/app/Rules/Triggers/AmountMore.php index 104d0363ef..6e17ff8d68 100644 --- a/app/Rules/Triggers/AmountMore.php +++ b/app/Rules/Triggers/AmountMore.php @@ -22,21 +22,20 @@ use Log; */ class AmountMore implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -65,11 +64,13 @@ class AmountMore implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $amount = $this->journal->amount_positive; + $amount = $journal->amount_positive; $compare = $this->trigger->trigger_value; $result = bccomp($amount, $compare, 4); if ($result === 1) { diff --git a/app/Rules/Triggers/DescriptionContains.php b/app/Rules/Triggers/DescriptionContains.php index 0d760016c9..e88dd97324 100644 --- a/app/Rules/Triggers/DescriptionContains.php +++ b/app/Rules/Triggers/DescriptionContains.php @@ -22,21 +22,20 @@ use Log; */ class DescriptionContains implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -65,12 +64,14 @@ class DescriptionContains implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { $search = strtolower($this->trigger->trigger_value); - $source = strtolower($this->journal->description); + $source = strtolower($journal->description); $strpos = strpos($source, $search); if (!($strpos === false)) { diff --git a/app/Rules/Triggers/DescriptionEnds.php b/app/Rules/Triggers/DescriptionEnds.php index 951cf8d7e1..2b419415e7 100644 --- a/app/Rules/Triggers/DescriptionEnds.php +++ b/app/Rules/Triggers/DescriptionEnds.php @@ -21,21 +21,20 @@ use Log; */ class DescriptionEnds implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -64,11 +63,13 @@ class DescriptionEnds implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $description = strtolower($this->journal->description); + $description = strtolower($journal->description); $descriptionLength = strlen($description); $search = strtolower($this->trigger->trigger_value); $searchLength = strlen($search); diff --git a/app/Rules/Triggers/DescriptionIs.php b/app/Rules/Triggers/DescriptionIs.php index 713aaa8fc8..95b8a134ae 100644 --- a/app/Rules/Triggers/DescriptionIs.php +++ b/app/Rules/Triggers/DescriptionIs.php @@ -21,39 +21,19 @@ use Log; */ class DescriptionIs implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $description = strtolower($this->journal->description); - $search = strtolower($this->trigger->trigger_value); - - if ($description == $search) { - Log::debug('"' . $description . '" equals "' . $search . '" exactly. Return true.'); - - return true; - } - Log::debug('"' . $description . '" does not equal "' . $search . '". Return false.'); - - return false; } @@ -81,4 +61,25 @@ class DescriptionIs implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $description = strtolower($journal->description); + $search = strtolower($this->trigger->trigger_value); + + if ($description == $search) { + Log::debug('"' . $description . '" equals "' . $search . '" exactly. Return true.'); + + return true; + } + Log::debug('"' . $description . '" does not equal "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/DescriptionStarts.php b/app/Rules/Triggers/DescriptionStarts.php index a9cc27a44a..5f8386a402 100644 --- a/app/Rules/Triggers/DescriptionStarts.php +++ b/app/Rules/Triggers/DescriptionStarts.php @@ -21,41 +21,19 @@ use Log; */ class DescriptionStarts implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $description = strtolower($this->journal->description); - $search = strtolower($this->trigger->trigger_value); - - $part = substr($description, 0, strlen($search)); - - if ($part == $search) { - Log::debug('"' . $description . '" starts with "' . $search . '". Return true.'); - - return true; - } - Log::debug('"' . $description . '" does not start with "' . $search . '". Return false.'); - - return false; } @@ -83,4 +61,27 @@ class DescriptionStarts implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $description = strtolower($journal->description); + $search = strtolower($this->trigger->trigger_value); + + $part = substr($description, 0, strlen($search)); + + if ($part == $search) { + Log::debug('"' . $description . '" starts with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $description . '" does not start with "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/FromAccountContains.php b/app/Rules/Triggers/FromAccountContains.php index 8e600b4d95..5b6988fa61 100644 --- a/app/Rules/Triggers/FromAccountContains.php +++ b/app/Rules/Triggers/FromAccountContains.php @@ -21,43 +21,19 @@ use Log; */ class FromAccountContains implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $fromAccountName = strtolower($this->journal->source_account->name); - $search = strtolower($this->trigger->trigger_value); - $strpos = strpos($fromAccountName, $search); - - if (!($strpos === false)) { - // found something - Log::debug('"' . $fromAccountName . '" contains the text "' . $search . '". Return true.'); - - return true; - } - - // found nothing. - Log::debug('"' . $fromAccountName . '" does not contain the text "' . $search . '". Return false.'); - - return false; } @@ -85,4 +61,29 @@ class FromAccountContains implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $fromAccountName = strtolower($journal->source_account->name); + $search = strtolower($this->trigger->trigger_value); + $strpos = strpos($fromAccountName, $search); + + if (!($strpos === false)) { + // found something + Log::debug('"' . $fromAccountName . '" contains the text "' . $search . '". Return true.'); + + return true; + } + + // found nothing. + Log::debug('"' . $fromAccountName . '" does not contain the text "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/FromAccountEnds.php b/app/Rules/Triggers/FromAccountEnds.php index 0c49e371e8..a6ef9bd98e 100644 --- a/app/Rules/Triggers/FromAccountEnds.php +++ b/app/Rules/Triggers/FromAccountEnds.php @@ -21,53 +21,19 @@ use Log; */ class FromAccountEnds implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $name = strtolower($this->journal->source_account->name); - $nameLength = strlen($name); - $search = strtolower($this->trigger->trigger_value); - $searchLength = strlen($search); - - // if the string to search for is longer than the account name, - // shorten the search string. - if ($searchLength > $nameLength) { - Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $name . '" (' . $nameLength . '). '); - $search = substr($search, ($nameLength * -1)); - $searchLength = strlen($search); - Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); - } - - - $part = substr($name, $searchLength * -1); - - if ($part == $search) { - Log::debug('"' . $name . '" ends with "' . $search . '". Return true.'); - - return true; - } - Log::debug('"' . $name . '" does not end with "' . $search . '". Return false.'); - - return false; } @@ -95,4 +61,39 @@ class FromAccountEnds implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $name = strtolower($journal->source_account->name); + $nameLength = strlen($name); + $search = strtolower($this->trigger->trigger_value); + $searchLength = strlen($search); + + // if the string to search for is longer than the account name, + // shorten the search string. + if ($searchLength > $nameLength) { + Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $name . '" (' . $nameLength . '). '); + $search = substr($search, ($nameLength * -1)); + $searchLength = strlen($search); + Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); + } + + + $part = substr($name, $searchLength * -1); + + if ($part == $search) { + Log::debug('"' . $name . '" ends with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $name . '" does not end with "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/FromAccountIs.php b/app/Rules/Triggers/FromAccountIs.php index 322890b90e..3105a70150 100644 --- a/app/Rules/Triggers/FromAccountIs.php +++ b/app/Rules/Triggers/FromAccountIs.php @@ -21,21 +21,20 @@ use Log; */ class FromAccountIs implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -64,11 +63,13 @@ class FromAccountIs implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $fromAccountName = strtolower($this->journal->source_account->name); + $fromAccountName = strtolower($journal->source_account->name); $search = strtolower($this->trigger->trigger_value); if ($fromAccountName == $search) { diff --git a/app/Rules/Triggers/FromAccountStarts.php b/app/Rules/Triggers/FromAccountStarts.php index e4b94aa896..a627f7a843 100644 --- a/app/Rules/Triggers/FromAccountStarts.php +++ b/app/Rules/Triggers/FromAccountStarts.php @@ -21,21 +21,20 @@ use Log; */ class FromAccountStarts implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } /** @@ -64,11 +63,13 @@ class FromAccountStarts implements TriggerInterface } /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { - $fromAccountName = strtolower($this->journal->source_account->name); + $fromAccountName = strtolower($journal->source_account->name); $search = strtolower($this->trigger->trigger_value); $part = substr($fromAccountName, 0, strlen($search)); diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php index 0f823d453f..cdbcb0b26e 100644 --- a/app/Rules/Triggers/ToAccountContains.php +++ b/app/Rules/Triggers/ToAccountContains.php @@ -21,47 +21,22 @@ use Log; */ class ToAccountContains implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $toAccountName = strtolower($this->journal->destination_account->name); - $search = strtolower($this->trigger->trigger_value); - $strpos = strpos($toAccountName, $search); - - if (!($strpos === false)) { - // found something - Log::debug('"' . $toAccountName . '" contains the text "' . $search . '". Return true.'); - - return true; - } - - // found nothing. - Log::debug('"' . $toAccountName . '" does not contain the text "' . $search . '". Return false.'); - - return false; } - /** * A trigger is said to "match anything", or match any given transaction, * when the trigger value is very vague or has no restrictions. Easy examples @@ -86,4 +61,29 @@ class ToAccountContains implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $toAccountName = strtolower($journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + $strpos = strpos($toAccountName, $search); + + if (!($strpos === false)) { + // found something + Log::debug('"' . $toAccountName . '" contains the text "' . $search . '". Return true.'); + + return true; + } + + // found nothing. + Log::debug('"' . $toAccountName . '" does not contain the text "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php index 3542b0594c..0f3046f7eb 100644 --- a/app/Rules/Triggers/ToAccountEnds.php +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -21,53 +21,19 @@ use Log; */ class ToAccountEnds implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $toAccountName = strtolower($this->journal->destination_account->name); - $toAccountNameLength = strlen($toAccountName); - $search = strtolower($this->trigger->trigger_value); - $searchLength = strlen($search); - - // if the string to search for is longer than the account name, - // shorten the search string. - if ($searchLength > $toAccountNameLength) { - Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $toAccountName . '" (' . $toAccountNameLength . '). '); - $search = substr($search, ($toAccountNameLength * -1)); - $searchLength = strlen($search); - Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); - } - - - $part = substr($toAccountName, $searchLength * -1); - - if ($part == $search) { - Log::debug('"' . $toAccountName . '" ends with "' . $search . '". Return true.'); - - return true; - } - Log::debug('"' . $toAccountName . '" does not end with "' . $search . '". Return false.'); - - return false; } @@ -95,4 +61,39 @@ class ToAccountEnds implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $toAccountName = strtolower($journal->destination_account->name); + $toAccountNameLength = strlen($toAccountName); + $search = strtolower($this->trigger->trigger_value); + $searchLength = strlen($search); + + // if the string to search for is longer than the account name, + // shorten the search string. + if ($searchLength > $toAccountNameLength) { + Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $toAccountName . '" (' . $toAccountNameLength . '). '); + $search = substr($search, ($toAccountNameLength * -1)); + $searchLength = strlen($search); + Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); + } + + + $part = substr($toAccountName, $searchLength * -1); + + if ($part == $search) { + Log::debug('"' . $toAccountName . '" ends with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not end with "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php index 7034d19072..cee32af66d 100644 --- a/app/Rules/Triggers/ToAccountIs.php +++ b/app/Rules/Triggers/ToAccountIs.php @@ -21,39 +21,19 @@ use Log; */ class ToAccountIs implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $toAccountName = strtolower($this->journal->destination_account->name); - $search = strtolower($this->trigger->trigger_value); - - if ($toAccountName == $search) { - Log::debug('"' . $toAccountName . '" equals "' . $search . '" exactly. Return true.'); - - return true; - } - Log::debug('"' . $toAccountName . '" does not equal "' . $search . '". Return false.'); - - return false; } @@ -81,4 +61,25 @@ class ToAccountIs implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $toAccountName = strtolower($journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + + if ($toAccountName == $search) { + Log::debug('"' . $toAccountName . '" equals "' . $search . '" exactly. Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not equal "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php index 57242382ce..6c9b1d876c 100644 --- a/app/Rules/Triggers/ToAccountStarts.php +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -21,41 +21,19 @@ use Log; */ class ToAccountStarts implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - - /** - * @return bool - */ - public function triggered() - { - $toAccountName = strtolower($this->journal->destination_account->name); - $search = strtolower($this->trigger->trigger_value); - - $part = substr($toAccountName, 0, strlen($search)); - - if ($part == $search) { - Log::debug('"' . $toAccountName . '" starts with "' . $search . '". Return true.'); - - return true; - } - Log::debug('"' . $toAccountName . '" does not start with "' . $search . '". Return false.'); - - return false; } @@ -83,4 +61,27 @@ class ToAccountStarts implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $toAccountName = strtolower($journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + + $part = substr($toAccountName, 0, strlen($search)); + + if ($part == $search) { + Log::debug('"' . $toAccountName . '" starts with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not start with "' . $search . '". Return false.'); + + return false; + + } } diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php index 2573a0ef96..3ff4ed9b68 100644 --- a/app/Rules/Triggers/TransactionType.php +++ b/app/Rules/Triggers/TransactionType.php @@ -21,39 +21,20 @@ use Log; */ class TransactionType implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; - } - /** - * @return bool - */ - public function triggered() - { - $type = strtolower($this->journal->transactionType->type); - $search = strtolower($this->trigger->trigger_value); - - if ($type == $search) { - Log::debug('Journal is of type "' . $type . '" which matches with "' . $search . '". Return true'); - - return true; - } - Log::debug('Journal is of type "' . $type . '" which does not match with "' . $search . '". Return false'); - - return false; } /** @@ -80,4 +61,24 @@ class TransactionType implements TriggerInterface return true; } + + /** + * @param TransactionJournal $journal + * + * @return bool + */ + public function triggered(TransactionJournal $journal) + { + $type = strtolower($journal->transactionType->type); + $search = strtolower($this->trigger->trigger_value); + + if ($type == $search) { + Log::debug('Journal is of type "' . $type . '" which matches with "' . $search . '". Return true'); + + return true; + } + Log::debug('Journal is of type "' . $type . '" which does not match with "' . $search . '". Return false'); + + return false; + } } diff --git a/app/Rules/Triggers/TriggerFactory.php b/app/Rules/Triggers/TriggerFactory.php index 2eaf47e354..d189be1e62 100644 --- a/app/Rules/Triggers/TriggerFactory.php +++ b/app/Rules/Triggers/TriggerFactory.php @@ -12,7 +12,6 @@ namespace FireflyIII\Rules\Triggers; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\RuleTrigger; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Support\Domain; /** @@ -27,17 +26,16 @@ class TriggerFactory /** * Returns the trigger for the given type and journal * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger * * @return TriggerInterface */ - public static function getTrigger(RuleTrigger $trigger, TransactionJournal $journal): TriggerInterface + public static function getTrigger(RuleTrigger $trigger): TriggerInterface { $triggerType = $trigger->trigger_type; $class = self::getTriggerClass($triggerType); - return new $class($trigger, $journal); + return new $class($trigger); } /** @@ -45,7 +43,8 @@ class TriggerFactory * * @param string $triggerType * - * @return TriggerInterface + * @return TriggerInterface|string + * @throws FireflyException */ public static function getTriggerClass(string $triggerType): string { diff --git a/app/Rules/Triggers/TriggerInterface.php b/app/Rules/Triggers/TriggerInterface.php index 1fa4c77ac2..fc96207354 100644 --- a/app/Rules/Triggers/TriggerInterface.php +++ b/app/Rules/Triggers/TriggerInterface.php @@ -23,10 +23,9 @@ interface TriggerInterface /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal); + public function __construct(RuleTrigger $trigger); /** * A trigger is said to "match anything", or match any given transaction, @@ -47,7 +46,9 @@ interface TriggerInterface public static function willMatchEverything($value = null); /** + * @param TransactionJournal $journal + * * @return bool */ - public function triggered(); + public function triggered(TransactionJournal $journal); } diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index f9e2046dfe..e03d7d1ee8 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -21,21 +21,20 @@ use Log; */ class UserAction implements TriggerInterface { - /** @var TransactionJournal */ - protected $journal; + + /** @var RuleTrigger */ protected $trigger; /** * TriggerInterface constructor. * - * @param RuleTrigger $trigger - * @param TransactionJournal $journal + * @param RuleTrigger $trigger */ - public function __construct(RuleTrigger $trigger, TransactionJournal $journal) + public function __construct(RuleTrigger $trigger) { $this->trigger = $trigger; - $this->journal = $journal; + } @@ -63,9 +62,11 @@ class UserAction implements TriggerInterface /** * This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition. * + * @param TransactionJournal $journal + * * @return bool */ - public function triggered() + public function triggered(TransactionJournal $journal) { Log::debug('user_action always returns true.');