Passed
Push — master ( 270226...e22eb7 )
by James
06:04 queued 11s
created

TokenController::index()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 32
rs 8.9777
1
<?php
2
/**
3
 * TokenController.php
4
 * Copyright (c) 2019 [email protected]
5
 *
6
 * This file is part of Firefly III CSV Importer.
7
 *
8
 * Firefly III CSV Importer is free software: you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III CSV Importer 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 CSV Importer.If not, see
20
 * <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace App\Http\Controllers;
24
25
26
use App\Exceptions\ApiHttpException;
27
use App\Services\FireflyIIIApi\Request\SystemInformationRequest;
28
use App\Services\FireflyIIIApi\Response\SystemInformationResponse;
29
use Illuminate\Http\JsonResponse;
30
31
/**
32
 * Class TokenController
33
 */
34
class TokenController extends Controller
35
{
36
    /**
37
     * Check if the Firefly III API responds properly.
38
     *
39
     * @return JsonResponse
40
     */
41
    public function doValidate(): JsonResponse
42
    {
43
        $response = ['result' => 'OK', 'message' => null];
44
        $request  = new SystemInformationRequest();
45
        try {
46
            $result = $request->get();
47
        } catch (ApiHttpException $e) {
48
            $response = ['result' => 'NOK', 'message' => $e->getMessage()];
49
        }
50
        // -1 = OK (minimum is smaller)
51
        // 0 = OK (same version)
52
        // 1 = NOK (too low a version)
53
54
        $minimum = config('csv_importer.minimum_version');
55
        $compare = version_compare($minimum, $result->version);
56
        if (1 === $compare) {
57
            $errorMessage = sprintf('Your Firefly III version %s is below the minimum required version %s', $result->version, $minimum);
58
            $response = ['result' => 'NOK', 'message' => $errorMessage];
59
        }
60
61
        return response()->json($response);
62
    }
63
64
    /**
65
     * Same thing but not over JSON.
66
     *
67
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
68
     */
69
    public function index()
70
    {
71
        $request      = new SystemInformationRequest();
72
        $errorMessage = 'No error message.';
73
        $isError      = false;
74
        $result       = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
75
        $compare      = 1;
76
        try {
77
            /** @var SystemInformationResponse $result */
78
            $result = $request->get();
79
        } catch (ApiHttpException $e) {
80
            $errorMessage = $e->getMessage();
81
            $isError      = true;
82
        }
83
        // -1 = OK (minimum is smaller)
84
        // 0 = OK (same version)
85
        // 1 = NOK (too low a version)
86
        if (false === $isError) {
87
            $minimum = config('csv_importer.minimum_version');
88
            $compare = version_compare($minimum, $result->version);
89
        }
90
        if (false === $isError && 1 === $compare) {
91
            $errorMessage = sprintf('Your Firefly III version %s is below the minimum required version %s', $result->version, $minimum);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $minimum does not seem to be defined for all execution paths leading up to this point.
Loading history...
92
            $isError      = true;
93
        }
94
95
        if (false === $isError) {
96
            return redirect(route('index'));
97
        }
98
        $pageTitle = 'Token error';
99
100
        return view('token.index', compact('errorMessage', 'pageTitle'));
101
    }
102
103
}
104