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.
Completed
Push — rewrite-laravel ( 75413a...5c1bf5 )
by Oliver
05:22
created

MmexController::handle()   D

Complexity

Conditions 15
Paths 72

Size

Total Lines 117
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 4.9121
c 0
b 0
f 0
cc 15
eloc 62
nc 72
nop 1

How to fix   Long Method    Complexity   

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
namespace App\Http\Controllers\Api;
4
5
use App\Constants;
6
use App\Http\Controllers\Controller;
7
use App\Models\Transaction;
8
use App\Serializers\MmexArraySerializer;
9
use App\Services\MmexFunctions;
10
use App\Services\MmexService;
11
use App\Transformers\TransactionTransformer;
12
use Illuminate\Http\Request;
13
use Log;
14
15
/**
16
 * API endpoint for client https://github.com/moneymanagerex/moneymanagerex/blob/master/src/webapp.cpp.
17
 * @package App\Http\Controllers\Api
18
 */
19
class MmexController extends Controller
20
{
21
    /**
22
     * @var MmexService
23
     */
24
    private $mmexService;
25
26
    /**
27
     * MmexController constructor.
28
     *
29
     * @param MmexService $mmexService
30
     */
31
    public function __construct(MmexService $mmexService)
32
    {
33
        $this->mmexService = $mmexService;
34
    }
35
36
    public function handle(Request $request)
37
    {
38
        $debugData = ['url' => $request->fullUrl(), 'data' => $request->all(), 'method' => $request->method()];
39
40
        Log::debug('Service Request', $debugData);
41
42
        $data = $request->all();
43
44
        $postData = null;
45
46
        if (isset($data['MMEX_Post'])) {
47
            $postData = json_decode($data['MMEX_Post']);
48
        } elseif (isset($data['postData'])) {
49
            $postData = json_decode($data['postData']);
50
        }
51
52
        if ($postData) {
53
            Log::debug('MmexController, $postData', [$postData]);
54
        }
55
56
        $function = $this->getFunction($data);
57
58
        if ($function == MmexFunctions::CheckGuid) {
59
            // TODO
60
            return $this->returnSuccess();
61
        }
62
63
        if ($function == MmexFunctions::CheckApiVersion) {
64
            return $this->returnText(Constants::$api_version);
65
        }
66
67
        if ($function == MmexFunctions::DownloadTransactions) {
68
            $transactions = $this->mmexService->getTransactions();
69
70
            $result = fractal()
71
                ->collection($transactions)
72
                ->serializeWith(new MmexArraySerializer())
73
                ->transformWith(new TransactionTransformer())
0 ignored issues
show
Documentation introduced by
new \App\Transformers\TransactionTransformer() is of type object<App\Transformers\TransactionTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
                ->toJson();
75
76
            return $this->returnText($result);
77
        }
78
79
        if ($function == MmexFunctions::DonwloadAttachment) {
80
81
            // is something like: Transaction_3_test-receipt-3.png
82
            $fileName = $data["download_attachment"];
83
84
            // extract transaction
85
            $fileNameParts = explode('_', $fileName);
86
            $transactionId = $fileNameParts[1];
87
            $transaction = Transaction::findOrFail($transactionId);
88
89
            // get attachment of transaction
90
            $media = $transaction->getMedia('attachments')->first(function ($item) use ($fileName) {
91
                return $item->file_name == $fileName;
92
            });
93
94
            // return file as download
95
            $filePath = $media->getPath();
96
97
            $headers = [
98
                "Content-Type"              => "",
99
                "Cache-Control"             => "public",
100
                "Content-Description"       => "File Transfer",
101
                "Content-Disposition"       => "attachment; filename= ".$fileName,
102
                "Content-Transfer-Encoding" => "binary",
103
            ];
104
105
            return response()->file($filePath, $headers);
106
        }
107
108
        if ($function == MmexFunctions::DeleteBankAccounts) {
109
            $this->mmexService->deleteAccounts();
110
111
            return $this->returnSuccess();
112
        }
113
114
        if ($function == MmexFunctions::ImportBankAccounts) {
115
            $this->mmexService->importBankAccounts($postData);
116
117
            return $this->returnSuccess();
118
        }
119
120
        if ($function == MmexFunctions::DeletePayees) {
121
            $this->mmexService->deletePayees();
122
123
            return $this->returnSuccess();
124
        }
125
126
        if ($function == MmexFunctions::ImportPayees) {
127
            $this->mmexService->importPayees($postData);
128
129
            return $this->returnSuccess();
130
        }
131
132
        if ($function == MmexFunctions::DeleteCategories) {
133
            $this->mmexService->deleteCategories();
134
135
            return $this->returnSuccess();
136
        }
137
138
        if ($function == MmexFunctions::ImportCategories) {
139
            $this->mmexService->importCategories($postData);
140
141
            return $this->returnSuccess();
142
        }
143
144
        if ($function == MmexFunctions::DeleteTransactions) {
145
            $transactionId = $data['delete_group'];
146
            $this->mmexService->deleteTransactions($transactionId);
147
148
            return $this->returnSuccess();
149
        }
150
151
        return $data;
152
    }
153
154
    private function getFunction($data)
155
    {
156
        if (isset($data[MmexFunctions::CheckApiVersion])) {
157
            return MmexFunctions::CheckApiVersion;
158
        }
159
        if (isset($data[MmexFunctions::CheckGuid])) {
160
            return MmexFunctions::CheckGuid;
161
        }
162
        if (isset($data[MmexFunctions::DeleteAttachment])) {
163
            return MmexFunctions::DeleteAttachment;
164
        }
165
        if (isset($data[MmexFunctions::DeleteBankAccounts])) {
166
            return MmexFunctions::DeleteBankAccounts;
167
        }
168
        if (isset($data[MmexFunctions::DeleteCategories])) {
169
            return MmexFunctions::DeleteCategories;
170
        }
171
        if (isset($data[MmexFunctions::DeletePayees])) {
172
            return MmexFunctions::DeletePayees;
173
        }
174
        if (isset($data[MmexFunctions::DeleteTransactions])) {
175
            return MmexFunctions::DeleteTransactions;
176
        }
177
        if (isset($data[MmexFunctions::DonwloadAttachment])) {
178
            return MmexFunctions::DonwloadAttachment;
179
        }
180
        if (isset($data[MmexFunctions::DownloadTransactions])) {
181
            return MmexFunctions::DownloadTransactions;
182
        }
183
        if (isset($data[MmexFunctions::ImportBankAccounts])) {
184
            return MmexFunctions::ImportBankAccounts;
185
        }
186
        if (isset($data[MmexFunctions::ImportBankAccounts])) {
187
            return MmexFunctions::ImportBankAccounts;
188
        }
189
        if (isset($data[MmexFunctions::ImportPayees])) {
190
            return MmexFunctions::ImportPayees;
191
        }
192
        if (isset($data[MmexFunctions::DeleteCategories])) {
193
            return MmexFunctions::DeleteCategories;
194
        }
195
        if (isset($data[MmexFunctions::ImportCategories])) {
196
            return MmexFunctions::ImportCategories;
197
        }
198
199
        throw new \Exception('No valid function request!');
200
    }
201
202
    private function returnSuccess()
203
    {
204
        return $this->returnText(Constants::$operation_succeded);
205
    }
206
207
    private function returnText($text)
208
    {
209
        // TODO: Really dirty hack to force key => value format for outputs via fractal serializer
210
        $text = str_replace(',"meta":null', '', $text);
211
        return response($text, 200)
0 ignored issues
show
Bug introduced by
The method header() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean sendHeaders()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
212
            ->header('Content-Type', 'text/plain; charset=UTF-8');
213
    }
214
}
215