1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * RuleGroupTestRequest.php |
||||
5 | * Copyright (c) 2019 [email protected] |
||||
6 | * |
||||
7 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||||
8 | * |
||||
9 | * This program is free software: you can redistribute it and/or modify |
||||
10 | * it under the terms of the GNU Affero General Public License as |
||||
11 | * published by the Free Software Foundation, either version 3 of the |
||||
12 | * License, or (at your option) any later version. |
||||
13 | * |
||||
14 | * This program is distributed in the hope that it will be useful, |
||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
17 | * GNU Affero General Public License for more details. |
||||
18 | * |
||||
19 | * You should have received a copy of the GNU Affero General Public License |
||||
20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
21 | */ |
||||
22 | |||||
23 | declare(strict_types=1); |
||||
24 | |||||
25 | |||||
26 | namespace FireflyIII\Api\V1\Requests; |
||||
27 | |||||
28 | |||||
29 | use Carbon\Carbon; |
||||
30 | use FireflyIII\Models\Account; |
||||
31 | use FireflyIII\Models\AccountType; |
||||
32 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||||
33 | use Illuminate\Support\Collection; |
||||
34 | use Log; |
||||
35 | |||||
36 | /** |
||||
37 | * Class RuleGroupTestRequest |
||||
38 | */ |
||||
39 | class RuleGroupTestRequest extends Request |
||||
40 | { |
||||
41 | /** |
||||
42 | * Authorize logged in users. |
||||
43 | * |
||||
44 | * @return bool |
||||
45 | */ |
||||
46 | public function authorize(): bool |
||||
47 | { |
||||
48 | // Only allow authenticated users |
||||
49 | return auth()->check(); |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * @return array |
||||
54 | */ |
||||
55 | public function getTestParameters(): array |
||||
56 | { |
||||
57 | return [ |
||||
58 | 'page' => $this->getPage(), |
||||
59 | 'start_date' => $this->getDate('start_date'), |
||||
60 | 'end_date' => $this->getDate('end_date'), |
||||
61 | 'search_limit' => $this->getSearchLimit(), |
||||
62 | 'trigger_limit' => $this->getTriggerLimit(), |
||||
63 | 'accounts' => $this->getAccounts(), |
||||
64 | ]; |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @return array |
||||
69 | */ |
||||
70 | public function rules(): array |
||||
71 | { |
||||
72 | return []; |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * @return Collection |
||||
77 | */ |
||||
78 | private function getAccounts(): Collection |
||||
79 | { |
||||
80 | $accountList = '' === (string) $this->query('accounts') ? [] : explode(',', $this->query('accounts')); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
81 | $accounts = new Collection; |
||||
82 | |||||
83 | /** @var AccountRepositoryInterface $accountRepository */ |
||||
84 | $accountRepository = app(AccountRepositoryInterface::class); |
||||
85 | |||||
86 | foreach ($accountList as $accountId) { |
||||
87 | Log::debug(sprintf('Searching for asset account with id "%s"', $accountId)); |
||||
88 | $account = $accountRepository->findNull((int) $accountId); |
||||
89 | if ($this->validAccount($account)) { |
||||
90 | /** @noinspection NullPointerExceptionInspection */ |
||||
91 | Log::debug(sprintf('Found account #%d ("%s") and its an asset account', $account->id, $account->name)); |
||||
92 | $accounts->push($account); |
||||
93 | } |
||||
94 | } |
||||
95 | |||||
96 | return $accounts; |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * @param string $field |
||||
101 | * |
||||
102 | * @return Carbon|null |
||||
103 | */ |
||||
104 | private function getDate(string $field): ?Carbon |
||||
105 | { |
||||
106 | /** @var Carbon $result */ |
||||
107 | $result = null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', $this->query($field)); |
||||
0 ignored issues
–
show
It seems like
$this->query($field) can also be of type array ; however, parameter $time of Carbon\Carbon::createFromFormat() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
108 | |||||
109 | return $result; |
||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * @return int |
||||
114 | */ |
||||
115 | private function getPage(): int |
||||
116 | { |
||||
117 | return 0 === (int) $this->query('page') ? 1 : (int) $this->query('page'); |
||||
118 | |||||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * @return int |
||||
123 | */ |
||||
124 | private function getSearchLimit(): int |
||||
125 | { |
||||
126 | return 0 === (int) $this->query('search_limit') ? (int) config('firefly.test-triggers.limit') : (int) $this->query('search_limit'); |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @return int |
||||
131 | */ |
||||
132 | private function getTriggerLimit(): int |
||||
133 | { |
||||
134 | return 0 === (int) $this->query('triggered_limit') ? (int) config('firefly.test-triggers.range') : (int) $this->query('triggered_limit'); |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * @param Account|null $account |
||||
139 | * |
||||
140 | * @return bool |
||||
141 | */ |
||||
142 | private function validAccount(?Account $account): bool |
||||
143 | { |
||||
144 | return null !== $account && AccountType::ASSET === $account->accountType->type; |
||||
145 | } |
||||
146 | |||||
147 | } |
||||
148 |