1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* DownloadController.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\Import; |
26
|
|
|
|
27
|
|
|
use App\Bunq\Download\JobStatus\JobStatus; |
28
|
|
|
use App\Bunq\Download\JobStatus\JobStatusManager; |
29
|
|
|
use App\Bunq\Download\RoutineManager; |
30
|
|
|
use App\Exceptions\ImportException; |
31
|
|
|
use App\Http\Controllers\Controller; |
32
|
|
|
use App\Services\Configuration\Configuration; |
33
|
|
|
use App\Services\Session\Constants; |
34
|
|
|
use Illuminate\Contracts\View\Factory; |
35
|
|
|
use Illuminate\Http\JsonResponse; |
36
|
|
|
use Illuminate\Http\Request; |
37
|
|
|
use Illuminate\View\View; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class DownloadController. |
41
|
|
|
*/ |
42
|
|
|
class DownloadController extends Controller |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* DownloadController constructor. |
46
|
|
|
*/ |
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
app('view')->share('pageTitle', 'Download transactions from bunq'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Factory|View |
55
|
|
|
*/ |
56
|
|
|
public function index() |
57
|
|
|
{ |
58
|
|
|
$mainTitle = 'Downloading transactions...'; |
59
|
|
|
$subTitle = 'Connecting to bunq and downloading your data...'; |
60
|
|
|
|
61
|
|
|
// job ID may be in session: |
62
|
|
|
$downloadIdentifier = session()->get(Constants::DOWNLOAD_JOB_IDENTIFIER); |
63
|
|
|
if (null !== $downloadIdentifier) { |
64
|
|
|
// create a new import job: |
65
|
|
|
new RoutineManager($downloadIdentifier); |
66
|
|
|
} |
67
|
|
|
if (null === $downloadIdentifier) { |
68
|
|
|
// create a new import job: |
69
|
|
|
$routine = new RoutineManager(); |
70
|
|
|
$downloadIdentifier = $routine->getDownloadIdentifier(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
app('log')->debug(sprintf('Download routine manager identifier is "%s"', $downloadIdentifier)); |
74
|
|
|
|
75
|
|
|
// store identifier in session so the status can get it. |
76
|
|
|
session()->put(Constants::DOWNLOAD_JOB_IDENTIFIER, $downloadIdentifier); |
77
|
|
|
app('log')->debug(sprintf('Stored "%s" under "%s"', $downloadIdentifier, Constants::DOWNLOAD_JOB_IDENTIFIER)); |
78
|
|
|
|
79
|
|
|
return view('import.download.index', compact('mainTitle', 'subTitle', 'downloadIdentifier')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Request $request |
84
|
|
|
* |
85
|
|
|
* @return JsonResponse |
86
|
|
|
*/ |
87
|
|
|
public function start(Request $request): JsonResponse |
88
|
|
|
{ |
89
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__)); |
90
|
|
|
$downloadIdentifier = $request->get('downloadIdentifier'); |
91
|
|
|
$routine = new RoutineManager($downloadIdentifier); |
92
|
|
|
|
93
|
|
|
// store identifier in session so the status can get it. |
94
|
|
|
session()->put(Constants::DOWNLOAD_JOB_IDENTIFIER, $downloadIdentifier); |
95
|
|
|
|
96
|
|
|
$downloadJobStatus = JobStatusManager::startOrFindJob($downloadIdentifier); |
|
|
|
|
97
|
|
|
if (JobStatus::JOB_DONE === $downloadJobStatus->status) { |
98
|
|
|
// TODO DISABLED DURING DEVELOPMENT: |
99
|
|
|
//app('log')->debug('Job already done!'); |
100
|
|
|
//return response()->json($downloadJobStatus->toArray()); |
101
|
|
|
} |
102
|
|
|
JobStatusManager::setJobStatus(JobStatus::JOB_RUNNING); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$config = session()->get(Constants::CONFIGURATION) ?? []; |
106
|
|
|
$routine->setConfiguration(Configuration::fromArray($config)); |
107
|
|
|
$routine->start(); |
108
|
|
|
} catch (ImportException $e) { |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// set done: |
112
|
|
|
JobStatusManager::setJobStatus(JobStatus::JOB_DONE); |
113
|
|
|
|
114
|
|
|
return response()->json($downloadJobStatus->toArray()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Request $request |
119
|
|
|
* |
120
|
|
|
* @return JsonResponse |
121
|
|
|
*/ |
122
|
|
|
public function status(Request $request): JsonResponse |
123
|
|
|
{ |
124
|
|
|
$downloadIdentifier = $request->get('downloadIdentifier'); |
125
|
|
|
//app('log')->debug(sprintf('Now at %s(%s)', __METHOD__, $downloadIdentifier)); |
126
|
|
|
if (null === $downloadIdentifier) { |
127
|
|
|
app('log')->warning('Download Identifier is NULL.'); |
128
|
|
|
// no status is known yet because no identifier is in the session. |
129
|
|
|
// As a fallback, return empty status |
130
|
|
|
$fakeStatus = new JobStatus(); |
131
|
|
|
|
132
|
|
|
return response()->json($fakeStatus->toArray()); |
133
|
|
|
} |
134
|
|
|
$importJobStatus = JobStatusManager::startOrFindJob($downloadIdentifier); |
135
|
|
|
|
136
|
|
|
return response()->json($importJobStatus->toArray()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|