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