1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* SyncController.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\Exceptions\ImportException; |
28
|
|
|
use App\Http\Controllers\Controller; |
29
|
|
|
use App\Services\Configuration\Configuration; |
30
|
|
|
use App\Services\Session\Constants; |
31
|
|
|
use App\Services\Sync\JobStatus\JobStatus; |
32
|
|
|
use App\Services\Sync\JobStatus\JobStatusManager; |
33
|
|
|
use App\Services\Sync\RoutineManager; |
34
|
|
|
use Illuminate\Http\JsonResponse; |
35
|
|
|
use Illuminate\Http\Request; |
36
|
|
|
use RuntimeException; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class SyncController. |
40
|
|
|
*/ |
41
|
|
|
class SyncController extends Controller |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* SyncController constructor. |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
app('view')->share('pageTitle', 'Send data to Firefly III'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function index() |
53
|
|
|
{ |
54
|
|
|
//app('log')->debug(sprintf('Now at %s', __METHOD__)); |
55
|
|
|
$mainTitle = 'Send data to Firefly III'; |
56
|
|
|
$subTitle = 'After download, comes import.'; |
57
|
|
|
|
58
|
|
|
// get download job ID so we have the data to send to FF3 |
59
|
|
|
$downloadIdentifier = session()->get(Constants::DOWNLOAD_JOB_IDENTIFIER); |
60
|
|
|
|
61
|
|
|
// get sync ID so we have a separate track thing. |
62
|
|
|
$syncIdentifier = session()->get(Constants::SYNC_JOB_IDENTIFIER); |
63
|
|
|
|
64
|
|
|
if (null !== $syncIdentifier) { |
65
|
|
|
// create a new import job: |
66
|
|
|
app('log')->debug('SyncController is creating new routine manager with existing sync identifier'); |
67
|
|
|
new RoutineManager($syncIdentifier); |
68
|
|
|
} |
69
|
|
|
if (null === $syncIdentifier) { |
70
|
|
|
app('log')->debug('SyncController is creating new routine manager with NEW sync identifier'); |
71
|
|
|
// create a new import job: |
72
|
|
|
$routine = new RoutineManager(null); |
73
|
|
|
$syncIdentifier = $routine->getSyncIdentifier(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
app('log')->debug(sprintf('Sync routine manager job identifier is "%s"', $syncIdentifier)); |
77
|
|
|
|
78
|
|
|
// store identifier in session so the status can get it. |
79
|
|
|
session()->put(Constants::SYNC_JOB_IDENTIFIER, $syncIdentifier); |
80
|
|
|
app('log')->debug(sprintf('Stored "%s" under "%s"', $syncIdentifier, Constants::SYNC_JOB_IDENTIFIER)); |
81
|
|
|
|
82
|
|
|
return view('import.sync.index', compact('mainTitle', 'subTitle', 'syncIdentifier', 'downloadIdentifier')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Request $request |
87
|
|
|
* |
88
|
|
|
* @return JsonResponse |
89
|
|
|
*/ |
90
|
|
|
public function start(Request $request): JsonResponse |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
//app('log')->debug(sprintf('Now at %s', __METHOD__)); |
93
|
|
|
|
94
|
|
|
// get download job ID so we have the data to send to FF3 |
95
|
|
|
$downloadIdentifier = session()->get(Constants::DOWNLOAD_JOB_IDENTIFIER); |
96
|
|
|
|
97
|
|
|
// get sync ID so we have a separate track thing. |
98
|
|
|
$syncIdentifier = session()->get(Constants::SYNC_JOB_IDENTIFIER); |
99
|
|
|
|
100
|
|
|
$routine = new RoutineManager($syncIdentifier); |
101
|
|
|
|
102
|
|
|
// store identifier in session so the status can get it (should already be there) |
103
|
|
|
session()->put(Constants::SYNC_JOB_IDENTIFIER, $syncIdentifier); |
104
|
|
|
session()->put(Constants::DOWNLOAD_JOB_IDENTIFIER, $downloadIdentifier); |
105
|
|
|
|
106
|
|
|
$downloadJobStatus = JobStatusManager::startOrFindJob($syncIdentifier); |
107
|
|
|
if (JobStatus::JOB_DONE === $downloadJobStatus->status) { |
108
|
|
|
// TODO DISABLED DURING DEVELOPMENT: |
109
|
|
|
//app('log')->debug('Job already done!'); |
110
|
|
|
//return response()->json($downloadJobStatus->toArray()); |
111
|
|
|
} |
112
|
|
|
JobStatusManager::setJobStatus(JobStatus::JOB_RUNNING); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
$config = session()->get(Constants::CONFIGURATION) ?? []; |
116
|
|
|
$routine->setConfiguration(Configuration::fromArray($config)); |
117
|
|
|
$routine->setDownloadIdentifier($downloadIdentifier); |
118
|
|
|
$routine->setSyncIdentifier($syncIdentifier); |
119
|
|
|
$routine->start(); |
120
|
|
|
} catch (ImportException $e) { |
121
|
|
|
// TODO better error handling. |
122
|
|
|
throw new RuntimeException($e->getMessage()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// set done: |
126
|
|
|
JobStatusManager::setJobStatus(JobStatus::JOB_DONE); |
127
|
|
|
|
128
|
|
|
return response()->json($downloadJobStatus->toArray()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Request $request |
133
|
|
|
* |
134
|
|
|
* @return JsonResponse |
135
|
|
|
*/ |
136
|
|
|
public function status(Request $request): JsonResponse |
137
|
|
|
{ |
138
|
|
|
$syncIdentifier = $request->get('syncIdentifier'); |
139
|
|
|
//app('log')->debug(sprintf('Now at %s(%s)', __METHOD__, $syncIdentifier)); |
140
|
|
|
if (null === $syncIdentifier) { |
141
|
|
|
app('log')->warning('Identifier is NULL.'); |
142
|
|
|
// no status is known yet because no identifier is in the session. |
143
|
|
|
// As a fallback, return empty status |
144
|
|
|
$fakeStatus = new JobStatus(); |
145
|
|
|
|
146
|
|
|
return response()->json($fakeStatus->toArray()); |
147
|
|
|
} |
148
|
|
|
$importJobStatus = JobStatusManager::startOrFindJob($syncIdentifier); |
149
|
|
|
|
150
|
|
|
return response()->json($importJobStatus->toArray()); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.