Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

RuleRepository::resetRulesInGroupOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
/**
3
 * RuleRepository.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III 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 General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Repositories\Rule;
24
25
use FireflyIII\Exceptions\FireflyException;
26
use FireflyIII\Models\Rule;
27
use FireflyIII\Models\RuleAction;
28
use FireflyIII\Models\RuleGroup;
29
use FireflyIII\Models\RuleTrigger;
30
use FireflyIII\User;
31
32
/**
33
 * Class RuleRepository.
34
 */
35
class RuleRepository implements RuleRepositoryInterface
36
{
37
    /** @var User */
38
    private $user;
39
40
    /**
41
     * @return int
42
     */
43
    public function count(): int
44
    {
45
        return $this->user->rules()->count();
46
    }
47
48
    /**
49
     * @param Rule $rule
50
     *
51
     * @return bool
52
     *
53
     */
54
    public function destroy(Rule $rule): bool
55
    {
56
        foreach ($rule->ruleTriggers as $trigger) {
57
            $trigger->delete();
58
        }
59
        foreach ($rule->ruleActions as $action) {
60
            $action->delete();
61
        }
62
        $rule->delete();
63
64
        return true;
65
    }
66
67
    /**
68
     * @param int $ruleId
69
     *
70
     * @return Rule
71
     */
72
    public function find(int $ruleId): Rule
73
    {
74
        $rule = $this->user->rules()->find($ruleId);
75
        if (null === $rule) {
76
            return new Rule;
77
        }
78
79
        return $rule;
80
    }
81
82
    /**
83
     * FIxXME can return null.
84
     *
85
     * @return RuleGroup
86
     */
87
    public function getFirstRuleGroup(): RuleGroup
88
    {
89
        return $this->user->ruleGroups()->first();
90
    }
91
92
    /**
93
     * @param RuleGroup $ruleGroup
94
     *
95
     * @return int
96
     */
97
    public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup): int
98
    {
99
        return (int)$ruleGroup->rules()->max('order');
100
    }
101
102
    /**
103
     * @param Rule $rule
104
     *
105
     * @return string
106
     *
107
     * @throws FireflyException
108
     */
109
    public function getPrimaryTrigger(Rule $rule): string
110
    {
111
        $count = $rule->ruleTriggers()->count();
112
        if (0 === $count) {
113
            throw new FireflyException('Rules should have more than zero triggers, rule #' . $rule->id . ' has none!');
114
        }
115
116
        return $rule->ruleTriggers()->where('trigger_type', 'user_action')->first()->trigger_value;
117
    }
118
119
    /**
120
     * @param Rule $rule
121
     *
122
     * @return bool
123
     */
124
    public function moveDown(Rule $rule): bool
125
    {
126
        $order = $rule->order;
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
127
128
        // find the rule with order+1 and give it order-1
129
        $other = $rule->ruleGroup->rules()->where('order', $order + 1)->first();
130
        if ($other) {
131
            --$other->order;
132
            $other->save();
133
        }
134
135
        ++$rule->order;
136
        $rule->save();
137
        $this->resetRulesInGroupOrder($rule->ruleGroup);
138
139
        return true;
140
    }
141
142
    /**
143
     * @param Rule $rule
144
     *
145
     * @return bool
146
     */
147
    public function moveUp(Rule $rule): bool
148
    {
149
        $order = $rule->order;
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
150
151
        // find the rule with order-1 and give it order+1
152
        $other = $rule->ruleGroup->rules()->where('order', $order - 1)->first();
153
        if ($other) {
154
            ++$other->order;
155
            $other->save();
156
        }
157
158
        --$rule->order;
159
        $rule->save();
160
        $this->resetRulesInGroupOrder($rule->ruleGroup);
161
162
        return true;
163
    }
164
165
    /**
166
     * @param Rule  $rule
167
     * @param array $ids
168
     *
169
     * @return bool
170
     */
171
    public function reorderRuleActions(Rule $rule, array $ids): bool
172
    {
173
        $order = 1;
174
        foreach ($ids as $actionId) {
175
            /** @var RuleTrigger $trigger */
176
            $action = $rule->ruleActions()->find($actionId);
177
            if (null !== $action) {
178
                $action->order = $order;
179
                $action->save();
180
                ++$order;
181
            }
182
        }
183
184
        return true;
185
    }
