Completed
Push — master ( bae40e...a4cb2c )
by James
19:36 queued 09:47
created

RuleGroupRepository::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * RuleGroupRepository.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\RuleGroup;
24
25
use FireflyIII\Models\Rule;
26
use FireflyIII\Models\RuleGroup;
27
use FireflyIII\User;
28
use Illuminate\Database\Eloquent\Relations\HasMany;
29
use Illuminate\Support\Collection;
30
use Log;
31
32
/**
33
 * Class RuleGroupRepository.
34
 */
35
class RuleGroupRepository implements RuleGroupRepositoryInterface
36
{
37
    /** @var User */
38
    private $user;
39
40
    /**
41
     * Constructor.
42
     */
43
    public function __construct()
44
    {
45
        if ('testing' === env('APP_ENV')) {
46
            Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
47
        }
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function count(): int
54
    {
55
        return $this->user->ruleGroups()->count();
56
    }
57
58
    /**
59
     * @param RuleGroup      $ruleGroup
60
     * @param RuleGroup|null $moveTo
61
     *
62
     * @return bool
63
     * @throws \Exception
64
     */
65
    public function destroy(RuleGroup $ruleGroup, ?RuleGroup $moveTo): bool
66
    {
67
        /** @var Rule $rule */
68
        foreach ($ruleGroup->rules as $rule) {
69
            if (null === $moveTo) {
70
                $rule->delete();
71
                continue;
72
            }
73
            // move
74
            $rule->ruleGroup()->associate($moveTo);
75
            $rule->save();
76
        }
77
78
        $ruleGroup->delete();
79
80
        $this->resetRuleGroupOrder();
81
        if (null !== $moveTo) {
82
            $this->resetRulesInGroupOrder($moveTo);
83
        }
84
85
        return true;
86
    }
87
88
    /**
89
     * @param int $ruleGroupId
90
     *
91
     * @return RuleGroup|null
92
     */
93
    public function find(int $ruleGroupId): ?RuleGroup
94
    {
95
        $group = $this->user->ruleGroups()->find($ruleGroupId);
96
        if (null === $group) {
97
            return null;
98
        }
99
100
        return $group;
101
    }
102
103
    /**
104
     * @return Collection
105
     */
106
    public function get(): Collection
107
    {
108
        return $this->user->ruleGroups()->orderBy('order', 'ASC')->get();
109
    }
110
111
    /**
112
     * @param User $user
113
     *
114
     * @return Collection
115
     */
116
    public function getActiveGroups(User $user): Collection
117
    {
118
        return $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get(['rule_groups.*']);
119
    }
120
121
    /**
122
     * @param RuleGroup $group
123
     *
124
     * @return Collection
125
     */
126
    public function getActiveStoreRules(RuleGroup $group): Collection
127
    {
128
        return $group->rules()
129
                     ->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
130
                     ->where('rule_triggers.trigger_type', 'user_action')
131
                     ->where('rule_triggers.trigger_value', 'store-journal')
132
                     ->where('rules.active', 1)
133
                     ->get(['rules.*']);
134
    }
135
136
    /**
137
     * @param RuleGroup $group
138
     *
139
     * @return Collection
140
     */
141
    public function getActiveUpdateRules(RuleGroup $group): Collection
142
    {
143
        return $group->rules()
144
                     ->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
145
                     ->where('rule_triggers.trigger_type', 'user_action')
146
                     ->where('rule_triggers.trigger_value', 'update-journal')
147
                     ->where('rules.active', 1)
148
                     ->get(['rules.*']);
149
    }
150
151
    /**
152
     * @return int
153
     */
154
    public function getHighestOrderRuleGroup(): int
155
    {
156
        $entry = $this->user->ruleGroups()->max('order');
157
158
        return (int)$entry;
159
    }
160
161
    /**
162
     * @param User $user
163
     *
164
     * @return Collection
165
     */
166
    public function getRuleGroupsWithRules(User $user): Collection
167
    {
168
        return $user->ruleGroups()
169
                    ->orderBy('active', 'DESC')
170
                    ->orderBy('order', 'ASC')
171
                    ->with(
172
                        [
173
                            'rules'              => function (HasMany $query) {
174
                                $query->orderBy('active', 'DESC');
175
                                $query->orderBy('order', 'ASC');
176
                            },
177
                            'rules.ruleTriggers' => function (HasMany $query) {
178
                                $query->orderBy('order', 'ASC');
179
                            },
180
                            'rules.ruleActions'  => function (HasMany $query) {
181
                                $query->orderBy('order', 'ASC');
182
                            },
183
                        ]
184
                    )->get();
185
    }
186
187
    /**
188
     * @param RuleGroup $ruleGroup
189
     *
190
     * @return bool
191
     */
192
    public function moveDown(RuleGroup $ruleGroup): bool
193
    {
194
        $order = $ruleGroup->order;
195
196
        // find the rule with order+1 and give it order-1
197
        $other = $this->user->ruleGroups()->where('order', $order + 1)->first();
198
        if ($other) {
199
            --$other->order;
200
            $other->save();
201
        }
202
203
        ++$ruleGroup->order;
204
        $ruleGroup->save();
205
        $this->resetRuleGroupOrder();
206
207
        return true;
208
    }
209
210
    /**
211
     * @param RuleGroup $ruleGroup
212
     *
213
     * @return bool
214
     */
215
    public function moveUp(RuleGroup $ruleGroup): bool
216
    {
217
        $order = $ruleGroup->order;
218
219
        // find the rule with order-1 and give it order+1
220
        $other = $this->user->ruleGroups()->where('order', $order - 1)->first();
221
        if ($other) {
222
            ++$other->order;
223
            $other->save();
224
        }
225
226
        --$ruleGroup->order;
227
        $ruleGroup->save();
228
        $this->resetRuleGroupOrder();
229
230
        return true;
231
    }
232
233
    /**
234
     * @return bool
235
     */
236
    public function resetRuleGroupOrder(): bool
237
    {
238
        $this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]);
239
240
        $set   = $this->user->ruleGroups()->where('active', 1)->orderBy('order', 'ASC')->get();
241
        $count = 1;
242
        /** @var RuleGroup $entry */
243
        foreach ($set as $entry) {
244
            $entry->order = $count;
245
            $entry->save();
246
            ++$count;
247
        }
248
249
        return true;
250
    }
