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

99 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
2017-12-16 19:47:14 +01:00
* FileRoutine.php
2020-02-05 05:53:12 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
2018-01-10 18:18:49 +01:00
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-05-06 20:42:30 +02:00
use FireflyIII\Support\Import\Routine\File\FileProcessorInterface;
use Log;
2017-12-16 19:47:14 +01:00
/**
* Class FileRoutine
* @deprecated
* @codeCoverageIgnore
2017-12-16 19:47:14 +01:00
*/
class FileRoutine implements RoutineInterface
{
2018-07-22 15:08:56 +02:00
/** @var ImportJob The import job */
2018-05-06 20:42:30 +02:00
private $importJob;
2018-07-22 15:08:56 +02:00
/** @var ImportJobRepositoryInterface Import job repository */
2018-05-06 20:42:30 +02:00
private $repository;
/**
* At the end of each run(), the import routine must set the job to the expected status.
*
* The final status of the routine must be "provider_finished".
2017-06-24 06:57:24 +02:00
*
* @throws FireflyException
2017-06-24 06:57:24 +02:00
*/
public function run(): void
2018-01-10 18:18:49 +01:00
{
2018-05-06 20:42:30 +02:00
Log::debug(sprintf('Now in run() for file routine with status: %s', $this->importJob->status));
2018-07-09 20:42:16 +02:00
if ('ready_to_run' === $this->importJob->status) {
$this->repository->setStatus($this->importJob, 'running');
// get processor, depending on file type
// is just CSV for now.
$processor = $this->getProcessor();
2018-05-12 15:50:01 +02:00
$processor->setImportJob($this->importJob);
$transactions = $processor->run();
$this->repository->setStatus($this->importJob, 'provider_finished');
$this->repository->setStage($this->importJob, 'final');
$this->repository->setTransactions($this->importJob, $transactions);
return;
2018-05-06 20:42:30 +02:00
}
2018-05-13 09:01:10 +02:00
throw new FireflyException(sprintf('Import routine cannot handle status "%s"', $this->importJob->status)); // @codeCoverageIgnore
2018-01-10 18:18:49 +01:00
}
/**
2018-07-22 15:08:56 +02:00
* Set the import job.
*
2018-05-12 15:50:01 +02:00
* @param ImportJob $importJob
2017-06-24 06:57:24 +02:00
*
2018-05-06 20:42:30 +02:00
* @return void
2017-06-24 06:57:24 +02:00
*/
2018-05-12 15:50:01 +02:00
public function setImportJob(ImportJob $importJob): void
2017-06-24 06:57:24 +02:00
{
2018-05-12 15:50:01 +02:00
$this->importJob = $importJob;
2018-05-06 20:42:30 +02:00
$this->repository = app(ImportJobRepositoryInterface::class);
2018-05-12 16:04:46 +02:00
$this->repository->setUser($importJob->user);
}
2018-05-06 20:42:30 +02:00
/**
* Return the appropriate file routine handler for
* the file type of the job.
*
* @return FileProcessorInterface
*/
private function getProcessor(): FileProcessorInterface
{
$config = $this->repository->getConfiguration($this->importJob);
$type = $config['file-type'] ?? 'csv';
$class = config(sprintf('import.options.file.processors.%s', $type));
return app($class);
2018-05-06 20:42:30 +02:00
}
2017-07-07 08:09:42 +02:00
}