2017-06-17 22:49:44 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ImportRoutine.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Import\Routine;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Import\FileProcessor\FileProcessorInterface;
|
|
|
|
use FireflyIII\Import\Storage\ImportStorage;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
class ImportRoutine
|
|
|
|
{
|
|
|
|
|
2017-06-21 20:04:35 +02:00
|
|
|
/** @var Collection */
|
|
|
|
public $errors;
|
2017-06-22 21:50:10 +02:00
|
|
|
/** @var Collection */
|
|
|
|
public $journals;
|
2017-06-21 20:04:35 +02:00
|
|
|
/** @var int */
|
|
|
|
public $lines = 0;
|
2017-06-17 22:49:44 +02:00
|
|
|
/** @var ImportJob */
|
|
|
|
private $job;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ImportRoutine constructor.
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
*/
|
|
|
|
public function __construct(ImportJob $job)
|
|
|
|
{
|
2017-06-21 20:04:35 +02:00
|
|
|
$this->job = $job;
|
|
|
|
$this->journals = new Collection;
|
2017-06-22 21:50:10 +02:00
|
|
|
$this->errors = new Collection;
|
2017-06-24 05:49:33 +02:00
|
|
|
Log::debug(sprintf('Job ID is #%d', $job->id));
|
2017-06-17 22:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function run(): bool
|
|
|
|
{
|
|
|
|
if ($this->job->status !== 'configured') {
|
2017-06-24 05:49:33 +02:00
|
|
|
Log::error(sprintf('Job %s is in state "%s" so it cannot be started.', $this->job->key, $this->job->status));
|
2017-06-17 22:49:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug(sprintf('Start with import job %s', $this->job->key));
|
|
|
|
$objects = new Collection;
|
|
|
|
$type = $this->job->file_type;
|
|
|
|
$class = config(sprintf('firefly.import_processors.%s', $type));
|
|
|
|
/** @var FileProcessorInterface $processor */
|
|
|
|
$processor = app($class);
|
|
|
|
$processor->setJob($this->job);
|
2017-06-22 21:50:10 +02:00
|
|
|
|
2017-06-17 22:49:44 +02:00
|
|
|
set_time_limit(0);
|
|
|
|
if ($this->job->status == 'configured') {
|
2017-06-22 21:50:10 +02:00
|
|
|
|
|
|
|
// set job as "running"...
|
|
|
|
$this->job->status = 'running';
|
|
|
|
$this->job->save();
|
|
|
|
|
2017-06-17 22:49:44 +02:00
|
|
|
Log::debug('Job is configured, start with run()');
|
|
|
|
$processor->run();
|
|
|
|
$objects = $processor->getObjects();
|
|
|
|
}
|
2017-06-21 20:04:35 +02:00
|
|
|
$this->lines = $objects->count();
|
|
|
|
// once done, use storage thing to actually store them:
|
|
|
|
Log::debug(sprintf('Returned %d valid objects from file processor', $this->lines));
|
2017-06-17 22:49:44 +02:00
|
|
|
|
|
|
|
$storage = new ImportStorage;
|
2017-06-20 21:04:25 +02:00
|
|
|
$storage->setJob($this->job);
|
2017-06-17 22:49:44 +02:00
|
|
|
$storage->setDateFormat($this->job->configuration['date-format']);
|
|
|
|
$storage->setObjects($objects);
|
|
|
|
$storage->store();
|
|
|
|
|
2017-06-21 20:04:35 +02:00
|
|
|
// update job:
|
|
|
|
$this->job->status = 'finished';
|
|
|
|
$this->job->save();
|
|
|
|
|
|
|
|
$this->journals = $storage->journals;
|
2017-06-22 21:50:10 +02:00
|
|
|
$this->errors = $storage->errors;
|
|
|
|
|
|
|
|
// run rules:
|
|
|
|
|
2017-06-17 22:49:44 +02:00
|
|
|
|
|
|
|
Log::debug(sprintf('Done with import job %s', $this->job->key));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|