251
252
    /**
253
     * @param RuleGroup $ruleGroup
254
     *
255
     * @return bool
256
     */
257
    public function resetRulesInGroupOrder(RuleGroup $ruleGroup): bool
258
    {
259
        $ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);
260
261
        $set   = $ruleGroup->rules()
262
                           ->orderBy('order', 'ASC')
263
                           ->orderBy('updated_at', 'DESC')
264
                           ->get();
265
        $count = 1;
266
        /** @var Rule $entry */
267
        foreach ($set as $entry) {
268
            $entry->order = $count;
269
            $entry->save();
270
            ++$count;
271
        }
272
273
        return true;
274
    }
275
276
    /**
277
     * @param User $user
278
     */
279
    public function setUser(User $user): void
280
    {
281
        $this->user = $user;
282
    }
283
284
    /**
285
     * @param array $data
286
     *
287
     * @return RuleGroup
288
     */
289
    public function store(array $data): RuleGroup
290
    {
291
        $order = $this->getHighestOrderRuleGroup();
292
293
        $newRuleGroup = new RuleGroup(
294
            [
295
                'user_id'     => $this->user->id,
296
                'title'       => $data['title'],
297
                'description' => $data['description'],
298
                'order'       => $order + 1,
299
                'active'      => 1,
300
            ]
301
        );
302
        $newRuleGroup->save();
303
        $this->resetRuleGroupOrder();
304
305
        return $newRuleGroup;
306
    }
307
308
    /**
309
     * @param RuleGroup $ruleGroup
310
     * @param array     $data
311
     *
312
     * @return RuleGroup
313
     */
314
    public function update(RuleGroup $ruleGroup, array $data): RuleGroup
315
    {
316
        // update the account:
317
        $ruleGroup->title       = $data['title'];
318
        $ruleGroup->description = $data['description'];
319
        $ruleGroup->active      = $data['active'];
320
        $ruleGroup->save();
321
        $this->resetRuleGroupOrder();
322
323
        return $ruleGroup;
324
    }
325
}
326