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

193 lines
5.8 KiB
PHP
Raw Normal View History

<?php
/**
2017-12-16 19:47:14 +01:00
* FileRoutine.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Routine;
2017-06-24 06:57:24 +02:00
use Carbon\Carbon;
use DB;
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\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
2017-12-16 19:47:14 +01:00
/**
* Class FileRoutine
*/
class FileRoutine implements RoutineInterface
{
2017-11-15 12:25:49 +01:00
/** @var Collection */
2017-06-21 20:04:35 +02:00
public $errors;
2017-11-15 12:25:49 +01:00
/** @var Collection */
2017-06-22 21:50:10 +02:00
public $journals;
2017-06-21 20:04:35 +02:00
/** @var int */
public $lines = 0;
2017-11-15 12:25:49 +01:00
/** @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
{
2017-11-15 12:25:49 +01:00
if ('configured' !== $this->job->status) {
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);
Log::debug('Back in run()');
2017-06-24 06:57:24 +02:00
// update job:
$this->job->status = 'finished';
$this->job->save();
Log::debug('Updated job...');
2017-09-14 18:10:31 +02:00
Log::debug(sprintf('%d journals in $storage->journals', $storage->journals->count()));
2017-06-24 06:57:24 +02:00
$this->journals = $storage->journals;
$this->errors = $storage->errors;
Log::debug('Going to call createImportTag()');
2017-06-24 06:57:24 +02:00
// 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
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
2017-11-15 12:25:49 +01:00
if ('configured' === $this->job->status) {
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
{
Log::debug('Now in createImportTag()');
2017-11-08 16:04:55 +01:00
2017-11-15 10:52:29 +01:00
if ($this->journals->count() < 1) {
2017-11-08 16:04:55 +01:00
Log::info(sprintf('Will not create tag, %d journals imported.', $this->journals->count()));
2017-11-15 11:33:07 +01:00
2017-11-08 16:04:55 +01:00
return new Tag;
}
2017-06-24 06:57:24 +02:00
/** @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();
Log::debug(sprintf('Created tag #%d ("%s")', $tag->id, $tag->tag));
Log::debug('Looping journals...');
$journalIds = $this->journals->pluck('id')->toArray();
$tagId = $tag->id;
foreach ($journalIds as $journalId) {
Log::debug(sprintf('Linking journal #%d to tag #%d...', $journalId, $tagId));
DB::table('tag_transaction_journal')->insert(['transaction_journal_id' => $journalId, 'tag_id' => $tagId]);
}
Log::info(sprintf('Linked %d journals to tag #%d ("%s")', $this->journals->count(), $tag->id, $tag->tag));
2017-06-22 21:50:10 +02:00
2017-06-24 06:57:24 +02:00
return $tag;
}
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();
Log::info('Back in storeObjects()');
2017-06-24 06:57:24 +02:00
return $storage;
}
2017-07-07 08:09:42 +02:00
}