|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* TokenController.php |
|
6
|
|
|
* Copyright (c) 2020 [email protected]. |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the Firefly III bunq importer |
|
9
|
|
|
* (https://github.com/firefly-iii/bunq-importer). |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace App\Http\Controllers; |
|
26
|
|
|
|
|
27
|
|
|
use App\Bunq\ApiContext\ApiContextManager; |
|
28
|
|
|
use App\Exceptions\ImportException; |
|
29
|
|
|
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException; |
|
30
|
|
|
use GrumpyDictator\FFIIIApiSupport\Request\SystemInformationRequest; |
|
31
|
|
|
use GrumpyDictator\FFIIIApiSupport\Response\SystemInformationResponse; |
|
32
|
|
|
use Illuminate\Contracts\View\Factory; |
|
33
|
|
|
use Illuminate\Http\JsonResponse; |
|
34
|
|
|
use Illuminate\Http\RedirectResponse; |
|
35
|
|
|
use Illuminate\Routing\Redirector; |
|
36
|
|
|
use Illuminate\View\View; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class TokenController. |
|
40
|
|
|
*/ |
|
41
|
|
|
class TokenController extends Controller |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* Check if the Firefly III API responds properly. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws ImportException |
|
47
|
|
|
* @return JsonResponse |
|
48
|
|
|
*/ |
|
49
|
|
|
public function doValidate(): JsonResponse |
|
50
|
|
|
{ |
|
51
|
|
|
$response = ['result' => 'OK', 'message' => null]; |
|
52
|
|
|
$token = (string) config('bunq.access_token'); |
|
53
|
|
|
$uri = (string) config('bunq.uri'); |
|
54
|
|
|
app('log')->debug(sprintf('Going to try and access %s', $uri)); |
|
55
|
|
|
$request = new SystemInformationRequest($uri, $token); |
|
56
|
|
|
try { |
|
57
|
|
|
/** @var SystemInformationResponse $result */ |
|
58
|
|
|
$result = $request->get(); |
|
59
|
|
|
} catch (ApiHttpException $e) { |
|
60
|
|
|
app('log')->error($e->getMessage()); |
|
61
|
|
|
app('log')->error($e->getTraceAsString()); |
|
62
|
|
|
$response = ['result' => 'NOK', 'message' => $e->getMessage()]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (isset($result)) { |
|
66
|
|
|
$minimum = config('bunq.minimum_version'); |
|
67
|
|
|
$compare = version_compare($minimum, $result->version); |
|
68
|
|
|
if (1 === $compare) { |
|
69
|
|
|
$errorMessage = sprintf('Your Firefly III version %s is below the minimum required version %s', $result->version, $minimum); |
|
70
|
|
|
$response = ['result' => 'NOK', 'message' => $errorMessage]; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// validate connection to bunq, create API context. |
|
75
|
|
|
try { |
|
76
|
|
|
ApiContextManager::getApiContext(); |
|
77
|
|
|
} catch (ApiHttpException $e) { |
|
78
|
|
|
app('log')->error($e->getMessage()); |
|
79
|
|
|
app('log')->error($e->getTraceAsString()); |
|
80
|
|
|
$errorMessage = sprintf('bunq complained: %s', $e->getMessage()); |
|
81
|
|
|
$response = ['result' => 'NOK', 'message' => $errorMessage]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return response()->json($response); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Same thing but not over JSON. |
|
89
|
|
|
* |
|
90
|
|
|
* @throws ImportException |
|
91
|
|
|
* @return Factory|RedirectResponse|Redirector|View |
|
92
|
|
|
*/ |
|
93
|
|
|
public function index() |
|
94
|
|
|
{ |
|
95
|
|
|
$token = (string) config('bunq.access_token'); |
|
96
|
|
|
$uri = (string) config('bunq.uri'); |
|
97
|
|
|
app('log')->debug(sprintf('Going to try and access %s', $uri)); |
|
98
|
|
|
$request = new SystemInformationRequest($uri, $token); |
|
99
|
|
|
$errorMessage = 'No error message.'; |
|
100
|
|
|
$isError = false; |
|
101
|
|
|
$result = null; |
|
|
|
|
|
|
102
|
|
|
$compare = 1; |
|
103
|
|
|
try { |
|
104
|
|
|
/** @var SystemInformationResponse $result */ |
|
105
|
|
|
$result = $request->get(); |
|
106
|
|
|
} catch (ApiHttpException $e) { |
|
107
|
|
|
app('log')->error($e->getMessage()); |
|
108
|
|
|
app('log')->error($e->getTraceAsString()); |
|
109
|
|
|
$errorMessage = $e->getMessage(); |
|
110
|
|
|
$isError = true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (false === $isError) { |
|
114
|
|
|
$minimum = config('bunq.minimum_version'); |
|
115
|
|
|
$compare = version_compare($minimum, $result->version); |
|
116
|
|
|
} |
|
117
|
|
|
if (false === $isError && 1 === $compare) { |
|
118
|
|
|
$errorMessage = sprintf('Your Firefly III version %s is below the minimum required version %s', $result->version, $minimum); |
|
|
|
|
|
|
119
|
|
|
$isError = true; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// validate connection to bunq, create API context. |
|
123
|
|
|
try { |
|
124
|
|
|
ApiContextManager::getApiContext(); |
|
125
|
|
|
} catch (ApiHttpException $e) { |
|
126
|
|
|
app('log')->error($e->getMessage()); |
|
127
|
|
|
app('log')->error($e->getTraceAsString()); |
|
128
|
|
|
$errorMessage = sprintf('bunq complained: %s', $e->getMessage()); |
|
129
|
|
|
$isError = true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (false === $isError) { |
|
133
|
|
|
return redirect(route('index')); |
|
134
|
|
|
} |
|
135
|
|
|
$pageTitle = 'Token error'; |
|
136
|
|
|
|
|
137
|
|
|
return view('token.index', compact('errorMessage', 'pageTitle')); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|