Checks if the types of the passed arguments in a function/method call are compatible.
1 | <?php |
||
2 | /** |
||
3 | * BudgetLimitController.php |
||
4 | * Copyright (c) 2019 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program 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 Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Api\V1\Controllers; |
||
25 | |||
26 | |||
27 | use FireflyIII\Api\V1\Requests\BudgetLimitRequest; |
||
28 | use FireflyIII\Exceptions\FireflyException; |
||
29 | use FireflyIII\Helpers\Collector\GroupCollectorInterface; |
||
30 | use FireflyIII\Models\BudgetLimit; |
||
31 | use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; |
||
32 | use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
||
33 | use FireflyIII\Support\Http\Api\TransactionFilter; |
||
34 | use FireflyIII\Transformers\BudgetLimitTransformer; |
||
35 | use FireflyIII\Transformers\TransactionGroupTransformer; |
||
36 | use FireflyIII\User; |
||
37 | use Illuminate\Http\JsonResponse; |
||
38 | use Illuminate\Http\Request; |
||
39 | use Illuminate\Pagination\LengthAwarePaginator; |
||
40 | use Illuminate\Support\Collection; |
||
41 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||
42 | use League\Fractal\Resource\Collection as FractalCollection; |
||
43 | use League\Fractal\Resource\Item; |
||
44 | |||
45 | /** |
||
46 | * Class BudgetLimitController. |
||
47 | * |
||
48 | */ |
||
49 | class BudgetLimitController extends Controller |
||
50 | { |
||
51 | use TransactionFilter; |
||
52 | /** @var BudgetLimitRepositoryInterface */ |
||
53 | private $blRepository; |
||
54 | /** @var BudgetRepositoryInterface The budget repository */ |
||
55 | private $repository; |
||
56 | |||
57 | /** |
||
58 | * BudgetLimitController constructor. |
||
59 | * |
||
60 | * @codeCoverageIgnore |
||
61 | */ |
||
62 | public function __construct() |
||
63 | { |
||
64 | parent::__construct(); |
||
65 | $this->middleware( |
||
66 | function ($request, $next) { |
||
67 | /** @var User $user */ |
||
68 | $user = auth()->user(); |
||
69 | $this->repository = app(BudgetRepositoryInterface::class); |
||
70 | $this->blRepository = app(BudgetLimitRepositoryInterface::class); |
||
71 | $this->repository->setUser($user); |
||
72 | $this->blRepository->setUser($user); |
||
73 | |||
74 | return $next($request); |
||
75 | } |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Remove the specified resource from storage. |
||
81 | * |
||
82 | * @param BudgetLimit $budgetLimit |
||
83 | * |
||
84 | * @return JsonResponse |
||
85 | * @codeCoverageIgnore |
||
86 | */ |
||
87 | public function delete(BudgetLimit $budgetLimit): JsonResponse |
||
88 | { |
||
89 | $this->blRepository->destroyBudgetLimit($budgetLimit); |
||
90 | |||
91 | return response()->json([], 204); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Display a listing of the resource. |
||
96 | * |
||
97 | * @param Request $request |
||
98 | * |
||
99 | * @return JsonResponse |
||
100 | * @codeCoverageIgnore |
||
101 | */ |
||
102 | public function index(Request $request): JsonResponse |
||
103 | { |
||
104 | $manager = $this->getManager(); |
||
105 | $budgetId = (int) ($request->get('budget_id') ?? 0); |
||
106 | $budget = $this->repository->findNull($budgetId); |
||
107 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
108 | $this->parameters->set('budget_id', $budgetId); |
||
109 | |||
110 | $collection = new Collection; |
||
111 | if (null === $budget) { |
||
112 | $collection = $this->blRepository->getAllBudgetLimits($this->parameters->get('start'), $this->parameters->get('end')); |
||
113 | } |
||
114 | if (null !== $budget) { |
||
115 | $collection = $this->blRepository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end')); |
||
116 | } |
||
117 | |||
118 | $count = $collection->count(); |
||
119 | $budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); |
||
120 | $paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page')); |
||
121 | $paginator->setPath(route('api.v1.budget_limits.index') . $this->buildParams()); |
||
122 | |||
123 | /** @var BudgetLimitTransformer $transformer */ |
||
124 | $transformer = app(BudgetLimitTransformer::class); |
||
125 | $transformer->setParameters($this->parameters); |
||
126 | |||
127 | $resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits'); |
||
128 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
129 | |||
130 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Display the specified resource. |
||
135 | * |
||
136 | * @param BudgetLimit $budgetLimit |
||
137 | * |
||
138 | * @return JsonResponse |
||
139 | * @codeCoverageIgnore |
||
140 | */ |
||
141 | public function show(BudgetLimit $budgetLimit): JsonResponse |
||
142 | { |
||
143 | $manager = $this->getManager(); |
||
144 | |||
145 | /** @var BudgetLimitTransformer $transformer */ |
||
146 | $transformer = app(BudgetLimitTransformer::class); |
||
147 | $transformer->setParameters($this->parameters); |
||
148 | |||
149 | $resource = new Item($budgetLimit, $transformer, 'budget_limits'); |
||
150 | |||
151 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Store a newly created resource in storage. |
||
156 | * |
||
157 | * @param BudgetLimitRequest $request |
||
158 | * |
||
159 | * @throws FireflyException |
||
160 | * |
||
161 | * @return JsonResponse |
||
162 | */ |
||
163 | public function store(BudgetLimitRequest $request): JsonResponse |
||
164 | { |
||
165 | $data = $request->getAll(); |
||
166 | $budget = $this->repository->findNull($data['budget_id']); |
||
167 | if (null === $budget) { |
||
168 | throw new FireflyException('200004: Budget does not exist.'); // @codeCoverageIgnore |
||
169 | } |
||
170 | $data['budget'] = $budget; |
||
171 | $budgetLimit = $this->blRepository->storeBudgetLimit($data); |
||
172 | $manager = $this->getManager(); |
||
173 | |||
174 | /** @var BudgetLimitTransformer $transformer */ |
||
175 | $transformer = app(BudgetLimitTransformer::class); |
||
176 | $transformer->setParameters($this->parameters); |
||
177 | |||
178 | $resource = new Item($budgetLimit, $transformer, 'budget_limits'); |
||
179 | |||
180 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Show all transactions. |
||
185 | * |
||
186 | * @param Request $request |
||
187 | * @param BudgetLimit $budgetLimit |
||
188 | * |
||
189 | * @return JsonResponse |
||
190 | * @codeCoverageIgnore |
||
191 | */ |
||
192 | public function transactions(Request $request, BudgetLimit $budgetLimit): JsonResponse |
||
193 | { |
||
194 | $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; |
||
195 | $type = $request->get('type') ?? 'default'; |
||
196 | $this->parameters->set('type', $type); |
||
197 | |||
198 | $types = $this->mapTransactionTypes($this->parameters->get('type')); |
||
199 | $manager = $this->getManager(); |
||
200 | |||
201 | /** @var User $admin */ |
||
202 | $admin = auth()->user(); |
||
203 | |||
204 | // use new group collector: |
||
205 | /** @var GroupCollectorInterface $collector */ |
||
206 | $collector = app(GroupCollectorInterface::class); |
||
207 | $collector |
||
208 | ->setUser($admin) |
||
209 | // filter on budget. |
||
210 | ->setBudget($budgetLimit->budget) |
||
211 | // all info needed for the API: |
||
212 | ->withAPIInformation() |
||
213 | // set page size: |
||
214 | ->setLimit($pageSize) |
||
215 | // set page to retrieve |
||
216 | ->setPage($this->parameters->get('page')) |
||
217 | // set types of transactions to return. |
||
218 | ->setTypes($types); |
||
219 | |||
220 | $collector->setRange($budgetLimit->start_date, $budgetLimit->end_date); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
221 | $collector->setTypes($types); |
||
222 | $paginator = $collector->getPaginatedGroups(); |
||
223 | $paginator->setPath(route('api.v1.budget_limits.transactions', [$budgetLimit->id]) . $this->buildParams()); |
||
224 | $transactions = $paginator->getCollection(); |
||
225 | |||
226 | /** @var TransactionGroupTransformer $transformer */ |
||
227 | $transformer = app(TransactionGroupTransformer::class); |
||
228 | $transformer->setParameters($this->parameters); |
||
229 | |||
230 | $resource = new FractalCollection($transactions, $transformer, 'transactions'); |
||
231 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||
232 | |||
233 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Update the specified resource in storage. |
||
238 | * |
||
239 | * @param BudgetLimitRequest $request |
||
240 | * @param BudgetLimit $budgetLimit |
||
241 | * |
||
242 | * @return JsonResponse |
||
243 | */ |
||
244 | public function update(BudgetLimitRequest $request, BudgetLimit $budgetLimit): JsonResponse |
||
245 | { |
||
246 | $data = $request->getAll(); |
||
247 | $data['budget'] = $budgetLimit->budget; |
||
248 | $budgetLimit = $this->blRepository->updateBudgetLimit($budgetLimit, $data); |
||
249 | $manager = $this->getManager(); |
||
250 | |||
251 | /** @var BudgetLimitTransformer $transformer */ |
||
252 | $transformer = app(BudgetLimitTransformer::class); |
||
253 | $transformer->setParameters($this->parameters); |
||
254 | |||
255 | $resource = new Item($budgetLimit, $transformer, 'budget_limits'); |
||
256 | |||
257 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||
258 | |||
259 | } |
||
260 | } |
||
261 |