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

125 lines
3.3 KiB
PHP
Raw Normal View History

2016-07-15 15:26:08 -05:00
<?php
/**
* Import.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-07-15 15:26:08 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-07-15 15:26:08 -05:00
*/
declare(strict_types=1);
2016-07-15 15:26:08 -05:00
namespace FireflyIII\Console\Commands;
use FireflyIII\Import\Logging\CommandHandler;
2017-06-20 14:04:25 -05:00
use FireflyIII\Import\Routine\ImportRoutine;
2016-07-15 15:26:08 -05:00
use FireflyIII\Models\ImportJob;
use Illuminate\Console\Command;
2017-06-21 13:04:35 -05:00
use Illuminate\Support\MessageBag;
2016-07-15 15:26:08 -05:00
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class Import.
2016-07-15 15:26:08 -05:00
*/
class Import extends Command
{
/**
* The console command description.
*
* @var string
*/
2016-10-20 12:10:43 -05:00
protected $description = 'This will start a new import.';
2016-07-15 15:26:08 -05:00
/**
* The name and signature of the console command.
*
* @var string
*/
2016-10-20 12:10:43 -05:00
protected $signature = 'firefly:start-import {key}';
2016-07-15 15:26:08 -05:00
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
2017-08-15 10:26:43 -05:00
* Run the import routine.
2016-07-15 15:26:08 -05:00
*/
public function handle()
{
2016-10-20 12:10:43 -05:00
Log::debug('Start start-import command');
2016-07-15 15:26:08 -05:00
$jobKey = $this->argument('key');
2017-03-01 14:02:47 -06:00
$job = ImportJob::where('key', $jobKey)->first();
2017-11-15 05:25:49 -06:00
if (null === $job) {
$this->error(sprintf('No job found with key "%s"', $jobKey));
2017-03-01 14:02:47 -06:00
return;
}
if (!$this->isValid($job)) {
2016-10-20 12:10:43 -05:00
Log::error('Job is not valid for some reason. Exit.');
2016-10-22 02:33:03 -05:00
2016-07-15 15:26:08 -05:00
return;
}
$this->line(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type));
2016-08-05 23:21:25 -05:00
2016-07-15 15:26:08 -05:00
$monolog = Log::getMonolog();
$handler = new CommandHandler($this);
$monolog->pushHandler($handler);
2017-06-20 14:04:25 -05:00
/** @var ImportRoutine $routine */
$routine = app(ImportRoutine::class);
$routine->setJob($job);
2017-06-20 14:04:25 -05:00
$routine->run();
2017-06-21 13:04:35 -05:00
/** @var MessageBag $error */
2017-06-24 01:37:09 -05:00
foreach ($routine->errors as $index => $error) {
$this->error(sprintf('Error importing line #%d: %s', $index, $error));
2017-06-21 13:04:35 -05:00
}
$this->line(sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->journals->count(), $routine->lines));
return;
2016-07-15 15:26:08 -05:00
}
/**
2017-08-15 10:26:43 -05:00
* Check if job is valid to be imported.
*
* @param ImportJob $job
*
* @return bool
*/
private function isValid(ImportJob $job): bool
{
2017-11-15 05:25:49 -06:00
if (null === $job) {
2017-06-20 14:04:25 -05:00
Log::error('This job does not seem to exist.');
$this->error('This job does not seem to exist.');
return false;
}
2017-11-15 05:25:49 -06:00
if ('configured' !== $job->status) {
2017-06-20 14:04:25 -05:00
Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status));
$this->error('This job is not ready to be imported.');
return false;
}
return true;
}
2016-07-15 15:26:08 -05:00
}