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 ( 7cfa91...64a2f2 )
by James
31:27 queued 21:57
created

TransferController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 40
c 1
b 0
f 0
dl 0
loc 74
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A search() 0 66 2
1
<?php
2
/**
3
 * TransferController.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
namespace FireflyIII\Api\V1\Controllers\Search;
23
24
25
use FireflyIII\Api\V1\Controllers\Controller;
26
use FireflyIII\Api\V1\Requests\Search\TransferRequest;
27
use FireflyIII\Exceptions\FireflyException;
28
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
29
use FireflyIII\Support\Search\TransferSearch;
30
use FireflyIII\Transformers\TransactionGroupTransformer;
31
use FireflyIII\User;
32
use Illuminate\Http\JsonResponse;
33
use Illuminate\Http\Request;
34
use Illuminate\Http\Response;
35
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
36
use League\Fractal\Resource\Collection as FractalCollection;
37
38
/**
39
 * Class TransferController
40
 */
41
class TransferController extends Controller
42
{
43
    /**
44
     * @param Request $request
45
     *
46
     * @return JsonResponse|Response
47
     * @throws FireflyException
48
     */
49
    public function search(TransferRequest $request)
50
    {
51
        // configure transfer search to search for a > b
52
        $search = app(TransferSearch::class);
53
        $search->setSource($request->get('source'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('source') can also be of type null; however, parameter $source of FireflyIII\Support\Searc...sferSearch::setSource() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

53
        $search->setSource(/** @scrutinizer ignore-type */ $request->get('source'));
Loading history...
54
        $search->setDestination($request->get('destination'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('destination') can also be of type null; however, parameter $destination of FireflyIII\Support\Searc...earch::setDestination() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
        $search->setDestination(/** @scrutinizer ignore-type */ $request->get('destination'));
Loading history...
55
        $search->setAmount($request->get('amount'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('amount') can also be of type null; however, parameter $amount of FireflyIII\Support\Searc...sferSearch::setAmount() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $search->setAmount(/** @scrutinizer ignore-type */ $request->get('amount'));
Loading history...
56
        $search->setDescription($request->get('description'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('description') can also be of type null; however, parameter $description of FireflyIII\Support\Searc...earch::setDescription() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $search->setDescription(/** @scrutinizer ignore-type */ $request->get('description'));
Loading history...
57
        $search->setDate($request->get('date'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('date') can also be of type null; however, parameter $date of FireflyIII\Support\Searc...ansferSearch::setDate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

57
        $search->setDate(/** @scrutinizer ignore-type */ $request->get('date'));
Loading history...
58
59
        $left = $search->search();
60
61
        // configure transfer search to search for b > a
62
        $search->setSource($request->get('destination'));
63
        $search->setDestination($request->get('source'));
64
        $search->setAmount($request->get('amount'));
65
        $search->setDescription($request->get('description'));
66
        $search->setDate($request->get('date'));
67
68
        $right = $search->search();
69
70
        // add parameters to URL:
71
        $this->parameters->set('source', $request->get('source'));
72
        $this->parameters->set('destination', $request->get('destination'));
73
        $this->parameters->set('amount', $request->get('amount'));
74
        $this->parameters->set('description', $request->get('description'));
75
        $this->parameters->set('date', $request->get('date'));
76
77
        // get all journal ID's.
78
        $total = $left->merge($right)->unique('id')->pluck('id')->toArray();
79
        if (0 === count($total)) {
80
            // forces search to be empty.
81
            $total = [-1];
82
        }
83
84
        // collector to return results.
85
        $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
86
        $manager  = $this->getManager();
87
        /** @var User $admin */
88
        $admin = auth()->user();
89
90
        // use new group collector:
91
        /** @var GroupCollectorInterface $collector */
92
        $collector = app(GroupCollectorInterface::class);
93
        $collector
94
            ->setUser($admin)
95
            // all info needed for the API:
96
            ->withAPIInformation()
97
            // set page size:
98
            ->setLimit($pageSize)
99
            // set page to retrieve
100
            ->setPage(1)
101
            ->setJournalIds($total);
102
103
        $paginator = $collector->getPaginatedGroups();
104
        $paginator->setPath(route('api.v1.search.transfers') . $this->buildParams());
105
        $transactions = $paginator->getCollection();
106
107
        /** @var TransactionGroupTransformer $transformer */
108
        $transformer = app(TransactionGroupTransformer::class);
109
        $transformer->setParameters($this->parameters);
110
111
        $resource = new FractalCollection($transactions, $transformer, 'transactions');
112
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
113
114
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
115
    }
116
}