firefly-iii/app/Handlers/Events/FireRulesForStore.php

75 lines
2.1 KiB
PHP
Raw Normal View History

2016-01-12 14:38:12 -06:00
<?php
/**
* FireRulesForStore.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Handlers\Events;
use FireflyIII\Events\TransactionJournalStored;
2016-01-13 00:16:29 -06:00
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Rules\Processor;
use FireflyIII\User;
use Illuminate\Support\Facades\Auth;
2016-01-12 22:37:25 -06:00
use Log;
2016-01-12 14:38:12 -06:00
/**
* Class FireRulesForStore
*
* @package FireflyIII\Handlers\Events
*/
class FireRulesForStore
{
/**
* Create the event handler.
*
* @codeCoverageIgnore
*
*/
public function __construct()
{
//
}
/**
* Connect a new transaction journal to any related piggy banks.
*
* @param TransactionJournalStored $event
*
* @return boolean
*/
public function handle(TransactionJournalStored $event)
{
2016-01-13 07:02:22 -06:00
Log::debug('Before event (in handle). From account name is: ' . $event->journal->source_account->name);
2016-01-12 14:38:12 -06:00
// get all the user's rule groups, with the rules, order by 'order'.
2016-01-13 00:16:29 -06:00
/** @var User $user */
$user = Auth::user();
2016-01-13 02:08:13 -06:00
$groups = $user->ruleGroups()->orderBy('order', 'ASC')->get();
2016-01-13 00:16:29 -06:00
//
/** @var RuleGroup $group */
foreach ($groups as $group) {
2016-01-13 07:02:22 -06:00
Log::debug('Now processing group "' . $group->title . '".');
2016-01-13 02:08:13 -06:00
$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')
->get(['rules.*']);
2016-01-13 00:16:29 -06:00
/** @var Rule $rule */
foreach ($rules as $rule) {
2016-01-13 07:02:22 -06:00
Log::debug('Now handling rule #' . $rule->id . ' (' . $rule->title. ')');
$processor = new Processor($rule, $event->journal);
2016-01-13 00:16:29 -06:00
// get some return out of this?
$processor->handle();
}
}
2016-01-13 07:02:22 -06:00
// echo 'Done processing rules. See log.';
// exit;
2016-01-12 14:38:12 -06:00
}
}