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 ( 075c06...6b2b2a )
by James
20:26 queued 10:33
created

AugumentData   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 26
eloc 91
c 3
b 0
f 0
dl 0
loc 250
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A combineAccounts() 0 18 3
A getAccountNames() 0 15 3
A getBudgetNames() 0 15 3
A extractNames() 0 9 2
A groupByName() 0 20 4
A getLimits() 0 30 3
A getCategoryNames() 0 15 3
A spentInPeriod() 0 34 3
A expandNames() 0 8 2
1
<?php
2
/**
3
 * AugumentData.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\Support\Http\Controllers;
25
26
use Carbon\Carbon;
27
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
28
use FireflyIII\Models\Account;
29
use FireflyIII\Models\AccountType;
30
use FireflyIII\Models\Budget;
31
use FireflyIII\Models\BudgetLimit;
32
use FireflyIII\Models\TransactionType;
33
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
34
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
35
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
36
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
37
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
38
use FireflyIII\Support\CacheProperties;
39
use Illuminate\Support\Collection;
40
41
/**
42
 * Trait AugumentData
43
 *
44
 */
45
trait AugumentData
46
{
47
    /**
48
     * Searches for the opposing account.
49
     *
50
     * @param Collection $accounts
51
     *
52
     * @return array
53
     */
54
    protected function combineAccounts(Collection $accounts): array // filter + group data
55
    {
56
        /** @var AccountRepositoryInterface $repository */
57
        $repository = app(AccountRepositoryInterface::class);
58
        $combined   = [];
59
        /** @var Account $expenseAccount */
60
        foreach ($accounts as $expenseAccount) {
61
            $collection = new Collection;
62
            $collection->push($expenseAccount);
63
64
            $revenue = $repository->findByName($expenseAccount->name, [AccountType::REVENUE]);
65
            if (null !== $revenue) {
66
                $collection->push($revenue);
67
            }
68
            $combined[$expenseAccount->name] = $collection;
69
        }
70
71
        return $combined;
72
    }
73
74
    /**
75
     * Small helper function for the revenue and expense account charts.
76
     *
77
     * @param array $names
78
     *
79
     * @return array
80
     */
81
    protected function expandNames(array $names): array
82
    {
83
        $result = [];
84
        foreach ($names as $entry) {
85
            $result[$entry['name']] = 0;
86
        }
87
88
        return $result;
89
    }
90
91
    /**
92
     * Small helper function for the revenue and expense account charts.
93
     *
94
     * @param Collection $accounts
95
     *
96
     * @return array
97
     */
98
    protected function extractNames(Collection $accounts): array
99
    {
100
        $return = [];
101
        /** @var Account $account */
102
        foreach ($accounts as $account) {
103
            $return[$account->id] = $account->name;
104
        }
105
106
        return $return;
107
    }
108
109
    /**
110
     * Get the account names belonging to a bunch of account ID's.
111
     *
112
     * @param array $accountIds
113
     *
114
     * @return array
115
     */
116
    protected function getAccountNames(array $accountIds): array // extract info from array.
117
    {
118
        /** @var AccountRepositoryInterface $repository */
119
        $repository = app(AccountRepositoryInterface::class);
120
        $accounts   = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT, AccountType::EXPENSE, AccountType::CASH]);
121
        $grouped    = $accounts->groupBy('id')->toArray();
122
        $return     = [];
123
        foreach ($accountIds as $accountId) {
124
            if (isset($grouped[$accountId])) {
125
                $return[$accountId] = $grouped[$accountId][0]['name'];
126
            }
127
        }
128
        $return[0] = '(no name)';
129
130
        return $return;
131
    }
132
133
    /**
134
     * Get the budget names from a set of budget ID's.
135
     *
136
     * @param array $budgetIds
137
     *
138
     * @return array
139
     */
140
    protected function getBudgetNames(array $budgetIds): array // extract info from array.
141
    {
142
        /** @var BudgetRepositoryInterface $repository */
143
        $repository = app(BudgetRepositoryInterface::class);
144
        $budgets    = $repository->getBudgets();
145
        $grouped    = $budgets->groupBy('id')->toArray();
146
        $return     = [];
147
        foreach ($budgetIds as $budgetId) {
148
            if (isset($grouped[$budgetId])) {
149
                $return[$budgetId] = $grouped[$budgetId][0]['name'];
150
            }
151
        }
152
        $return[0] = (string)trans('firefly.no_budget');
153
154
        return $return;
155
    }
156
157
    /**
158
     * Get the category names from a set of category ID's. Small helper function for some of the charts.
159
     *
160
     * @param array $categoryIds
161
     *
162
     * @return array
163
     */
164
    protected function getCategoryNames(array $categoryIds): array // extract info from array.
