1 | <?php |
||
2 | /** |
||
3 | * TransactionLinkController.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\Controllers; |
||
25 | |||
26 | use FireflyIII\Api\V1\Requests\TransactionLinkRequest; |
||
27 | use FireflyIII\Exceptions\FireflyException; |
||
28 | use FireflyIII\Models\TransactionJournalLink; |
||
29 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
30 | use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; |
||
31 | use FireflyIII\Support\Http\Api\TransactionFilter; |
||
32 | use FireflyIII\Transformers\JournalLinkTransformer; |
||
0 ignored issues
–
show
|
|||
33 | use FireflyIII\Transformers\TransactionLinkTransformer; |
||
34 | use FireflyIII\User; |
||
35 | use Illuminate\Http\JsonResponse; |
||
36 | use Illuminate\Http\Request; |
||
37 | use Illuminate\Pagination\LengthAwarePaginator; |
||
38 | use League\Fractal\Manager; |
||
39 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||
40 | use League\Fractal\Resource\Collection as FractalCollection; |
||
41 | use League\Fractal\Resource\Item; |
||
42 | use League\Fractal\Serializer\JsonApiSerializer; |
||
43 | |||
44 | /** |
||
45 | * Class TransactionLinkController |
||
46 | */ |
||
47 | class TransactionLinkController extends Controller |
||
48 | { |
||
49 | use TransactionFilter; |
||
50 | |||
51 | /** @var JournalRepositoryInterface The journal repository */ |
||
52 | private $journalRepository; |
||
53 | /** @var LinkTypeRepositoryInterface The link type repository */ |
||
54 | private $repository; |
||
55 | |||
56 | /** |
||
57 | * JournalLinkController constructor. |
||
58 | */ |
||
59 | public function __construct() |
||
60 | { |
||
61 | parent::__construct(); |
||
62 | $this->middleware( |
||
63 | function ($request, $next) { |
||
64 | /** @var User $user */ |
||
65 | $user = auth()->user(); |
||
66 | |||
67 | $this->repository = app(LinkTypeRepositoryInterface::class); |
||
68 | $this->journalRepository = app(JournalRepositoryInterface::class); |
||
69 | |||
70 | $this->repository->setUser($user); |
||
71 | $this->journalRepository->setUser($user); |
||
72 | |||
73 | return $next($request); |
||
74 | } |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Delete the resource. |
||
80 | * |
||
81 | * @param TransactionJournalLink $link |
||
82 | * |
||
83 | * @return JsonResponse |
||
84 | */ |
||
85 | public function delete(TransactionJournalLink $link): JsonResponse |
||
86 | { |
||
87 | $this->repository->destroyLink($link); |
||
88 | |||
89 | return response()->json([], 204); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * List all of them. |
||
94 | * |
||
95 | * @param Request $request |
||
96 | * |
||
97 | * @return JsonResponse] |
||
98 | */ |
||
99 | public function index(Request $request): JsonResponse |
||
100 | { |
||
101 | // create some objects: |
||
102 | $manager = new Manager; |
||
103 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
104 | |||
105 | // read type from URI |
||
106 | $name = $request->get('name') ?? null; |
||
107 | |||
108 | // types to get, page size: |
||
109 | $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
110 | $linkType = $this->repository->findByName($name); |
||
111 | |||
112 | // get list of transaction links. Count it and split it. |
||
113 | $collection = $this->repository->getJournalLinks($linkType); |
||
114 | $count = $collection->count(); |
||
115 | $journalLinks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
116 | |||
117 | // make paginator: |
||
118 | $paginator = new LengthAwarePaginator($journalLinks, $count, $pageSize, $this->parameters->get('page')); |
||
119 | $paginator->setPath(route('api.v1.transaction_links.index') . $this->buildParams()); |
||
120 | |||
121 | // present to user. |
||
122 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
123 | |||
124 | /** @var TransactionLinkTransformer $transformer */ |
||
125 | $transformer = app(TransactionLinkTransformer::class); |
||
126 | $transformer->setParameters($this->parameters); |
||
127 | |||
128 | $resource = new FractalCollection($journalLinks, $transformer, 'transaction_links'); |
||
129 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
130 | |||
131 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
132 | |||
133 | } |
||
134 | |||
135 | /** |
||
136 | * List single resource. |
||
137 | * |
||
138 | * @param Request $request |
||
139 | * @param TransactionJournalLink $journalLink |
||
140 | * |
||
141 | * @return JsonResponse |
||
142 | */ |
||
143 | public function show(Request $request, TransactionJournalLink $journalLink): JsonResponse |
||
144 | { |
||
145 | $manager = new Manager; |
||
146 | $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; |
||
147 | $manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
148 | |||
149 | /** @var TransactionLinkTransformer $transformer */ |
||
150 | $transformer = app(TransactionLinkTransformer::class); |
||
151 | $transformer->setParameters($this->parameters); |
||
152 | |||
153 | $resource = new Item($journalLink, $transformer, 'transaction_links'); |
||
154 | |||
155 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
156 | |||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Store new object. |
||
161 | * |
||
162 | * @param TransactionLinkRequest $request |
||
163 | * |
||
164 | * @return JsonResponse |
||
165 | * @throws FireflyException |
||
166 | */ |
||
167 | public function store(TransactionLinkRequest $request): JsonResponse |
||
168 | { |
||
169 | $manager = new Manager; |
||
170 | $data = $request->getAll(); |
||
171 | $inward = $this->journalRepository->findNull($data['inward_id'] ?? 0); |
||
172 | $outward = $this->journalRepository->findNull($data['outward_id'] ?? 0); |
||
173 | if (null === $inward || null === $outward) { |
||
174 | throw new FireflyException('Source or destination is NULL.'); |
||
175 | } |
||
176 | $data['direction'] = 'inward'; |
||
177 | |||
178 | $journalLink = $this->repository->storeLink($data, $inward, $outward); |
||
179 | |||
180 | /** @var TransactionLinkTransformer $transformer */ |
||
181 | $transformer = app(TransactionLinkTransformer::class); |
||
182 | $transformer->setParameters($this->parameters); |
||
183 | |||
184 | $resource = new Item($journalLink, $transformer, 'transaction_links'); |
||
185 | |||
186 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Update object. |
||
192 | * |
||
193 | * @param TransactionLinkRequest $request |
||
194 | * @param TransactionJournalLink $journalLink |
||
195 | * |
||
196 | * @return JsonResponse |
||
197 | * @throws FireflyException |
||
198 | */ |
||
199 | public function update(TransactionLinkRequest $request, TransactionJournalLink $journalLink): JsonResponse |
||
200 | { |
||
201 | $manager = new Manager; |
||
202 | $data = $request->getAll(); |
||
203 | $data['inward'] = $this->journalRepository->findNull($data['inward_id'] ?? 0); |
||
204 | $data['outward'] = $this->journalRepository->findNull($data['outward_id'] ?? 0); |
||
205 | if (null === $data['inward'] || null === $data['outward']) { |
||
206 | throw new FireflyException('Source or destination is NULL.'); |
||
207 | } |
||
208 | $data['direction'] = 'inward'; |
||
209 | $journalLink = $this->repository->updateLink($journalLink, $data); |
||
210 | |||
211 | /** @var TransactionLinkTransformer $transformer */ |
||
212 | $transformer = app(TransactionLinkTransformer::class); |
||
213 | $transformer->setParameters($this->parameters); |
||
214 | |||
215 | $resource = new Item($journalLink, $transformer, 'transaction_links'); |
||
216 | |||
217 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
218 | |||
219 | } |
||
220 | } |
||
221 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths