1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * Import.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\Models\ImportJob; |
||
28 | use Illuminate\Console\Command; |
||
29 | use Illuminate\Support\MessageBag; |
||
30 | use Log; |
||
31 | |||
32 | /** |
||
33 | * Class Import. |
||
34 | */ |
||
35 | class Import extends Command |
||
36 | { |
||
37 | /** |
||
38 | * The console command description. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $description = 'This will start a new import.'; |
||
43 | |||
44 | /** |
||
45 | * The name and signature of the console command. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $signature = 'firefly:start-import {key}'; |
||
50 | |||
51 | /** |
||
52 | * Run the import routine. |
||
53 | * |
||
54 | * @throws FireflyException |
||
55 | */ |
||
56 | public function handle() |
||
57 | { |
||
58 | Log::debug('Start start-import command'); |
||
59 | $jobKey = $this->argument('key'); |
||
60 | $job = ImportJob::where('key', $jobKey)->first(); |
||
61 | if (null === $job) { |
||
62 | $this->errorLine(sprintf('No job found with key "%s"', $jobKey)); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
63 | |||
64 | return; |
||
65 | } |
||
66 | if (!$this->isValid($job)) { |
||
67 | $this->errorLine('Job is not valid for some reason. Exit.'); |
||
68 | |||
69 | return; |
||
70 | } |
||
71 | |||
72 | $this->infoLine(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type)); |
||
0 ignored issues
–
show
|
|||
73 | |||
74 | // actually start job: |
||
75 | $type = 'csv' === $job->file_type ? 'file' : $job->file_type; |
||
76 | $key = sprintf('import.routine.%s', $type); |
||
77 | $className = config($key); |
||
78 | if (null === $className || !class_exists($className)) { |
||
79 | throw new FireflyException(sprintf('Cannot find import routine class for job of type "%s".', $type)); // @codeCoverageIgnore |
||
80 | } |
||
81 | |||
82 | /** @var RoutineInterface $routine */ |
||
83 | $routine = app($className); |
||
84 | $routine->setJob($job); |
||
85 | $routine->run(); |
||
86 | |||
87 | /** @var MessageBag $error */ |
||
88 | foreach ($routine->getErrors() as $index => $error) { |
||
89 | $this->errorLine(sprintf('Error importing line #%d: %s', $index, $error)); |
||
90 | } |
||
91 | |||
92 | $this->infoLine( |
||
93 | sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->getLines()) |
||
94 | ); |
||
95 | |||
96 | return; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $message |
||
101 | * @param array|null $data |
||
102 | */ |
||
103 | private function errorLine(string $message, array $data = null): void |
||
104 | { |
||
105 | Log::error($message, $data ?? []); |
||
106 | $this->error($message); |
||
107 | |||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $message |
||
112 | * @param array $data |
||
113 | */ |
||
114 | private function infoLine(string $message, array $data = null): void |
||
115 | { |
||
116 | Log::info($message, $data ?? []); |
||
117 | $this->line($message); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Check if job is valid to be imported. |
||
122 | * |
||
123 | * @param ImportJob $job |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | private function isValid(ImportJob $job): bool |
||
128 | { |
||
129 | if (null === $job) { |
||
130 | $this->errorLine('This job does not seem to exist.'); |
||
131 | |||
132 | return false; |
||
133 | } |
||
134 | |||
135 | if ('configured' !== $job->status) { |
||
136 | Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status)); |
||
137 | $this->errorLine('This job is not ready to be imported.'); |
||
138 | |||
139 | return false; |
||
140 | } |
||
141 | |||
142 | return true; |
||
143 | } |
||
144 | } |
||
145 |