186
187
    /**
188
     * @param Rule  $rule
189
     * @param array $ids
190
     *
191
     * @return bool
192
     */
193
    public function reorderRuleTriggers(Rule $rule, array $ids): bool
194
    {
195
        $order = 1;
196
        foreach ($ids as $triggerId) {
197
            /** @var RuleTrigger $trigger */
198
            $trigger = $rule->ruleTriggers()->find($triggerId);
199
            if (null !== $trigger) {
200
                $trigger->order = $order;
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
201
                $trigger->save();
202
                ++$order;
203
            }
204
        }
205
206
        return true;
207
    }
208
209
    /**
210
     * @param RuleGroup $ruleGroup
211
     *
212
     * @return bool
213
     */
214
    public function resetRulesInGroupOrder(RuleGroup $ruleGroup): bool
215
    {
216
        $ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);
217
218
        $set   = $ruleGroup->rules()
219
                           ->orderBy('order', 'ASC')
220
                           ->orderBy('updated_at', 'DESC')
221
                           ->get();
222
        $count = 1;
223
        /** @var Rule $entry */
224
        foreach ($set as $entry) {
225
            $entry->order = $count;
226
            $entry->save();
227
            ++$count;
228
        }
229
230
        return true;
231
    }
232
233
    /**
234
     * @param User $user
235
     */
236
    public function setUser(User $user)
237
    {
238
        $this->user = $user;
239
    }
240
241
    /**
242
     * @param array $data
243
     *
244
     * @return Rule
245
     */
246
    public function store(array $data): Rule
247
    {
248
        /** @var RuleGroup $ruleGroup */
249
        $ruleGroup = $this->user->ruleGroups()->find($data['rule_group_id']);
250
251
        // get max order:
252
        $order = $this->getHighestOrderInRuleGroup($ruleGroup);
253
254
        // start by creating a new rule:
255
        $rule = new Rule;
256
        $rule->user()->associate($this->user->id);
257
258
        $rule->rule_group_id   = $data['rule_group_id'];
0 ignored issues
show
Bug introduced by James Cole
The property rule_group_id does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
259
        $rule->order           = ($order + 1);
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
260
        $rule->active          = 1;
0 ignored issues
show
Bug introduced by James Cole
The property active does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
261
        $rule->stop_processing = 1 === (int)$data['stop_processing'];
0 ignored issues
show
Bug introduced by James Cole
The property stop_processing does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
262
        $rule->title           = $data['title'];
0 ignored issues
show
Bug introduced by James Cole
The property title does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
263
        $rule->description     = strlen($data['description']) > 0 ? $data['description'] : null;
264
265
        $rule->save();
266
267
        // start storing triggers:
268
        $this->storeTriggers($rule, $data);
269
270
        // same for actions.
271
        $this->storeActions($rule, $data);
272
273
        return $rule;
274
    }
275
276
    /**
277
     * @param Rule  $rule
278
     * @param array $values
279
     *
280
     * @return RuleAction
281
     */
282
    public function storeAction(Rule $rule, array $values): RuleAction
283
    {
284
        $ruleAction = new RuleAction;
285
        $ruleAction->rule()->associate($rule);
286
        $ruleAction->order           = $values['order'];
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\RuleAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
287
        $ruleAction->active          = 1;
0 ignored issues
show
Bug introduced by James Cole
The property active does not seem to exist on FireflyIII\Models\RuleAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
288
        $ruleAction->stop_processing = $values['stopProcessing'];
0 ignored issues
show
Bug introduced by James Cole
The property stop_processing does not seem to exist on FireflyIII\Models\RuleAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
289
        $ruleAction->action_type     = $values['action'];
0 ignored issues
show
Bug introduced by James Cole
The property action_type does not seem to exist on FireflyIII\Models\RuleAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
290
        $ruleAction->action_value    = $values['value'] ?? '';
0 ignored issues
show
Bug introduced by James Cole
The property action_value does not seem to exist on FireflyIII\Models\RuleAction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
291
        $ruleAction->save();
292
293
        return $ruleAction;
294
    }
