Cleaned up the Processor. This completely breaks everything @ roberthorlings has built but stay with me.

This commit is contained in:
James Cole 2016-02-17 18:47:05 +01:00
parent 2bd2f5a5aa
commit 8024ad123e
3 changed files with 31 additions and 73 deletions

View File

@ -62,11 +62,10 @@ class FireRulesForStore
->get(['rules.*']); ->get(['rules.*']);
/** @var Rule $rule */ /** @var Rule $rule */
foreach ($rules as $rule) { foreach ($rules as $rule) {
Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')');
$processor = new Processor($rule, $event->journal);
// get some return out of this? Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')');
$processor->handle(); $processor = Processor::make($rule);
$processor->handleTransactionJournal($event->journal);
if ($rule->stop_processing) { if ($rule->stop_processing) {
break; break;

View File

@ -60,10 +60,10 @@ class FireRulesForUpdate
/** @var Rule $rule */ /** @var Rule $rule */
foreach ($rules as $rule) { foreach ($rules as $rule) {
Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')'); Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')');
$processor = new Processor($rule, $event->journal);
// get some return out of this? Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title . ')');
$processor->handle(); $processor = Processor::make($rule);
$processor->handleTransactionJournal($event->journal);
if ($rule->stop_processing) { if ($rule->stop_processing) {
break; break;

View File

@ -18,6 +18,7 @@ use FireflyIII\Rules\Actions\ActionFactory;
use FireflyIII\Rules\Actions\ActionInterface; use FireflyIII\Rules\Actions\ActionInterface;
use FireflyIII\Rules\Triggers\TriggerFactory; use FireflyIII\Rules\Triggers\TriggerFactory;
use FireflyIII\Rules\Triggers\TriggerInterface; use FireflyIII\Rules\Triggers\TriggerInterface;
use Illuminate\Support\Collection;
use Log; use Log;
/** /**
@ -27,57 +28,45 @@ use Log;
*/ */
class Processor class Processor
{ {
/** @var Collection */
private $actions;
/** @var TransactionJournal */ /** @var TransactionJournal */
protected $journal; private $journal;
/** @var Rule */ /** @var Rule */
protected $rule; private $rule;
/** @var Collection */
private $triggers;
/** /**
* Processor constructor. * Processor constructor.
* *
* @param Rule $rule
* @param TransactionJournal $journal
*/ */
public function __construct(Rule $rule, TransactionJournal $journal) private function __construct()
{ {
$this->rule = $rule;
$this->journal = $journal;
}
/**
* @return TransactionJournal
*/
public function getJournal()
{
return $this->journal;
}
/**
* @param TransactionJournal $journal
*/
public function setJournal($journal)
{
$this->journal = $journal;
}
/**
* @return Rule
*/
public function getRule()
{
return $this->rule;
} }
/** /**
* @param Rule $rule * @param Rule $rule
*
* @return Processor
*/ */
public function setRule($rule) public static function make(Rule $rule)
{ {
$this->rule = $rule; $self = new self;
$self->rule = $rule;
$self->triggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
$self->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();
return $self;
} }
public function handle() /**
* @param TransactionJournal $journal
*/
public function handleTransactionJournal(TransactionJournal $journal)
{ {
$this->journal = $journal;
// get all triggers: // get all triggers:
$triggered = $this->triggered(); $triggered = $this->triggered();
if ($triggered) { if ($triggered) {
@ -87,30 +76,10 @@ class Processor
} }
/**
* Checks whether the current transaction is triggered by the current rule
*
* @return boolean
*/
public function isTriggered()
{
return $this->triggered();
}
/**
* Checks whether the current transaction is triggered by the list of given triggers
*
* @return boolean
*/
public function isTriggeredBy(array $triggers)
{
return $this->triggeredBy($triggers);
}
/** /**
* @return bool * @return bool
*/ */
protected function actions() private function actions()
{ {
/** /**
* @var int $index * @var int $index
@ -129,28 +98,18 @@ class Processor
return true; return true;
} }
/**
* Checks whether the current transaction is triggered by the current rule
*
* @return bool
*/
protected function triggered()
{
return $this->triggeredBy($this->rule->ruleTriggers()->orderBy('order', 'ASC')->get());
}
/** /**
* Method to check whether the current transaction would be triggered * Method to check whether the current transaction would be triggered
* by the given list of triggers * by the given list of triggers
* *
* @return bool * @return bool
*/ */
protected function triggeredBy($triggers) private function triggered()
{ {
$foundTriggers = 0; $foundTriggers = 0;
$hitTriggers = 0; $hitTriggers = 0;
/** @var RuleTrigger $trigger */ /** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) { foreach ($this->triggers as $trigger) {
$foundTriggers++; $foundTriggers++;
/** @var TriggerInterface $triggerClass */ /** @var TriggerInterface $triggerClass */