1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SpectreConfigurator.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III 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. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Import\Configuration; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Exceptions\FireflyException; |
26
|
|
|
use FireflyIII\Models\ImportJob; |
27
|
|
|
use FireflyIII\Support\Import\Configuration\Spectre\HaveAccounts; |
28
|
|
|
use Log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class SpectreConfigurator. |
32
|
|
|
*/ |
33
|
|
|
class SpectreConfigurator implements ConfiguratorInterface |
34
|
|
|
{ |
35
|
|
|
/** @var ImportJob */ |
36
|
|
|
private $job; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $warning = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ConfiguratorInterface constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store any data from the $data array into the job. |
50
|
|
|
* |
51
|
|
|
* @param array $data |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function configureJob(array $data): bool |
56
|
|
|
{ |
57
|
|
|
$config = $this->job->configuration; |
58
|
|
|
$stage = $config['stage']; |
59
|
|
|
$status = $this->job->status; |
|
|
|
|
60
|
|
|
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage)); |
61
|
|
|
switch ($stage) { |
62
|
|
|
case 'have-accounts': |
63
|
|
|
/** @var HaveAccounts $class */ |
64
|
|
|
$class = app(HaveAccounts::class); |
65
|
|
|
$class->setJob($this->job); |
66
|
|
|
$class->storeConfiguration($data); |
67
|
|
|
|
68
|
|
|
// update job for next step and set to "configured". |
69
|
|
|
$config = $this->job->configuration; |
70
|
|
|
$config['stage'] = 'have-account-mapping'; |
71
|
|
|
$this->job->configuration = $config; |
72
|
|
|
$this->job->status = 'configured'; |
73
|
|
|
$this->job->save(); |
74
|
|
|
return true; |
75
|
|
|
break; |
|
|
|
|
76
|
|
|
default: |
77
|
|
|
throw new FireflyException(sprintf('Cannot store configuration when job is in state "%s"', $stage)); |
78
|
|
|
break; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return the data required for the next step in the job configuration. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getNextData(): array |
88
|
|
|
{ |
89
|
|
|
$config = $this->job->configuration; |
90
|
|
|
$stage = $config['stage']; |
91
|
|
|
$status = $this->job->status; |
|
|
|
|
92
|
|
|
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage)); |
93
|
|
|
switch ($stage) { |
94
|
|
|
case 'has-token': |
95
|
|
|
// simply redirect to Spectre. |
96
|
|
|
$config['is-redirected'] = true; |
97
|
|
|
$config['stage'] = 'user-logged-in'; |
98
|
|
|
$status = 'configured'; |
99
|
|
|
break; |
100
|
|
|
case 'have-accounts': |
101
|
|
|
// use special class: |
|
|
|
|
102
|
|
|
/** @var HaveAccounts $class */ |
103
|
|
|
$class = app(HaveAccounts::class); |
104
|
|
|
$class->setJob($this->job); |
105
|
|
|
$data = $class->getData(); |
106
|
|
|
|
107
|
|
|
return $data; |
108
|
|
|
default: |
109
|
|
|
return []; |
110
|
|
|
break; |
|
|
|
|
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// update config and status: |
115
|
|
|
$this->job->configuration = $config; |
116
|
|
|
$this->job->status = $status; |
117
|
|
|
$this->job->save(); |
118
|
|
|
|
119
|
|
|
return $this->job->configuration; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getNextView(): string |
126
|
|
|
{ |
127
|
|
|
$config = $this->job->configuration; |
128
|
|
|
$stage = $config['stage']; |
129
|
|
|
Log::debug(sprintf('in getNextView(), for stage "%s".', $stage)); |
130
|
|
|
switch ($stage) { |
131
|
|
|
case 'has-token': |
132
|
|
|
// redirect to Spectre. |
133
|
|
|
return 'import.spectre.redirect'; |
134
|
|
|
break; |
|
|
|
|
135
|
|
|
case 'have-accounts': |
136
|
|
|
return 'import.spectre.accounts'; |
137
|
|
|
break; |
|
|
|
|
138
|
|
|
default: |
139
|
|
|
return ''; |
140
|
|
|
break; |
|
|
|
|
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return possible warning to user. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getWarningMessage(): string |
151
|
|
|
{ |
152
|
|
|
return $this->warning; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function isJobConfigured(): bool |
159
|
|
|
{ |
160
|
|
|
$config = $this->job->configuration; |
161
|
|
|
$stage = $config['stage']; |
162
|
|
|
Log::debug(sprintf('in isJobConfigured(), for stage "%s".', $stage)); |
163
|
|
|
switch ($stage) { |
164
|
|
|
case 'has-token': |
165
|
|
|
case 'have-accounts': |
166
|
|
|
Log::debug('isJobConfigured returns false'); |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
default: |
170
|
|
|
Log::debug('isJobConfigured returns true'); |
171
|
|
|
|
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param ImportJob $job |
178
|
|
|
*/ |
179
|
|
|
public function setJob(ImportJob $job) |
180
|
|
|
{ |
181
|
|
|
$defaultConfig = [ |
182
|
|
|
'has-token' => false, |
183
|
|
|
'token' => '', |
184
|
|
|
'token-expires' => 0, |
185
|
|
|
'token-url' => '', |
186
|
|
|
'is-redirected' => false, |
187
|
|
|
'customer' => null, |
188
|
|
|
'login' => null, |
189
|
|
|
'stage' => 'initial', |
190
|
|
|
'accounts' => '', |
191
|
|
|
'accounts-mapped' => '', |
192
|
|
|
'auto-start' => true, |
193
|
|
|
]; |
194
|
|
|
$extendedStatus = $job->extended_status; |
195
|
|
|
$extendedStatus['steps'] = 100; |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
$config = $job->configuration; |
199
|
|
|
$finalConfig = array_merge($defaultConfig, $config); |
200
|
|
|
$job->configuration = $finalConfig; |
201
|
|
|
$job->extended_status = $extendedStatus; |
202
|
|
|
$job->save(); |
203
|
|
|
$this->job = $job; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.