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 ( 4d9763...76aa8a )
by James
23:28 queued 10:43
created

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

Labels
Severity
1
<?php
2
/**
3
 * TagController.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 Carbon\Carbon;
27
use FireflyIII\Api\V1\Requests\TagRequest;
28
use FireflyIII\Exceptions\FireflyException;
29
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
30
use FireflyIII\Helpers\Filter\InternalTransferFilter;
31
use FireflyIII\Models\Tag;
32
use FireflyIII\Models\TransactionType;
33
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
34
use FireflyIII\Support\Http\Api\TransactionFilter;
35
use FireflyIII\Transformers\TagTransformer;
36
use FireflyIII\Transformers\TransactionTransformer;
37
use FireflyIII\User;
38
use Illuminate\Http\JsonResponse;
39
use Illuminate\Http\Request;
40
use Illuminate\Pagination\LengthAwarePaginator;
41
use League\Fractal\Manager;
42
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
43
use League\Fractal\Resource\Collection as FractalCollection;
44
use League\Fractal\Resource\Item;
45
use League\Fractal\Serializer\JsonApiSerializer;
46
47
/**
48
 * Class TagController
49
 */
50
class TagController extends Controller
51
{
52
    use TransactionFilter;
53
54
    /** @var TagRepositoryInterface The tag repository */
55
    private $repository;
56
57
    /**
58
     * RuleController constructor.
59
     */
60
    public function __construct()
61
    {
62
        parent::__construct();
63
        $this->middleware(
64
            function ($request, $next) {
65
                /** @var User $user */
66
                $user = auth()->user();
67
68
                $this->repository = app(TagRepositoryInterface::class);
69
                $this->repository->setUser($user);
70
71
                return $next($request);
72
            }
73
        );
74
    }
75
76
    /**
77
     * @param Request $request
78
     *
79
     * @return JsonResponse
80
     * @throws FireflyException
81
     */
82
    public function cloud(Request $request): JsonResponse
83
    {
84
        // parameters for cloud:
85
        $start = (string)$request->get('start');
86
        $end   = (string)$request->get('end');
87
        if ('' === $start || '' === $end) {
88
            throw new FireflyException('Start and end are mandatory parameters.');
89
        }
90
        $start = Carbon::createFromFormat('Y-m-d', $start);
91
        $end   = Carbon::createFromFormat('Y-m-d', $end);
92
93
        // get all tags:
94
        $tags   = $this->repository->get();
95
        $min    = null;
96
        $max    = 0;
97
        $return = [
98
            'tags' => [],
99
        ];
100
        /** @var Tag $tag */
101
        foreach ($tags as $tag) {
102
            $earned = (float)$this->repository->earnedInPeriod($tag, $start, $end);
0 ignored issues
show
It seems like $end can also be of type false; however, parameter $end of FireflyIII\Repositories\...rface::earnedInPeriod() does only seem to accept Carbon\Carbon, 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

102
            $earned = (float)$this->repository->earnedInPeriod($tag, $start, /** @scrutinizer ignore-type */ $end);
Loading history...
It seems like $start can also be of type false; however, parameter $start of FireflyIII\Repositories\...rface::earnedInPeriod() does only seem to accept Carbon\Carbon, 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

102
            $earned = (float)$this->repository->earnedInPeriod($tag, /** @scrutinizer ignore-type */ $start, $end);
Loading history...
103
            $spent  = (float)$this->repository->spentInPeriod($tag, $start, $end);
0 ignored issues
show
It seems like $end can also be of type false; however, parameter $end of FireflyIII\Repositories\...erface::spentInPeriod() does only seem to accept Carbon\Carbon, 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

103
            $spent  = (float)$this->repository->spentInPeriod($tag, $start, /** @scrutinizer ignore-type */ $end);
Loading history...
It seems like $start can also be of type false; however, parameter $start of FireflyIII\Repositories\...erface::spentInPeriod() does only seem to accept Carbon\Carbon, 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

103
            $spent  = (float)$this->repository->spentInPeriod($tag, /** @scrutinizer ignore-type */ $start, $end);
Loading history...
104
            $size   = ($spent * -1) + $earned;
105
            $min    = $min ?? $size;
106
            if ($size > 0) {
107
                $max              = $size > $max ? $size : $max;
108
                $return['tags'][] = [
109
                    'tag'  => $tag->tag,
110
                    'id'   => $tag->id,
111
                    'size' => $size,
112
                ];
113
            }
114
        }
115
        foreach ($return['tags'] as $index => $info) {
116
            $return['tags'][$index]['relative'] = $return['tags'][$index]['size'] / $max;
117
        }
118
        $return['min'] = $min;
119
        $return['max'] = $max;
120
121
122
        return response()->json($return);
123
    }
124
125
    /**
126
     * Delete the resource.
127
     *
128
     * @param Tag $tag
129
     *
130
     * @return JsonResponse
131
     */
132
    public function delete(Tag $tag): JsonResponse
133
    {
134
        $this->repository->destroy($tag);
135
136
        return response()->json([], 204);
137
    }
138
139
    /**
140
     * List all of them.
141
     *
142
     * @param Request $request
143
     *
144
     * @return JsonResponse]
145
     */
146
    public function index(Request $request): JsonResponse
147
    {
148
        // create some objects:
149
        $manager = new Manager;
150
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
151
152
        // types to get, page size:
153
        $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
154
155
        // get list of budgets. Count it and split it.
156
        $collection = $this->repository->get();
157
        $count      = $collection->count();
158
        $rules      = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
159
160
        // make paginator:
161
        $paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page'));
162
        $paginator->setPath(route('api.v1.tags.index') . $this->buildParams());
163
164
        // present to user.
165
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
166
167
        /** @var TagTransformer $transformer */
168
        $transformer = app(TagTransformer::class);
169
        $transformer->setParameters($this->parameters);
170
171
        $resource = new FractalCollection($rules, $transformer, 'tags');
172
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
173
174
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
175
    }
176
177
    /**
178
     * List single resource.
179
     *
180
     * @param Request $request
181
     * @param Tag     $tag
182
     *
183
     * @return JsonResponse
184
     */
185
    public function show(Request $request, Tag $tag): JsonResponse
186
    {
187
        $manager = new Manager();
188
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
189
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
190
191
        /** @var TagTransformer $transformer */
192
        $transformer = app(TagTransformer::class);
193
        $transformer->setParameters($this->parameters);
194
195
        $resource = new Item($tag, $transformer, 'tags');
196
197
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
198
199
    }
200
201
    /**
202
     * Store new object.
203
     *
204
     * @param TagRequest $request
205
     *
206
     * @return JsonResponse
207
     */
208
    public function store(TagRequest $request): JsonResponse
209
    {
210
        $rule    = $this->repository->store($request->getAll());
211
        $manager = new Manager();
212
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
213
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
214
215
        /** @var TagTransformer $transformer */
216
        $transformer = app(TagTransformer::class);
217
        $transformer->setParameters($this->parameters);
218
219
        $resource = new Item($rule, $transformer, 'tags');
220
221
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
222
    }
223
224
    /**
225
     * Show all transactions.
226
     *
227
     * @param Request $request
228
     * @param Tag     $tag
229
     *
230
     * @return JsonResponse
231
     */
232
    public function transactions(Request $request, Tag $tag): JsonResponse
233
    {
234
        $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
235
        $type     = $request->get('type') ?? 'default';
236
        $this->parameters->set('type', $type);
237
238
        $types   = $this->mapTransactionTypes($this->parameters->get('type'));
239
        $manager = new Manager();
240
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
241
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
242
243
        /** @var User $admin */
244
        $admin = auth()->user();
245
        /** @var TransactionCollectorInterface $collector */
246
        $collector = app(TransactionCollectorInterface::class);
247
        $collector->setUser($admin);
248
        $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
249
        $collector->setAllAssetAccounts();
250
        $collector->setTag($tag);
251
252
        if (\in_array(TransactionType::TRANSFER, $types, true)) {
253
            $collector->removeFilter(InternalTransferFilter::class);
254
        }
255
256
        if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
257
            $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
258
        }
259
        $collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
260
        $collector->setTypes($types);
261
        $paginator = $collector->getPaginatedTransactions();
262
        $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
263
        $transactions = $paginator->getCollection();
264
265
        /** @var TransactionTransformer $transformer */
266
        $transformer = app(TransactionTransformer::class);
267
        $transformer->setParameters($this->parameters);
268
269
        $resource = new FractalCollection($transactions, $transformer, 'transactions');
270
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
271
272
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
273
    }
274
275
    /**
276
     * Update a rule.
277
     *
278
     * @param TagRequest $request
279
     * @param Tag        $tag
280
     *
281
     * @return JsonResponse
282
     */
283
    public function update(TagRequest $request, Tag $tag): JsonResponse
284
    {
285
        $rule    = $this->repository->update($tag, $request->getAll());
286
        $manager = new Manager();
287
        $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
288
        $manager->setSerializer(new JsonApiSerializer($baseUrl));
289
        /** @var TagTransformer $transformer */
290
        $transformer = app(TagTransformer::class);
291
        $transformer->setParameters($this->parameters);
292
293
        $resource = new Item($rule, $transformer, 'tags');
294
295
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
296
297
    }
298
}
299