1 | <?php |
||
2 | /** |
||
3 | * CategoryUpdateService.php |
||
4 | * Copyright (c) 2019 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Services\Internal\Update; |
||
25 | |||
26 | use FireflyIII\Models\Category; |
||
27 | use FireflyIII\Models\RuleAction; |
||
28 | use FireflyIII\Models\RuleTrigger; |
||
29 | use Log; |
||
30 | |||
31 | /** |
||
32 | * Class CategoryUpdateService |
||
33 | * |
||
34 | * @codeCoverageIgnore |
||
35 | */ |
||
36 | class CategoryUpdateService |
||
37 | { |
||
38 | /** |
||
39 | * Constructor. |
||
40 | */ |
||
41 | public function __construct() |
||
42 | { |
||
43 | if ('testing' === config('app.env')) { |
||
44 | Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param Category $category |
||
50 | * @param array $data |
||
51 | * |
||
52 | * @return Category |
||
53 | */ |
||
54 | public function update(Category $category, array $data): Category |
||
55 | { |
||
56 | $oldName = $category->name; |
||
57 | $category->name = $data['name']; |
||
58 | $category->save(); |
||
59 | |||
60 | // update triggers and actions |
||
61 | $this->updateRuleTriggers($oldName, $data['name']); |
||
62 | $this->updateRuleActions($oldName, $data['name']); |
||
63 | |||
64 | return $category; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string $oldName |
||
69 | * @param string $newName |
||
70 | */ |
||
71 | private function updateRuleActions(string $oldName, string $newName): void |
||
72 | { |
||
73 | $types = ['set_category',]; |
||
74 | $actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id') |
||
75 | ->where('rules.user_id', $this->user->id) |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
76 | ->whereIn('rule_actions.action_type', $types) |
||
77 | ->where('rule_actions.action_value', $oldName) |
||
78 | ->get(['rule_actions.*']); |
||
79 | Log::debug(sprintf('Found %d actions to update.', $actions->count())); |
||
80 | /** @var RuleAction $action */ |
||
81 | foreach ($actions as $action) { |
||
82 | $action->action_value = $newName; |
||
83 | $action->save(); |
||
84 | Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value)); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $oldName |
||
90 | * @param string $newName |
||
91 | */ |
||
92 | private function updateRuleTriggers(string $oldName, string $newName): void |
||
93 | { |
||
94 | $types = ['category_is',]; |
||
95 | $triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id') |
||
96 | ->where('rules.user_id', $this->user->id) |
||
0 ignored issues
–
show
|
|||
97 | ->whereIn('rule_triggers.trigger_type', $types) |
||
98 | ->where('rule_triggers.trigger_value', $oldName) |
||
99 | ->get(['rule_triggers.*']); |
||
100 | Log::debug(sprintf('Found %d triggers to update.', $triggers->count())); |
||
101 | /** @var RuleTrigger $trigger */ |
||
102 | foreach ($triggers as $trigger) { |
||
103 | $trigger->trigger_value = $newName; |
||
104 | $trigger->save(); |
||
105 | Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value)); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | } |
||
110 |