295
296
    /**
297
     * @param Rule  $rule
298
     * @param array $values
299
     *
300
     * @return RuleTrigger
301
     */
302
    public function storeTrigger(Rule $rule, array $values): RuleTrigger
303
    {
304
        $ruleTrigger = new RuleTrigger;
305
        $ruleTrigger->rule()->associate($rule);
306
        $ruleTrigger->order           = $values['order'];
0 ignored issues
show
Bug introduced by James Cole
The property order does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
307
        $ruleTrigger->active          = 1;
0 ignored issues
show
Bug introduced by James Cole
The property active does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
308
        $ruleTrigger->stop_processing = $values['stopProcessing'];
0 ignored issues
show
Bug introduced by James Cole
The property stop_processing does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
309
        $ruleTrigger->trigger_type    = $values['action'];
0 ignored issues
show
Bug introduced by James Cole
The property trigger_type does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
310
        $ruleTrigger->trigger_value   = $values['value'] ?? '';
0 ignored issues
show
Bug introduced by James Cole
The property trigger_value does not seem to exist on FireflyIII\Models\RuleTrigger. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
311
        $ruleTrigger->save();
312
313
        return $ruleTrigger;
314
    }
315
316
    /**
317
     * @param Rule  $rule
318
     * @param array $data
319
     *
320
     * @return Rule
321
     */
322
    public function update(Rule $rule, array $data): Rule
323
    {
324
        // update rule:
325
        $rule->rule_group_id   = $data['rule_group_id'];
0 ignored issues
show
Bug introduced by James Cole
The property rule_group_id does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
326
        $rule->active          = $data['active'];
0 ignored issues
show
Bug introduced by James Cole
The property active does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
327
        $rule->stop_processing = $data['stop_processing'];
0 ignored issues
show
Bug introduced by James Cole
The property stop_processing does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
328
        $rule->title           = $data['title'];
0 ignored issues
show
Bug introduced by James Cole
The property title does not seem to exist on FireflyIII\Models\Rule. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
329
        $rule->description     = $data['description'];
330
        $rule->save();
331
332
        // delete triggers:
333
        $rule->ruleTriggers()->delete();
334
335
        // delete actions:
336
        $rule->ruleActions()->delete();
337
338
        // recreate triggers:
339
        $this->storeTriggers($rule, $data);
340
341
        // recreate actions:
342
        $this->storeActions($rule, $data);
343
344
        return $rule;
345
    }
346
347
    /**
348
     * @param Rule  $rule
349
     * @param array $data
350
     *
351
     * @return bool
352
     */
353
    private function storeActions(Rule $rule, array $data): bool
354
    {
355
        $order = 1;
356
        foreach ($data['rule-actions'] as $index => $action) {
357
            $value          = $data['rule-action-values'][$index] ?? '';
358
            $stopProcessing = isset($data['rule-action-stop'][$index]) ? true : false;
359
360
            $actionValues = [
361
                'action'         => $action,
362
                'value'          => $value,
363
                'stopProcessing' => $stopProcessing,
364
                'order'          => $order,
365
            ];
366
367
            $this->storeAction($rule, $actionValues);
368
        }
369
370
        return true;
371
    }
372
373
    /**
374
     * @param Rule  $rule
375
     * @param array $data
376
     *
377
     * @return bool
378
     */
379
    private function storeTriggers(Rule $rule, array $data): bool
380
    {
381
        $order          = 1;
382
        $stopProcessing = false;
383
384
        $triggerValues = [
385
            'action'         => 'user_action',
386
            'value'          => $data['trigger'],
387
            'stopProcessing' => $stopProcessing,
388
            'order'          => $order,
389
        ];
390
391
        $this->storeTrigger($rule, $triggerValues);
392
        foreach ($data['rule-triggers'] as $index => $trigger) {
393
            $value          = $data['rule-trigger-values'][$index] ?? '';
394
            $stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false;
395
396
            $triggerValues = [
397
                'action'         => $trigger,
398
                'value'          => $value,
399
                'stopProcessing' => $stopProcessing,
400
                'order'          => $order,
401
            ];
402
403
            $this->storeTrigger($rule, $triggerValues);
404
            ++$order;
405
        }
406
407
        return true;
408
    }
409
}
410