1 | <?php |
||
2 | /** |
||
3 | * TransactionLinkRequest.php |
||
4 | * Copyright (c) 2018 [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 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Api\V1\Requests; |
||
25 | |||
26 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
27 | use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; |
||
28 | use FireflyIII\User; |
||
29 | use Illuminate\Validation\Validator; |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * Class TransactionLinkRequest |
||
34 | */ |
||
35 | class TransactionLinkRequest extends Request |
||
36 | { |
||
37 | /** |
||
38 | * Authorize logged in users. |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | public function authorize(): bool |
||
43 | { |
||
44 | // Only allow authenticated users |
||
45 | return auth()->check(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get all data from the request. |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | public function getAll(): array |
||
54 | { |
||
55 | return [ |
||
56 | 'link_type_id' => $this->integer('link_type_id'), |
||
57 | 'link_type_name' => $this->string('link_type_name'), |
||
58 | 'inward_id' => $this->integer('inward_id'), |
||
59 | 'outward_id' => $this->integer('outward_id'), |
||
60 | 'notes' => $this->string('notes'), |
||
61 | ]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * The rules that the incoming request must be matched against. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function rules(): array |
||
71 | { |
||
72 | return [ |
||
73 | 'link_type_id' => 'exists:link_types,id|required_without:link_type_name', |
||
74 | 'link_type_name' => 'exists:link_types,name|required_without:link_type_id', |
||
75 | 'inward_id' => 'required|belongsToUser:transaction_journals,id|different:outward_id', |
||
76 | 'outward_id' => 'required|belongsToUser:transaction_journals,id|different:inward_id', |
||
77 | 'notes' => 'between:0,65000', |
||
78 | ]; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Configure the validator instance. |
||
83 | * |
||
84 | * @param Validator $validator |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function withValidator(Validator $validator): void |
||
89 | { |
||
90 | $validator->after( |
||
91 | function (Validator $validator) { |
||
92 | $this->validateExistingLink($validator); |
||
93 | } |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param Validator $validator |
||
99 | */ |
||
100 | private function validateExistingLink(Validator $validator): void |
||
101 | { |
||
102 | /** @var User $user */ |
||
103 | $user = auth()->user(); |
||
104 | /** @var LinkTypeRepositoryInterface $repository */ |
||
105 | $repository = app(LinkTypeRepositoryInterface::class); |
||
106 | $repository->setUser($user); |
||
107 | |||
108 | /** @var JournalRepositoryInterface $journalRepos */ |
||
109 | $journalRepos = app(JournalRepositoryInterface::class); |
||
110 | $journalRepos->setUser($user); |
||
111 | |||
112 | $data = $validator->getData(); |
||
113 | $inwardId = (int)($data['inward_id'] ?? 0); |
||
114 | $outwardId = (int)($data['outward_id'] ?? 0); |
||
115 | $inward = $journalRepos->findNull($inwardId); |
||
116 | $outward = $journalRepos->findNull($outwardId); |
||
117 | |||
118 | if (null === $inward) { |
||
119 | $validator->errors()->add('inward_id', 'Invalid inward ID.'); |
||
120 | |||
121 | return; |
||
122 | } |
||
123 | if (null === $outward) { |
||
124 | $validator->errors()->add('outward_id', 'Invalid outward ID.'); |
||
125 | |||
126 | return; |
||
127 | } |
||
128 | |||
129 | if ($repository->findLink($inward, $outward)) { |
||
130 | // only if not updating: |
||
131 | $link = $this->route()->parameter('journalLink'); |
||
132 | if (null === $link) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
133 | $validator->errors()->add('outward_id', 'Already have a link between inward and outward.'); |
||
134 | $validator->errors()->add('inward_id', 'Already have a link between inward and outward.'); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 |