Files
firefly-iii/app/Import/Routine/ImportRoutine.php

167 lines
4.3 KiB
PHP
Raw Normal View History

<?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;
2017-06-24 06:57:24 +02:00
use Carbon\Carbon;
use FireflyIII\Import\FileProcessor\FileProcessorInterface;
use FireflyIII\Import\Storage\ImportStorage;
use FireflyIII\Models\ImportJob;
2017-06-24 06:57:24 +02:00
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
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;
/** @var ImportJob */
private $job;
/**
* ImportRoutine constructor.
*
*/
public function __construct()
{
2017-06-21 20:04:35 +02:00
$this->journals = new Collection;
2017-06-22 21:50:10 +02:00
$this->errors = new Collection;
}
/**
*
*/
public function run(): bool
{
if ($this->job->status !== 'configured') {
Log::error(sprintf('Job %s is in state "%s" so it cannot be started.', $this->job->key, $this->job->status));
return false;
}
2017-06-24 06:57:24 +02:00
set_time_limit(0);
2017-06-24 08:37:09 +02:00
Log::info(sprintf('Start with import job %s', $this->job->key));
2017-06-24 06:57:24 +02:00
$importObjects = $this->getImportObjects();
$this->lines = $importObjects->count();
// once done, use storage thing to actually store them:
2017-06-24 08:37:09 +02:00
Log::info(sprintf('Returned %d valid objects from file processor', $this->lines));
2017-06-24 06:57:24 +02:00
$storage = $this->storeObjects($importObjects);
// update job:
$this->job->status = 'finished';
$this->job->save();
$this->journals = $storage->journals;
$this->errors = $storage->errors;
// create tag, link tag to all journals:
$this->createImportTag();
2017-06-24 08:37:09 +02:00
Log::info(sprintf('Done with import job %s', $this->job->key));
2017-06-24 06:57:24 +02:00
2017-07-07 08:04:21 +02:00
2017-06-24 06:57:24 +02:00
return true;
}
2017-07-07 08:09:42 +02:00
/**
* @param ImportJob $job
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
}
2017-06-24 06:57:24 +02:00
/**
* @return Collection
*/
protected function getImportObjects(): Collection
{
$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
if ($this->job->status == 'configured') {
2017-06-22 21:50:10 +02:00
// set job as "running"...
$this->job->status = 'running';
$this->job->save();
Log::debug('Job is configured, start with run()');
$processor->run();
$objects = $processor->getObjects();
}
2017-06-24 06:57:24 +02:00
return $objects;
}
2017-06-24 06:57:24 +02:00
/**
*
*/
private function createImportTag(): Tag
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$repository->setUser($this->job->user);
$data = [
'tag' => trans('firefly.import_with_key', ['key' => $this->job->key]),
'date' => new Carbon,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data);
$extended = $this->job->extended_status;
$extended['tag'] = $tag->id;
$this->job->extended_status = $extended;
2017-06-21 20:04:35 +02:00
$this->job->save();
2017-06-24 06:57:24 +02:00
$this->journals->each(
function (TransactionJournal $journal) use ($tag) {
$journal->tags()->save($tag);
}
);
2017-06-22 21:50:10 +02:00
2017-06-24 06:57:24 +02:00
return $tag;
2017-06-22 21:50:10 +02:00
2017-06-24 06:57:24 +02:00
}
2017-06-24 06:57:24 +02:00
/**
* @param Collection $objects
*
* @return ImportStorage
*/
private function storeObjects(Collection $objects): ImportStorage
{
$storage = new ImportStorage;
$storage->setJob($this->job);
$storage->setDateFormat($this->job->configuration['date-format']);
$storage->setObjects($objects);
$storage->store();
2017-06-24 06:57:24 +02:00
return $storage;
}
2017-07-07 08:09:42 +02:00
}