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.

CategoryController::overview()   B
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 121
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 121
rs 7.5571
c 0
b 0
f 0
cc 9
nc 24
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * CategoryController.php
5
 * Copyright (c) 2019 [email protected]
6
 *
7
 * This file is part of Firefly III (https://github.com/firefly-iii).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program 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 Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace FireflyIII\Api\V1\Controllers\Chart;
26
27
use Carbon\Carbon;
28
use FireflyIII\Api\V1\Controllers\Controller;
29
use FireflyIII\Api\V1\Requests\DateRequest;
30
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
31
use FireflyIII\Repositories\Category\NoCategoryRepositoryInterface;
32
use FireflyIII\Repositories\Category\OperationsRepositoryInterface;
33
use FireflyIII\User;
34
use Illuminate\Http\JsonResponse;
35
36
/**
37
 * Class CategoryController
38
 */
39
class CategoryController extends Controller
40
{
41
    /** @var CategoryRepositoryInterface */
42
    private $categoryRepository;
43
    /** @var NoCategoryRepositoryInterface */
44
    private $noCatRepository;
45
    /** @var OperationsRepositoryInterface */
46
    private $opsRepository;
47
48
    /**
49
     * AccountController constructor.
50
     *
51
     * @codeCoverageIgnore
52
     */
53
    public function __construct()
54
    {
55
        parent::__construct();
56
        $this->middleware(
57
            function ($request, $next) {
58
                /** @var User $user */
59
                $user                     = auth()->user();
60
                $this->categoryRepository = app(CategoryRepositoryInterface::class);
61
                $this->opsRepository      = app(OperationsRepositoryInterface::class);
62
                $this->noCatRepository    = app(NoCategoryRepositoryInterface::class);
63
                $this->categoryRepository->setUser($user);
64
                $this->opsRepository->setUser($user);
65
                $this->noCatRepository->setUser($user);
66
67
                return $next($request);
68
            }
69
        );
70
    }
71
72
73
    /**
74
     * @param DateRequest $request
75
     *
76
     * @return JsonResponse
77
     *
78
     * TODO after 4.8,0, simplify
79
     */
80
    public function overview(DateRequest $request): JsonResponse
81
    {
82
        // parameters for chart:
83
        $dates = $request->getAll();
84
        /** @var Carbon $start */
85
        $start = $dates['start'];
86
        /** @var Carbon $end */
87
        $end = $dates['end'];
88
89
90
        $tempData      = [];
91
        $spentWith     = $this->opsRepository->listExpenses($start, $end);
92
        $earnedWith    = $this->opsRepository->listIncome($start, $end);
93
        $spentWithout  = $this->noCatRepository->listExpenses($start, $end);
94
        $earnedWithout = $this->noCatRepository->listIncome($start, $end);
95
        $categories    = [];
96
97
98
        foreach ([$spentWith, $earnedWith, $spentWithout, $earnedWithout] as $set) {
99
            foreach ($set as $currency) {
100
                foreach ($currency['categories'] as $category) {
101
                    $categories[] = $category['name'];
102
                    $inKey        = sprintf('%d-i', $currency['currency_id']);
103
                    $outKey       = sprintf('%d-e', $currency['currency_id']);
104
                    // make data arrays if not yet present.
105
                    $tempData[$inKey]  = $tempData[$inKey] ?? [
106
                            'currency_id'             => $currency['currency_id'],
107
                            'label'                   => (string) trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
108
                            'currency_code'           => $currency['currency_code'],
109
                            'currency_symbol'         => $currency['currency_symbol'],
110
                            'currency_decimal_places' => $currency['currency_decimal_places'],
111
                            'type'                    => 'bar', // line, area or bar
112
                            'yAxisID'                 => 0, // 0, 1, 2
113
                            'entries'                 => [
114
                                // per category:
115
                                // "category" => 5,
116
                            ],
117
                        ];
118
                    $tempData[$outKey] = $tempData[$outKey] ?? [
119
                            'currency_id'             => $currency['currency_id'],
120
                            'label'                   => (string) trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
121
                            'currency_code'           => $currency['currency_code'],
122
                            'currency_symbol'         => $currency['currency_symbol'],
123
                            'currency_decimal_places' => $currency['currency_decimal_places'],
124
                            'type'                    => 'bar', // line, area or bar
125
                            'yAxisID'                 => 0, // 0, 1, 2
126
                            'entries'                 => [
127
                                // per category:
128
                                // "category" => 5,
129
                            ],
130
                        ];
131
132
                    foreach ($category['transaction_journals'] as $journal) {
133
                        // is it expense or income?
134
                        $letter                                  = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i';
135
                        $currentKey                              = sprintf('%d-%s', $currency['currency_id'], $letter);
136
                        $name                                    = $category['name'];
137
                        $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0';
138
                        $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']);
139
                    }
140
                }
141
            }
142
        }
143
144
        //        foreach ([] as $set) {
145
        //            foreach ($set as $currency) {
146
        //                $inKey        = sprintf('%d-i', $currency['currency_id']);
147
        //                $outKey       = sprintf('%d-e', $currency['currency_id']);
148
        //                $categories[] = (string)trans('firefly.no_category');
149
        //                // make data arrays if not yet present.
150
        //                $tempData[$inKey]  = $tempData[$inKey] ?? [
151
        //                        'currency_id'             => $currency['currency_id'],
152
        //                        'label'                   => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]),
153
        //                        'currency_code'           => $currency['currency_code'],
154
        //                        'currency_symbol'         => $currency['currency_symbol'],
155
        //                        'currency_decimal_places' => $currency['currency_decimal_places'],
156
        //                        'type'                    => 'bar', // line, area or bar
157
        //                        'yAxisID'                 => 0, // 0, 1, 2
158
        //                        'entries'                 => [
159
        //                            // per category:
160
        //                            // "category" => 5,
161
        //                        ],
162
        //                    ];
163
        //                $tempData[$outKey] = $tempData[$outKey] ?? [
164
        //                        'currency_id'             => $currency['currency_id'],
165
        //                        'label'                   => (string)trans('firefly.box_spent_in_currency', ['currency' => $currency['currency_name']]),
166
        //                        'currency_code'           => $currency['currency_code'],
167
        //                        'currency_symbol'         => $currency['currency_symbol'],
168
        //                        'currency_decimal_places' => $currency['currency_decimal_places'],
169
        //                        'type'                    => 'bar', // line, area or bar
170
        //                        'yAxisID'                 => 0, // 0, 1, 2
171
        //                        'entries'                 => [
172
        //                            // per category:
173
        //                            // "category" => 5,
174
        //                        ],
175
        //                    ];
176
        //                foreach ($currency['transaction_journals'] as $journal) {
177
        //                    // is it expense or income?
178
        //                    $letter                                  = -1 === bccomp($journal['amount'], '0') ? 'e' : 'i';
179
        //                    $currentKey                              = sprintf('%d-%s', $currency['currency_id'], $letter);
180
        //                    $name                                    = (string)trans('firefly.no_category');
181
        //                    $tempData[$currentKey]['entries'][$name] = $tempData[$currentKey]['entries'][$name] ?? '0';
182
        //                    $tempData[$currentKey]['entries'][$name] = bcadd($tempData[$currentKey]['entries'][$name], $journal['amount']);
183
        //                }
184
        //            }
185
        //        }
186
187
        // re-sort every spent array and add 0 for missing entries.
188
        foreach ($tempData as $index => $set) {
189
            $oldSet = $set['entries'];
190
            $newSet = [];
191
            foreach ($categories as $category) {
192
                $value             = $oldSet[$category] ?? '0';
193
                $value             = -1 === bccomp($value, '0') ? bcmul($value, '-1') : $value;
194
                $newSet[$category] = $value;
195
            }
196
            $tempData[$index]['entries'] = $newSet;
197
        }
198
        $chartData = array_values($tempData);
199
200
        return response()->json($chartData);
201
    }
202
}
203