2017-06-17 22:49:44 +02:00
|
|
|
<?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
|
2017-06-17 22:49:44 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2017-06-17 22:49:44 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Import\Routine;
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2017-06-17 22:49:44 +02:00
|
|
|
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;
|
2017-06-17 22:49:44 +02:00
|
|
|
use Log;
|
|
|
|
|
2017-12-16 19:47:14 +01:00
|
|
|
/**
|
|
|
|
* Class FileRoutine
|
2020-03-15 15:31:51 +01:00
|
|
|
* @deprecated
|
|
|
|
* @codeCoverageIgnore
|
2017-12-16 19:47:14 +01:00
|
|
|
*/
|
|
|
|
class FileRoutine implements RoutineInterface
|
2017-06-17 22:49:44 +02:00
|
|
|
{
|
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;
|
|
|
|
|
2017-06-17 22:49:44 +02:00
|
|
|
/**
|
2018-05-04 20:21:27 +02:00
|
|
|
* At the end of each run(), the import routine must set the job to the expected status.
|
2017-06-17 22:49:44 +02:00
|
|
|
*
|
2018-05-04 20:21:27 +02:00
|
|
|
* The final status of the routine must be "provider_finished".
|
2017-06-24 06:57:24 +02:00
|
|
|
*
|
2018-05-04 20:21:27 +02:00
|
|
|
* @throws FireflyException
|
2017-06-24 06:57:24 +02:00
|
|
|
*/
|
2018-05-04 20:21:27 +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) {
|
2018-05-12 19:09:34 +02:00
|
|
|
$this->repository->setStatus($this->importJob, 'running');
|
2018-05-10 20:05:02 +02:00
|
|
|
// 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);
|
2018-05-10 20:05:02 +02:00
|
|
|
$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);
|
2017-06-17 22:49:44 +02:00
|
|
|
}
|
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));
|
|
|
|
|
2018-05-10 20:05:02 +02:00
|
|
|
return app($class);
|
2018-05-06 20:42:30 +02:00
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|