1 | <?php |
||
2 | /** |
||
3 | * LinkTypeRepository.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\LinkType; |
||
24 | |||
25 | use FireflyIII\Exceptions\FireflyException; |
||
26 | use FireflyIII\Models\LinkType; |
||
27 | use FireflyIII\Models\Note; |
||
28 | use FireflyIII\Models\TransactionJournal; |
||
29 | use FireflyIII\Models\TransactionJournalLink; |
||
30 | use FireflyIII\User; |
||
31 | use Illuminate\Support\Collection; |
||
32 | use Log; |
||
33 | |||
34 | /** |
||
35 | * Class LinkTypeRepository. |
||
36 | */ |
||
37 | class LinkTypeRepository implements LinkTypeRepositoryInterface |
||
38 | { |
||
39 | /** @var User */ |
||
40 | private $user; |
||
41 | |||
42 | /** |
||
43 | * @param LinkType $linkType |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | public function countJournals(LinkType $linkType): int |
||
48 | { |
||
49 | return $linkType->transactionJournalLinks()->count(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param LinkType $linkType |
||
54 | * @param LinkType $moveTo |
||
55 | * |
||
56 | * @return bool |
||
57 | * |
||
58 | * @throws \Exception |
||
59 | */ |
||
60 | public function destroy(LinkType $linkType, LinkType $moveTo): bool |
||
61 | { |
||
62 | if (null !== $moveTo->id) { |
||
63 | TransactionJournalLink::where('link_type_id', $linkType->id)->update(['link_type_id' => $moveTo->id]); |
||
64 | } |
||
65 | $linkType->delete(); |
||
66 | |||
67 | return true; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param TransactionJournalLink $link |
||
72 | * |
||
73 | * @return bool |
||
74 | * |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function destroyLink(TransactionJournalLink $link): bool |
||
78 | { |
||
79 | $link->delete(); |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param int $id |
||
86 | * |
||
87 | * @return LinkType |
||
88 | */ |
||
89 | public function find(int $id): LinkType |
||
90 | { |
||
91 | $linkType = LinkType::find($id); |
||
92 | if (null === $linkType) { |
||
93 | return new LinkType; |
||
94 | } |
||
95 | |||
96 | return $linkType; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Check if link exists between journals. |
||
101 | * |
||
102 | * @param TransactionJournal $one |
||
103 | * @param TransactionJournal $two |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function findLink(TransactionJournal $one, TransactionJournal $two): bool |
||
108 | { |
||
109 | $count = TransactionJournalLink::whereDestinationId($one->id)->whereSourceId($two->id)->count(); |
||
110 | $opposingCount = TransactionJournalLink::whereDestinationId($two->id)->whereSourceId($one->id)->count(); |
||
111 | |||
112 | return $count + $opposingCount > 0; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return Collection |
||
117 | */ |
||
118 | public function get(): Collection |
||
119 | { |
||
120 | return LinkType::orderBy('name', 'ASC')->get(); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Return list of existing connections. |
||
125 | * |
||
126 | * @param TransactionJournal $journal |
||
127 | * |
||
128 | * @return Collection |
||
129 | */ |
||
130 | public function getLinks(TransactionJournal $journal): Collection |
||
131 | { |
||
132 | $outward = TransactionJournalLink::whereSourceId($journal->id)->get(); |
||
133 | $inward = TransactionJournalLink::whereDestinationId($journal->id)->get(); |
||
134 | $merged = $outward->merge($inward); |
||
135 | |||
136 | $filtered = $merged->filter( |
||
137 | function (TransactionJournalLink $link) { |
||
138 | return (!is_null($link->source) && !is_null($link->destination)); |
||
139 | } |
||
140 | ); |
||
141 | |||
142 | return $filtered; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param User $user |
||
147 | */ |
||
148 | public function setUser(User $user) |
||
149 | { |
||
150 | $this->user = $user; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param array $data |
||
155 | * |
||
156 | * @return LinkType |
||
157 | */ |
||
158 | public function store(array $data): LinkType |
||
159 | { |
||
160 | $linkType = new LinkType; |
||
161 | $linkType->name = $data['name']; |
||
162 | $linkType->inward = $data['inward']; |
||
163 | $linkType->outward = $data['outward']; |
||
164 | $linkType->editable = true; |
||
165 | $linkType->save(); |
||
166 | |||
167 | return $linkType; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Store link between two journals. |
||
172 | * |
||
173 | * @param array $information |
||
174 | * @param TransactionJournal $left |
||
175 | * @param TransactionJournal $right |
||
176 | * |
||
177 | * @return mixed |
||
178 | * @throws FireflyException |
||
179 | */ |
||
180 | public function storeLink(array $information, TransactionJournal $left, TransactionJournal $right): TransactionJournalLink |
||
181 | { |
||
182 | $linkType = $this->find(intval($information['link_type_id']) ?? 0); |
||
183 | if (is_null($linkType->id)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
184 | throw new FireflyException(sprintf('Link type #%d cannot be resolved to an actual link type', intval($information['link_type_id']) ?? 0)); |
||
185 | } |
||
186 | $link = new TransactionJournalLink; |
||
187 | $link->linkType()->associate($linkType); |
||
188 | if ('inward' === $information['direction']) { |
||
189 | Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->inward, $left->id, $right->id)); |
||
190 | $link->source()->associate($left); |
||
191 | $link->destination()->associate($right); |
||
192 | } |
||
193 | |||
194 | if ('outward' === $information['direction']) { |
||
195 | Log::debug(sprintf('Link type is inwards ("%s"), so %d is source and %d is destination.', $linkType->outward, $right->id, $left->id)); |
||
196 | $link->source()->associate($right); |
||
197 | $link->destination()->associate($left); |
||
198 | } |
||
199 | $link->save(); |
||
200 | |||
201 | // make note in noteable: |
||
202 | if (strlen($information['notes']) > 0) { |
||
203 | $dbNote = $link->notes()->first(); |
||
204 | if (null === $dbNote) { |
||
205 | $dbNote = new Note(); |
||
206 | $dbNote->noteable()->associate($link); |
||
207 | } |
||
208 | $dbNote->text = trim($information['notes']); |
||
209 | $dbNote->save(); |
||
210 | } |
||
211 | |||
212 | return $link; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param TransactionJournalLink $link |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function switchLink(TransactionJournalLink $link): bool |
||
221 | { |
||
222 | $source = $link->source_id; |
||
223 | $link->source_id = $link->destination_id; |
||
224 | $link->destination_id = $source; |
||
225 | $link->save(); |
||
226 | |||
227 | return true; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param LinkType $linkType |
||
232 | * @param array $data |
||
233 | * |
||
234 | * @return LinkType |
||
235 | */ |
||
236 | public function update(LinkType $linkType, array $data): LinkType |
||
237 | { |
||
238 | $linkType->name = $data['name']; |
||
239 | $linkType->inward = $data['inward']; |
||
240 | $linkType->outward = $data['outward']; |
||
241 | $linkType->save(); |
||
242 | |||
243 | return $linkType; |
||
244 | } |
||
245 | } |
||
246 |