1 | <?php |
||||||
2 | declare(strict_types=1); |
||||||
3 | /** |
||||||
4 | * CreateImport.php |
||||||
5 | * Copyright (c) 2017 [email protected] |
||||||
6 | * |
||||||
7 | * This file is part of Firefly III. |
||||||
8 | * |
||||||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||||||
10 | * it under the terms of the GNU General Public License as published by |
||||||
11 | * the Free Software Foundation, either version 3 of the License, or |
||||||
12 | * (at your option) any later version. |
||||||
13 | * |
||||||
14 | * Firefly III is distributed in the hope that it will be useful, |
||||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
17 | * GNU General Public License for more details. |
||||||
18 | * |
||||||
19 | * You should have received a copy of the GNU General Public License |
||||||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||||||
21 | */ |
||||||
22 | |||||||
23 | namespace FireflyIII\Console\Commands; |
||||||
24 | |||||||
25 | use FireflyIII\Exceptions\FireflyException; |
||||||
26 | use FireflyIII\Import\Routine\RoutineInterface; |
||||||
27 | use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; |
||||||
28 | use FireflyIII\Repositories\User\UserRepositoryInterface; |
||||||
29 | use FireflyIII\Services\Internal\File\EncryptService; |
||||||
30 | use Illuminate\Console\Command; |
||||||
31 | use Illuminate\Support\MessageBag; |
||||||
32 | use Log; |
||||||
33 | use Preferences; |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Class CreateImport. |
||||||
37 | */ |
||||||
38 | class CreateImport extends Command |
||||||
39 | { |
||||||
40 | use VerifiesAccessToken; |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
41 | /** |
||||||
42 | * The console command description. |
||||||
43 | * |
||||||
44 | * @var string |
||||||
45 | */ |
||||||
46 | protected $description = 'Use this command to create a new import. Your user ID can be found on the /profile page.'; |
||||||
47 | |||||||
48 | /** |
||||||
49 | * The name and signature of the console command. |
||||||
50 | * |
||||||
51 | * @var string |
||||||
52 | */ |
||||||
53 | protected $signature |
||||||
54 | = 'firefly:create-import |
||||||
55 | {file : The file to import.} |
||||||
56 | {configuration : The configuration file to use for the import.} |
||||||
57 | {--type=csv : The file type of the import.} |
||||||
58 | {--user= : The user ID that the import should import for.} |
||||||
59 | {--token= : The user\'s access token.} |
||||||
60 | {--start : Starts the job immediately.}'; |
||||||
61 | |||||||
62 | /** |
||||||
63 | * Run the command. |
||||||
64 | * |
||||||
65 | * @noinspection MultipleReturnStatementsInspection |
||||||
66 | * |
||||||
67 | * @throws FireflyException |
||||||
68 | */ |
||||||
69 | public function handle(): int |
||||||
70 | { |
||||||
71 | if (!$this->verifyAccessToken()) { |
||||||
72 | $this->errorLine('Invalid access token.'); |
||||||
73 | |||||||
74 | return 1; |
||||||
75 | } |
||||||
76 | /** @var UserRepositoryInterface $userRepository */ |
||||||
77 | $userRepository = app(UserRepositoryInterface::class); |
||||||
78 | $file = $this->argument('file'); |
||||||
79 | $configuration = $this->argument('configuration'); |
||||||
80 | $user = $userRepository->findNull((int)$this->option('user')); |
||||||
81 | $cwd = getcwd(); |
||||||
82 | $type = strtolower($this->option('type')); |
||||||
83 | |||||||
84 | if (!$this->validArguments()) { |
||||||
85 | $this->errorLine('Invalid arguments.'); |
||||||
86 | |||||||
87 | return 1; |
||||||
88 | } |
||||||
89 | |||||||
90 | $configurationData = json_decode(file_get_contents($configuration), true); |
||||||
0 ignored issues
–
show
It seems like
$configuration can also be of type array ; however, parameter $filename of file_get_contents() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
91 | if (null === $configurationData) { |
||||||
92 | $this->errorLine(sprintf('Firefly III cannot read the contents of configuration file "%s" (working directory: "%s").', $configuration, $cwd)); |
||||||
0 ignored issues
–
show
It seems like
$configuration can also be of type array ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
93 | |||||||
94 | return 1; |
||||||
95 | } |
||||||
96 | |||||||
97 | $this->infoLine(sprintf('Going to create a job to import file: %s', $file)); |
||||||
98 | $this->infoLine(sprintf('Using configuration file: %s', $configuration)); |
||||||
99 | $this->infoLine(sprintf('Import into user: #%d (%s)', $user->id, $user->email)); |
||||||
100 | $this->infoLine(sprintf('Type of import: %s', $type)); |
||||||
101 | |||||||
102 | /** @var ImportJobRepositoryInterface $jobRepository */ |
||||||
103 | $jobRepository = app(ImportJobRepositoryInterface::class); |
||||||
104 | $jobRepository->setUser($user); |
||||||
105 | $job = $jobRepository->create($type); |
||||||
106 | $this->infoLine(sprintf('Created job "%s"', $job->key)); |
||||||
0 ignored issues
–
show
|
|||||||
107 | |||||||
108 | /** @var EncryptService $service */ |
||||||
109 | $service = app(EncryptService::class); |
||||||
110 | $service->encrypt($file, $job->key); |
||||||
0 ignored issues
–
show
It seems like
$file can also be of type array ; however, parameter $file of FireflyIII\Services\Inte...cryptService::encrypt() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
111 | |||||||
112 | $this->infoLine('Stored import data...'); |
||||||
113 | |||||||
114 | $jobRepository->setConfiguration($job, $configurationData); |
||||||
115 | $jobRepository->updateStatus($job, 'configured'); |
||||||
116 | $this->infoLine('Stored configuration...'); |
||||||
117 | |||||||
118 | if (true === $this->option('start')) { |
||||||
0 ignored issues
–
show
|
|||||||
119 | $this->infoLine('The import will start in a moment. This process is not visible...'); |
||||||
120 | Log::debug('Go for import!'); |
||||||
121 | |||||||
122 | // normally would refer to other firefly:start-import but that doesn't seem to work all to well... |
||||||
123 | |||||||
124 | // start the actual routine: |
||||||
125 | $type = 'csv' === $job->file_type ? 'file' : $job->file_type; |
||||||
126 | $key = sprintf('import.routine.%s', $type); |
||||||
127 | $className = config($key); |
||||||
128 | if (null === $className || !class_exists($className)) { |
||||||
129 | throw new FireflyException(sprintf('Cannot find import routine class for job of type "%s".', $type)); // @codeCoverageIgnore |
||||||
130 | } |
||||||
131 | /** @var RoutineInterface $routine */ |
||||||
132 | $routine = app($className); |
||||||
133 | $routine->setJob($job); |
||||||
134 | $routine->run(); |
||||||
135 | |||||||
136 | // give feedback. |
||||||
137 | /** @var MessageBag $error */ |
||||||
138 | foreach ($routine->getErrors() as $index => $error) { |
||||||
139 | $this->errorLine(sprintf('Error importing line #%d: %s', $index, $error)); |
||||||
140 | } |
||||||
141 | $this->infoLine( |
||||||
142 | sprintf( |
||||||
143 | 'The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines() |
||||||
144 | ) |
||||||
145 | ); |
||||||
146 | } |
||||||
147 | |||||||
148 | // clear cache for user: |
||||||
149 | Preferences::setForUser($user, 'lastActivity', microtime()); |
||||||
0 ignored issues
–
show
The method
setForUser() does not exist on FireflyIII\Support\Facades\Preferences . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
150 | |||||||
151 | return 0; |
||||||
152 | } |
||||||
153 | |||||||
154 | /** |
||||||
155 | * @param string $message |
||||||
156 | * @param array|null $data |
||||||
157 | */ |
||||||
158 | private function errorLine(string $message, array $data = null): void |
||||||
159 | { |
||||||
160 | Log::error($message, $data ?? []); |
||||||
161 | $this->error($message); |
||||||
162 | |||||||
163 | } |
||||||
164 | |||||||
165 | /** |
||||||
166 | * @param string $message |
||||||
167 | * @param array $data |
||||||
168 | */ |
||||||
169 | private function infoLine(string $message, array $data = null): void |
||||||
170 | { |
||||||
171 | Log::info($message, $data ?? []); |
||||||
172 | $this->line($message); |
||||||
173 | } |
||||||
174 | |||||||
175 | /** |
||||||
176 | * Verify user inserts correct arguments. |
||||||
177 | * |
||||||
178 | * @noinspection MultipleReturnStatementsInspection |
||||||
179 | * @return bool |
||||||
180 | */ |
||||||
181 | private function validArguments(): bool |
||||||
182 | { |
||||||
183 | $file = $this->argument('file'); |
||||||
184 | $configuration = $this->argument('configuration'); |
||||||
185 | $cwd = getcwd(); |
||||||
186 | $validTypes = config('import.options.file.import_formats'); |
||||||
187 | $type = strtolower($this->option('type')); |
||||||
188 | |||||||
189 | if (!\in_array($type, $validTypes, true)) { |
||||||
190 | $this->errorLine(sprintf('Cannot import file of type "%s"', $type)); |
||||||
191 | |||||||
192 | return false; |
||||||
193 | } |
||||||
194 | |||||||
195 | if (!file_exists($file)) { |
||||||
0 ignored issues
–
show
It seems like
$file can also be of type array ; however, parameter $filename of file_exists() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
196 | $this->errorLine(sprintf('Firefly III cannot find file "%s" (working directory: "%s").', $file, $cwd)); |
||||||
0 ignored issues
–
show
It seems like
$file can also be of type array ; however, parameter $args of sprintf() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
197 | |||||||
198 | return false; |
||||||
199 | } |
||||||
200 | |||||||
201 | if (!file_exists($configuration)) { |
||||||
202 | $this->errorLine(sprintf('Firefly III cannot find configuration file "%s" (working directory: "%s").', $configuration, $cwd)); |
||||||
203 | |||||||
204 | return false; |
||||||
205 | } |
||||||
206 | |||||||
207 | return true; |
||||||
208 | } |
||||||
209 | } |
||||||
210 |