Files
firefly-iii/app/Console/Commands/Import.php

166 lines
4.5 KiB
PHP
Raw Normal View History

<?php
2016-07-15 22:26:08 +02:00
/**
* Import.php
2018-05-11 10:08:34 +02:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
2016-07-15 22:26:08 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-07-15 22:26:08 +02:00
*/
/** @noinspection MultipleReturnStatementsInspection */
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2016-07-15 22:26:08 +02:00
namespace FireflyIII\Console\Commands;
2017-12-21 19:18:53 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Routine\RoutineInterface;
2016-07-15 22:26:08 +02:00
use FireflyIII\Models\ImportJob;
2018-07-05 21:18:53 +02:00
use FireflyIII\Models\Tag;
2016-07-15 22:26:08 +02:00
use Illuminate\Console\Command;
use Log;
/**
2017-11-15 12:25:49 +01:00
* Class Import.
*
* @codeCoverageIgnore
2016-07-15 22:26:08 +02:00
*/
class Import extends Command
{
/**
* The console command description.
*
* @var string
*/
2016-10-20 19:10:43 +02:00
protected $description = 'This will start a new import.';
2016-07-15 22:26:08 +02:00
/**
* The name and signature of the console command.
*
* @var string
*/
2016-10-20 19:10:43 +02:00
protected $signature = 'firefly:start-import {key}';
2016-07-15 22:26:08 +02:00
/**
2017-08-15 17:26:43 +02:00
* Run the import routine.
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2017-12-22 18:32:43 +01:00
*
* @throws FireflyException
2016-07-15 22:26:08 +02:00
*/
2018-07-22 10:05:06 +02:00
public function handle(): int
2016-07-15 22:26:08 +02:00
{
2016-10-20 19:10:43 +02:00
Log::debug('Start start-import command');
2018-07-26 06:10:17 +02:00
$jobKey = (string)$this->argument('key');
2018-07-27 04:46:21 +02:00
/** @var ImportJob $job */
2018-08-06 19:14:30 +02:00
$job = ImportJob::where('key', $jobKey)->first();
2017-11-15 12:25:49 +01:00
if (null === $job) {
$this->errorLine(sprintf('No job found with key "%s"', $jobKey));
2017-03-01 21:02:47 +01:00
2018-07-22 10:05:06 +02:00
return 1;
}
if (!$this->isValid($job)) {
$this->errorLine('Job is not valid for some reason. Exit.');
2016-10-22 09:33:03 +02:00
2018-07-22 10:05:06 +02:00
return 1;
2016-07-15 22:26:08 +02:00
}
$this->infoLine(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type));
2017-06-20 21:04:25 +02:00
2017-12-21 19:18:53 +01:00
// actually start job:
2018-02-09 19:11:55 +01:00
$type = 'csv' === $job->file_type ? 'file' : $job->file_type;
$key = sprintf('import.routine.%s', $type);
2017-12-21 19:18:53 +01:00
$className = config($key);
if (null === $className || !class_exists($className)) {
throw new FireflyException(sprintf('Cannot find import routine class for job of type "%s".', $type)); // @codeCoverageIgnore
}
/** @var RoutineInterface $routine */
$routine = app($className);
2018-07-05 21:18:53 +02:00
$routine->setImportJob($job);
2017-06-20 21:04:25 +02:00
$routine->run();
2018-07-05 21:18:53 +02:00
/**
* @var int $index
* @var string $error
*/
foreach ($job->errors as $index => $error) {
$this->errorLine(sprintf('Error importing line #%d: %s', $index, $error));
2017-06-21 20:04:35 +02:00
}
2018-07-05 21:18:53 +02:00
/** @var Tag $tag */
$tag = $job->tag()->first();
$count = 0;
if (null === $tag) {
$count = $tag->transactionJournals()->count();
}
$this->infoLine(sprintf('The import has finished. %d transactions have been imported.', $count));
2018-07-22 10:05:06 +02:00
return 0;
2016-07-15 22:26:08 +02:00
}
/**
* Displays an error.
*
* @param string $message
* @param array|null $data
*/
private function errorLine(string $message, array $data = null): void
{
Log::error($message, $data ?? []);
$this->error($message);
}
/**
* Displays an informational message.
*
* @param string $message
* @param array $data
*/
private function infoLine(string $message, array $data = null): void
{
Log::info($message, $data ?? []);
$this->line($message);
}
/**
2017-08-15 17:26:43 +02:00
* Check if job is valid to be imported.
*
* @param ImportJob $job
*
* @return bool
*/
private function isValid(ImportJob $job): bool
{
2017-11-15 12:25:49 +01:00
if (null === $job) {
$this->errorLine('This job does not seem to exist.');
return false;
}
2017-11-15 12:25:49 +01:00
if ('configured' !== $job->status) {
2017-06-20 21:04:25 +02:00
Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status));
$this->errorLine('This job is not ready to be imported.');
return false;
}
return true;
}
2016-07-15 22:26:08 +02:00
}