GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Api/V1/Controllers/TransactionController.php (4 issues)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
/**
4
 * TransactionController.php
5
 * Copyright (c) 2018 [email protected]
6
 *
7
 * This file is part of Firefly III.
8
 *
9
 * Firefly III is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Firefly III 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 General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
24
namespace FireflyIII\Api\V1\Controllers;
25
26
use FireflyIII\Api\V1\Requests\TransactionRequest;
27
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
28
use FireflyIII\Helpers\Filter\InternalTransferFilter;
29
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
30
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
31
use FireflyIII\Models\Transaction;
32
use FireflyIII\Models\TransactionType;
33
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
34
use FireflyIII\Transformers\TransactionTransformer;
35
use Illuminate\Http\Request;
36
use Illuminate\Support\Collection;
37
use League\Fractal\Manager;
38
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
39
use League\Fractal\Resource\Collection as FractalCollection;
40
use League\Fractal\Serializer\JsonApiSerializer;
41
use Log;
42
use Preferences;
43
44
/**
45
 * Class TransactionController
46
 */
47
class TransactionController extends Controller
48
{
49
50
    /** @var JournalRepositoryInterface */
51
    private $repository;
52
53
    /**
54
     * TransactionController constructor.
55
     *
56
     * @throws \FireflyIII\Exceptions\FireflyException
57
     */
58
    public function __construct()
59
    {
60
        parent::__construct();
61
        $this->middleware(
62
            function ($request, $next) {
63
                /** @var JournalRepositoryInterface repository */
64
                $this->repository = app(JournalRepositoryInterface::class);
65
                $this->repository->setUser(auth()->user());
66
67
                return $next($request);
68
            }
69
        );
70
    }
71
72
    /**
73
     * Remove the specified resource from storage.
74
     *
75
     * @param  \FireflyIII\Models\Transaction $transaction
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function delete(Transaction $transaction)
80
    {
81
        $journal = $transaction->transactionJournal;
82
        $this->repository->destroy($journal);
83
84
        return response()->json([], 204);
85
    }
86
87
    /**
88
     * @param Request $request
89
     *
90
     * @return \Illuminate\Http\JsonResponse
91
     */
92
    public function index(Request $request)
93
    {
94
        $pageSize = (int)Preferences::getForUser(auth()->user(), 'listPageSize', 50)->data;
0 ignored issues
show
The method getForUser() does not exist on FireflyIII\Support\Facades\Preferences. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $pageSize = (int)Preferences::/** @scrutinizer ignore-call */ getForUser(auth()->user(), 'listPageSize', 50)->data;
Loading history...
95
96
        // read type from URI
97
        $type = $request->get('type') ?? 'default';
98
        $this->parameters->set('type', $type);
99
100
        // types to get, page size:
101
        $types = $this->mapTypes($this->parameters->get('type'));
102
103
        $manager = new Manager();
104
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
105
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
106
107
        // collect transactions using the journal collector
108
        $collector = app(JournalCollectorInterface::class);
109
        $collector->setUser(auth()->user());
110
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
111
        $collector->setAllAssetAccounts();
112
113
        // remove internal transfer filter:
114
        if (in_array(TransactionType::TRANSFER, $types)) {
115
            $collector->removeFilter(InternalTransferFilter::class);
116
        }
117
118
        if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
119
            $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
120
        }
121
        $collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
122
        $collector->setTypes($types);
123
        $paginator = $collector->getPaginatedJournals();
124
        $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
125
        $transactions = $paginator->getCollection();
126
127
128
        $resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
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
     * @param Request     $request
137
     * @param Transaction $transaction
138
     *
139
     * @return \Illuminate\Http\JsonResponse
140
     */
141
    public function show(Request $request, Transaction $transaction)
142
    {
143
        $manager = new Manager();
144
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
145
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
146
147
        // add include parameter:
148
        $include = $request->get('include') ?? '';
149
        $manager->parseIncludes($include);
150
151
        // collect transactions using the journal collector
152
        $collector = app(JournalCollectorInterface::class);
153
        $collector->setUser(auth()->user());
154
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
155
        // filter on specific journals.
156
        $collector->setJournals(new Collection([$transaction->transactionJournal]));
157
158
        // add filter to remove transactions:
159
        $transactionType = $transaction->transactionJournal->transactionType->type;
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\TransactionType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
160
        if ($transactionType === TransactionType::WITHDRAWAL) {
161
            $collector->addFilter(PositiveAmountFilter::class);
162
        }
163
        if (!($transactionType === TransactionType::WITHDRAWAL)) {
164
            $collector->addFilter(NegativeAmountFilter::class);
165
        }
166
167
        $transactions = $collector->getJournals();
168
        $resource     = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
169
170
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
171
    }
172
173
    /**
174
     * @param TransactionRequest         $request
175
     *
176
     * @param JournalRepositoryInterface $repository
177
     *
178
     * @return \Illuminate\Http\JsonResponse
179
     */
180
    public function store(TransactionRequest $request, JournalRepositoryInterface $repository)
181
    {
182
        $data         = $request->getAll();
183
        $data['user'] = auth()->user()->id;
184
        $journal      = $repository->store($data);
185
186
        $manager = new Manager();
187
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
188
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
189
190
        // add include parameter:
191
        $include = $request->get('include') ?? '';
192
        $manager->parseIncludes($include);
193
194
        // collect transactions using the journal collector
195
        $collector = app(JournalCollectorInterface::class);
196
        $collector->setUser(auth()->user());
197
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
198
        // filter on specific journals.
199
        $collector->setJournals(new Collection([$journal]));
200
201
        // add filter to remove transactions:
202
        $transactionType = $journal->transactionType->type;
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\TransactionType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
203
        if ($transactionType === TransactionType::WITHDRAWAL) {
204
            $collector->addFilter(PositiveAmountFilter::class);
205
        }
206
        if (!($transactionType === TransactionType::WITHDRAWAL)) {
207
            $collector->addFilter(NegativeAmountFilter::class);
208
        }
209
210
        $transactions = $collector->getJournals();
211
        $resource     = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
212
213
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
214
    }
215
216
217
    /**
218
     * @param TransactionRequest         $request
219
     * @param JournalRepositoryInterface $repository
220
     * @param Transaction                $transaction
221
     *
222
     * @return \Illuminate\Http\JsonResponse
223
     */
224
    public function update(TransactionRequest $request, JournalRepositoryInterface $repository, Transaction $transaction)
225
    {
226
        $data         = $request->getAll();
227
        $data['user'] = auth()->user()->id;
228
229
        Log::debug('Inside transaction update');
230
231
        $journal = $repository->update($transaction->transactionJournal, $data);
232
233
        $manager = new Manager();
234
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
235
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
236
237
        // add include parameter:
238
        $include = $request->get('include') ?? '';
239
        $manager->parseIncludes($include);
240
241
        // needs a lot of extra data to match the journal collector. Or just expand that one.
242
        // collect transactions using the journal collector
243
        $collector = app(JournalCollectorInterface::class);
244
        $collector->setUser(auth()->user());
245
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
246
        // filter on specific journals.
247
        $collector->setJournals(new Collection([$journal]));
248
249
        // add filter to remove transactions:
250
        $transactionType = $journal->transactionType->type;
0 ignored issues
show
The property type does not seem to exist on FireflyIII\Models\TransactionType. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
251
        if ($transactionType === TransactionType::WITHDRAWAL) {
252
            $collector->addFilter(PositiveAmountFilter::class);
253
        }
254
        if (!($transactionType === TransactionType::WITHDRAWAL)) {
255
            $collector->addFilter(NegativeAmountFilter::class);
256
        }
257
258
        $transactions = $collector->getJournals();
259
        $resource     = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
260
261
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
262
263
    }
264
265
    /**
266
     * @param string $type
267
     *
268
     * @return array
269
     */
270
    private function mapTypes(string $type): array
271
    {
272
        $types = [
273
            'all'             => [
274
                TransactionType::WITHDRAWAL,
275
                TransactionType::DEPOSIT,
276
                TransactionType::TRANSFER,
277
                TransactionType::OPENING_BALANCE,
278
                TransactionType::RECONCILIATION,
279
            ],
280
            'withdrawal'      => [
281
                TransactionType::WITHDRAWAL,
282
            ],
283
            'withdrawals'     => [
284
                TransactionType::WITHDRAWAL,
285
            ],
286
            'expense'         => [
287
                TransactionType::WITHDRAWAL,
288
            ],
289
            'income'          => [
290
                TransactionType::DEPOSIT,
291
            ],
292
            'deposit'         => [
293
                TransactionType::DEPOSIT,
294
            ],
295
            'deposits'        => [
296
                TransactionType::DEPOSIT,
297
            ],
298
            'transfer'        => [
299
                TransactionType::TRANSFER,
300
            ],
301
            'transfers'       => [
302
                TransactionType::TRANSFER,
303
            ],
304
            'opening_balance' => [
305
                TransactionType::OPENING_BALANCE,
306
            ],
307
            'reconciliation'  => [
308
                TransactionType::RECONCILIATION,
309
            ],
310
            'reconciliations' => [
311
                TransactionType::RECONCILIATION,
312
            ],
313
            'special'         => [
314
                TransactionType::OPENING_BALANCE,
315
                TransactionType::RECONCILIATION,
316
            ],
317
            'specials'        => [
318
                TransactionType::OPENING_BALANCE,
319
                TransactionType::RECONCILIATION,
320
            ],
321
            'default'         => [
322
                TransactionType::WITHDRAWAL,
323
                TransactionType::DEPOSIT,
324
                TransactionType::TRANSFER,
325
            ],
326
        ];
327
        if (isset($types[$type])) {
328
            return $types[$type];
329
        }
330
331
        return $types['default']; // @codeCoverageIgnore
332
333
    }
334
}
335