Ejemplo n.º 1
0
 /**
  * Handle the event.
  *
  * @param  TransactionJournalUpdated $event
  *
  * @return void
  */
 public function handle(TransactionJournalUpdated $event)
 {
     // get all the user's rule groups, with the rules, order by 'order'.
     /** @var User $user */
     $user = Auth::user();
     $groups = $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get();
     //
     /** @var RuleGroup $group */
     foreach ($groups as $group) {
         Log::debug('Now processing group "' . $group->title . '".');
         $rules = $group->rules()->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')->where('rule_triggers.trigger_type', 'user_action')->where('rule_triggers.trigger_value', 'update-journal')->where('rules.active', 1)->get(['rules.*']);
         /** @var Rule $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?
             $processor->handle();
             if ($rule->stop_processing) {
                 break;
             }
         }
     }
 }
 /**
  * Connect a new transaction journal to any related piggy banks.
  *
  * @param  TransactionJournalStored $event
  *
  * @return bool
  */
 public function handle(TransactionJournalStored $event) : bool
 {
     // get all the user's rule groups, with the rules, order by 'order'.
     /** @var User $user */
     $user = auth()->user();
     $groups = $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get();
     //
     /** @var RuleGroup $group */
     foreach ($groups as $group) {
         $rules = $group->rules()->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')->where('rule_triggers.trigger_type', 'user_action')->where('rule_triggers.trigger_value', 'store-journal')->where('rules.active', 1)->get(['rules.*']);
         /** @var Rule $rule */
         foreach ($rules as $rule) {
             $processor = Processor::make($rule);
             $processor->handleTransactionJournal($event->journal);
             if ($rule->stop_processing) {
                 return true;
             }
         }
     }
     return true;
 }
 /**
  * This method will search the user's transaction journal (with an upper limit of $range) for
  * transaction journals matching the given $triggers. This is accomplished by trying to fire these
  * triggers onto each transaction journal until enough matches are found ($limit).
  *
  * @return Collection
  *
  */
 public function findMatchingTransactions() : Collection
 {
     if (count($this->triggers) === 0) {
         return new Collection();
     }
     $pagesize = min($this->range / 2, $this->limit * 2);
     // Variables used within the loop
     $processed = 0;
     $page = 1;
     $result = new Collection();
     $processor = Processor::makeFromStringArray($this->triggers);
     // Start a loop to fetch batches of transactions. The loop will finish if:
     //   - all transactions have been fetched from the database
     //   - the maximum number of transactions to return has been found
     //   - the maximum number of transactions to search in have been searched
     do {
         // Fetch a batch of transactions from the database
         $paginator = $this->repository->getJournals($this->transactionTypes, $page, $pagesize);
         $set = $paginator->getCollection();
         // Filter transactions that match the given triggers.
         $filtered = $set->filter(function (TransactionJournal $journal) use($processor) {
             Log::debug(sprintf('Test these triggers on #%d', $journal->id));
             return $processor->handleTransactionJournal($journal);
         });
         // merge:
         /** @var Collection $result */
         $result = $result->merge($filtered);
         // Update counters
         $page++;
         $processed += count($set);
         // Check for conditions to finish the loop
         $reachedEndOfList = $set->count() < $pagesize;
         $foundEnough = $result->count() >= $this->limit;
         $searchedEnough = $processed >= $this->range;
     } while (!$reachedEndOfList && !$foundEnough && !$searchedEnough);
     // If the list of matchingTransactions is larger than the maximum number of results
     // (e.g. if a large percentage of the transactions match), truncate the list
     $result = $result->slice(0, $this->limit);
     return $result;
 }
 /**
  * Collects a list of rule processors, one for each rule within the rule group
  *
  * @return array
  */
 protected function collectProcessors()
 {
     // Find all rules belonging to this rulegroup
     $rules = $this->ruleGroup->rules()->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')->where('rule_triggers.trigger_type', 'user_action')->where('rule_triggers.trigger_value', 'store-journal')->where('rules.active', 1)->get(['rules.*']);
     // Create a list of processors for these rules
     return array_map(function ($rule) {
         return Processor::make($rule);
     }, $rules->all());
 }
Ejemplo n.º 5
0
 /**
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 private function applyRules(TransactionJournal $journal) : bool
 {
     if ($this->rules->count() > 0) {
         /** @var Rule $rule */
         foreach ($this->rules as $rule) {
             Log::debug(sprintf('Going to apply rule #%d to journal %d.', $rule->id, $journal->id));
             $processor = Processor::make($rule);
             $processor->handleTransactionJournal($journal);
             if ($rule->stop_processing) {
                 return true;
             }
         }
     }
     return true;
 }
Ejemplo n.º 6
0
 /**
  * @param Collection         $groups
  * @param TransactionJournal $journal
  */
 private function fireRule(Collection $groups, TransactionJournal $journal)
 {
     /** @var RuleGroup $group */
     foreach ($groups as $group) {
         /** @var Rule $rule */
         foreach ($group->rules as $rule) {
             $processor = new Processor($rule, $journal);
             $processor->handle();
             if ($rule->stop_processing) {
                 break;
             }
         }
     }
 }