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.

ShowController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
eloc 60
c 4
b 0
f 0
dl 0
loc 126
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A debugShow() 0 3 1
A getAmounts() 0 28 5
A show() 0 48 5
1
<?php
2
/**
3
 * ShowController.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\Http\Controllers\Transaction;
25
26
use FireflyIII\Exceptions\FireflyException;
27
use FireflyIII\Http\Controllers\Controller;
28
use FireflyIII\Models\TransactionGroup;
29
use FireflyIII\Models\TransactionJournal;
30
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
31
use FireflyIII\Transformers\TransactionGroupTransformer;
32
use Illuminate\Contracts\View\Factory;
33
use Illuminate\Http\JsonResponse;
34
use Illuminate\Http\Request;
35
use Illuminate\Support\Str;
36
use Illuminate\View\View;
37
use Symfony\Component\HttpFoundation\ParameterBag;
38
39
/**
40
 * Class ShowController
41
 */
42
class ShowController extends Controller
43
{
44
    /** @var TransactionGroupRepositoryInterface */
45
    private $repository;
46
47
    /**
48
     * ShowController constructor.
49
     */
50
    public function __construct()
51
    {
52
        parent::__construct();
53
54
        // some useful repositories:
55
        $this->middleware(
56
            function ($request, $next) {
57
                $this->repository = app(TransactionGroupRepositoryInterface::class);
58
59
                app('view')->share('title', (string) trans('firefly.transactions'));
60
                app('view')->share('mainTitleIcon', 'fa-exchange');
61
62
                return $next($request);
63
            }
64
        );
65
    }
66
67
    /**
68
     * @param TransactionGroup $transactionGroup
69
     *
70
     * @return JsonResponse
71
     */
72
    public function debugShow(TransactionGroup $transactionGroup)
73
    {
74
        return response()->json($this->repository->expandGroup($transactionGroup));
75
    }
76
77
    /**
78
     * @param TransactionGroup $transactionGroup
79
     *
80
     * @throws FireflyException
81
     * @return Factory|View
82
     */
83
    public function show(Request $request, TransactionGroup $transactionGroup)
84
    {
85
        /** @var TransactionJournal $first */
86
        $first  = $transactionGroup->transactionJournals()->first(['transaction_journals.*']);
87
        $splits = $transactionGroup->transactionJournals()->count();
88
89
        if (null === $first) {
90
            throw new FireflyException('This transaction is broken :(.');
91
        }
92
93
        $type     = (string) trans(sprintf('firefly.%s', $first->transactionType->type));
94
        $title    = 1 === $splits ? $first->description : $transactionGroup->title;
95
        $subTitle = sprintf('%s: "%s"', $type, $title);
96
97
        /** @var TransactionGroupTransformer $transformer */
98
        $transformer = app(TransactionGroupTransformer::class);
99
        $transformer->setParameters(new ParameterBag);
100
        $groupArray = $transformer->transformObject($transactionGroup);
101
102
        // do some amount calculations:
103
        $amounts = $this->getAmounts($groupArray);
104
105
        // make sure notes are escaped but not double escaped.
106
        foreach ($groupArray['transactions'] as $index => $transaction) {
107
            $search = ['&amp;', '&gt;', '&lt;'];
108
            if (!Str::contains($transaction['notes'], $search)) {
109
                $groupArray['transactions'][$index]['notes'] = e($transaction['notes']);
110
            }
111
            $groupArray['transactions'][$index]['tags'] = $this->repository->getTagObjects($groupArray['transactions'][$index]['transaction_journal_id']);
112
        }
113
114
        $events      = $this->repository->getPiggyEvents($transactionGroup);
115
        $attachments = $this->repository->getAttachments($transactionGroup);
116
        $links       = $this->repository->getLinks($transactionGroup);
117
118
        return view(
119
            'transactions.show',
120
            compact(
121
                'transactionGroup',
122
                'amounts',
123
                'first',
124
                'type',
125
                'subTitle',
126
                'splits',
127
                'groupArray',
128
                'events',
129
                'attachments',
130
                'links'
131
            )
132
        );
133
    }
134
135
    /**
136
     * @param array $group
137
     *
138
     * @return array
139
     */
140
    private function getAmounts(array $group): array
141
    {
142
        $amounts = [];
143
        foreach ($group['transactions'] as $transaction) {
144
            $symbol = $transaction['currency_symbol'];
145
            if (!isset($amounts[$symbol])) {
146
                $amounts[$symbol] = [
147
                    'amount'         => '0',
148
                    'symbol'         => $symbol,
149
                    'decimal_places' => $transaction['currency_decimal_places'],
150
                ];
151
            }
152
            $amounts[$symbol]['amount'] = bcadd($amounts[$symbol]['amount'], $transaction['amount']);
153
            if (null !== $transaction['foreign_amount']) {
154
                // same for foreign currency:
155
                $foreignSymbol = $transaction['foreign_currency_symbol'];
156
                if (!isset($amounts[$foreignSymbol])) {
157
                    $amounts[$foreignSymbol] = [
158
                        'amount'         => '0',
159
                        'symbol'         => $foreignSymbol,
160
                        'decimal_places' => $transaction['foreign_currency_decimal_places'],
161
                    ];
162
                }
163
                $amounts[$foreignSymbol]['amount'] = bcadd($amounts[$foreignSymbol]['amount'], $transaction['foreign_amount']);
164
            }
165
        }
166
167
        return $amounts;
168
    }
169
}
170