mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-21 09:00:07 +00:00
Rule actions no longer need to know the journal at construction time. This is easier.
This commit is contained in:
@@ -28,16 +28,15 @@ class ActionFactory
|
|||||||
* Returns the action for the given type and journal
|
* Returns the action for the given type and journal
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*
|
*
|
||||||
* @return ActionInterface
|
* @return ActionInterface
|
||||||
*/
|
*/
|
||||||
public static function getAction(RuleAction $action, TransactionJournal $journal): ActionInterface
|
public static function getAction(RuleAction $action): ActionInterface
|
||||||
{
|
{
|
||||||
$actionType = $action->action_type;
|
$actionType = $action->action_type;
|
||||||
$class = self::getActionClass($actionType);
|
$class = self::getActionClass($actionType);
|
||||||
|
|
||||||
return new $class($action, $journal);
|
return new $class($action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -24,12 +24,13 @@ interface ActionInterface
|
|||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal);
|
public function __construct(RuleAction $action);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act();
|
public function act(TransactionJournal $journal);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,32 +24,32 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
class AddTag implements ActionInterface
|
class AddTag implements ActionInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @var RuleAction */
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
// journal has this tag maybe?
|
// journal has this tag maybe?
|
||||||
$tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => Auth::user()->id]);
|
$tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => Auth::user()->id]);
|
||||||
|
|
||||||
$count = $this->journal->tags()->where('id', $tag->id)->count();
|
$count = $journal->tags()->where('id', $tag->id)->count();
|
||||||
if ($count == 0) {
|
if ($count == 0) {
|
||||||
$this->journal->tags()->save($tag);
|
$journal->tags()->save($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -22,27 +22,27 @@ class AppendDescription implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->description = $this->journal->description . $this->action->action_value;
|
$journal->description = $journal->description . $this->action->action_value;
|
||||||
$this->journal->save();
|
$journal->save();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,26 +23,26 @@ class ClearBudget implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->budgets()->detach();
|
$journal->budgets()->detach();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,26 +23,26 @@ class ClearCategory implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->categories()->detach();
|
$journal->categories()->detach();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,27 +22,27 @@ class PrependDescription implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->description = $this->action->action_value . $this->journal->description;
|
$journal->description = $this->action->action_value . $journal->description;
|
||||||
$this->journal->save();
|
$journal->save();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,26 +22,26 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
class RemoveAllTags implements ActionInterface
|
class RemoveAllTags implements ActionInterface
|
||||||
{
|
{
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->tags()->detach();
|
$journal->tags()->detach();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|||||||
@@ -25,24 +25,24 @@ class RemoveTag implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
// if tag does not exist, no need to continue:
|
// if tag does not exist, no need to continue:
|
||||||
$name = $this->action->action_value;
|
$name = $this->action->action_value;
|
||||||
@@ -54,7 +54,7 @@ class RemoveTag implements ActionInterface
|
|||||||
)->first();
|
)->first();
|
||||||
|
|
||||||
if (!is_null($tag)) {
|
if (!is_null($tag)) {
|
||||||
$this->journal->tags()->detach([$tag->id]);
|
$journal->tags()->detach([$tag->id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -26,24 +26,24 @@ class SetBudget implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
/** @var BudgetRepositoryInterface $repository */
|
/** @var BudgetRepositoryInterface $repository */
|
||||||
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
||||||
@@ -55,8 +55,8 @@ class SetBudget implements ActionInterface
|
|||||||
}
|
}
|
||||||
)->first();
|
)->first();
|
||||||
if (!is_null($budget)) {
|
if (!is_null($budget)) {
|
||||||
Log::debug('Will set budget "' . $search . '" (#' . $budget->id . ') on journal #' . $this->journal->id . '.');
|
Log::debug('Will set budget "' . $search . '" (#' . $budget->id . ') on journal #' . $journal->id . '.');
|
||||||
$this->journal->budgets()->sync([$budget->id]);
|
$journal->budgets()->sync([$budget->id]);
|
||||||
} else {
|
} else {
|
||||||
Log::debug('Could not find budget "' . $search . '". Failed.');
|
Log::debug('Could not find budget "' . $search . '". Failed.');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,29 +26,29 @@ class SetCategory implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$name = $this->action->action_value;
|
$name = $this->action->action_value;
|
||||||
$category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => Auth::user()->id]);
|
$category = Category::firstOrCreateEncrypted(['name' => $name, 'user_id' => Auth::user()->id]);
|
||||||
Log::debug('Will set category "' . $name . '" (#' . $category->id . ') on journal #' . $this->journal->id . '.');
|
Log::debug('Will set category "' . $name . '" (#' . $category->id . ') on journal #' . $journal->id . '.');
|
||||||
$this->journal->categories()->sync([$category->id]);
|
$journal->categories()->sync([$category->id]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,27 +22,27 @@ class SetDescription implements ActionInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
private $journal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
*
|
*
|
||||||
* @param RuleAction $action
|
* @param RuleAction $action
|
||||||
* @param TransactionJournal $journal
|
|
||||||
*/
|
*/
|
||||||
public function __construct(RuleAction $action, TransactionJournal $journal)
|
public function __construct(RuleAction $action)
|
||||||
{
|
{
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->journal = $journal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param TransactionJournal $journal
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act()
|
public function act(TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
$this->journal->description = $this->action->action_value;
|
$journal->description = $this->action->action_value;
|
||||||
$this->journal->save();
|
$journal->save();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,8 +87,8 @@ class Processor
|
|||||||
*/
|
*/
|
||||||
foreach ($this->actions as $action) {
|
foreach ($this->actions as $action) {
|
||||||
/** @var ActionInterface $actionClass */
|
/** @var ActionInterface $actionClass */
|
||||||
$actionClass = ActionFactory::getAction($action, $this->journal);
|
$actionClass = ActionFactory::getAction($action);
|
||||||
$actionClass->act();
|
$actionClass->act($this->journal);
|
||||||
if ($action->stop_processing) {
|
if ($action->stop_processing) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user