165
    {
166
        /** @var CategoryRepositoryInterface $repository */
167
        $repository = app(CategoryRepositoryInterface::class);
168
        $categories = $repository->getCategories();
169
        $grouped    = $categories->groupBy('id')->toArray();
170
        $return     = [];
171
        foreach ($categoryIds as $categoryId) {
172
            if (isset($grouped[$categoryId])) {
173
                $return[$categoryId] = $grouped[$categoryId][0]['name'];
174
            }
175
        }
176
        $return[0] = (string)trans('firefly.no_category');
177
178
        return $return;
179
    }
180
181
    /**
182
     * Gets all budget limits for a budget.
183
     *
184
     * @param Budget $budget
185
     * @param Carbon $start
186
     * @param Carbon $end
187
     *
188
     * @return Collection
189
     */
190
    protected function getLimits(Budget $budget, Carbon $start, Carbon $end): Collection // get data + augment with info
191
    {
192
        /** @var OperationsRepositoryInterface $opsRepository */
193
        $opsRepository = app(OperationsRepositoryInterface::class);
194
195
        /** @var BudgetLimitRepositoryInterface $blRepository */
196
        $blRepository = app(BudgetLimitRepositoryInterface::class);
197
198
        // properties for cache
199
        $cache = new CacheProperties;
200
        $cache->addProperty($start);
201
        $cache->addProperty($end);
202
        $cache->addProperty($budget->id);
203
        $cache->addProperty('get-limits');
204
205
        if ($cache->has()) {
206
            return $cache->get(); // @codeCoverageIgnore
207
        }
208
209
        $set    = $blRepository->getBudgetLimits($budget, $start, $end);
210
        $limits = new Collection();
211
212
        /** @var BudgetLimit $entry */
213
        foreach ($set as $entry) {
214
            $entry->spent = $opsRepository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date);
0 ignored issues
show
Deprecated Code introduced by
The function FireflyIII\Repositories\...erface::spentInPeriod() has been deprecated. ( Ignorable by Annotation )

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

214
            $entry->spent = /** @scrutinizer ignore-deprecated */ $opsRepository->spentInPeriod(new Collection([$budget]), new Collection(), $entry->start_date, $entry->end_date);
Loading history...
215
            $limits->push($entry);
216
        }
217
        $cache->store($limits);
218
219
        return $set;
220
    }
221
222
    /**
223
     * Group set of transactions by name of opposing account.
224
     *
225
     * @param array $array
226
     *
227
     * @return array
228
     */
229
    protected function groupByName(array $array): array // filter + group data
230
    {
231
232
        // group by opposing account name.
233
        $grouped = [];
234
        /** @var array $journal */
235
        foreach ($array as $journal) {
236
            $name = '(no name)';
237
            if (TransactionType::WITHDRAWAL === $journal['transaction_type_type']) {
238
                $name = $journal['destination_account_name'];
239
            }
240
            if (TransactionType::WITHDRAWAL !== $journal['transaction_type_type']) {
241
                $name = $journal['source_account_name'];
242
            }
243
244
            $grouped[$name] = $grouped[$name] ?? '0';
245
            $grouped[$name] = bcadd($journal['amount'], $grouped[$name]);
246
        }
247
248
        return $grouped;
249
    }
250
251
    /**
252
     * Spent in a period.
253
     *
254
     * @param Collection $assets
255
     * @param Collection $opposing
256
     * @param Carbon     $start
257
     * @param Carbon     $end
258
     *
259
     * @return array
260
     */
261
    protected function spentInPeriod(Collection $assets, Collection $opposing, Carbon $start, Carbon $end): array // get data + augment with info
262
    {
263
        /** @var GroupCollectorInterface $collector */
264
        $collector = app(GroupCollectorInterface::class);
265
266
        $total = $assets->merge($opposing);
267
        $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setAccounts($total);
268
        $journals = $collector->getExtractedJournals();
269
        $sum      = [
270
            'grand_sum'    => '0',
271
            'per_currency' => [],
272
        ];
273
        // loop to support multi currency
274
        foreach ($journals as $journal) {
275
            $currencyId = (int)$journal['currency_id'];
276
277
            // if not set, set to zero:
278
            if (!isset($sum['per_currency'][$currencyId])) {
279
                $sum['per_currency'][$currencyId] = [
280
                    'sum'      => '0',
281
                    'currency' => [
282
                        'name'           => $journal['currency_name'],
283
                        'symbol'         => $journal['currency_symbol'],
284
                        'decimal_places' => $journal['currency_decimal_places'],
285
                    ],
286
                ];
287
            }
288
289
            // add amount
290
            $sum['per_currency'][$currencyId]['sum'] = bcadd($sum['per_currency'][$currencyId]['sum'], $journal['amount']);
291
            $sum['grand_sum']                        = bcadd($sum['grand_sum'], $journal['amount']);
292
        }
293
294
        return $sum;
295
    }
296
}
297