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

97 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
2017-12-16 12:47:14 -06:00
* FileRoutine.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05: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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Routine;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
2018-01-10 11:18:49 -06:00
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-05-06 13:42:30 -05:00
use FireflyIII\Support\Import\Routine\File\FileProcessorInterface;
use Log;
2017-12-16 12:47:14 -06:00
/**
* Class FileRoutine
*/
class FileRoutine implements RoutineInterface
{
2018-05-06 13:42:30 -05:00
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
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-23 23:57:24 -05:00
*
* @throws FireflyException
2017-06-23 23:57:24 -05:00
*/
public function run(): void
2018-01-10 11:18:49 -06:00
{
2018-05-06 13:42:30 -05:00
Log::debug(sprintf('Now in run() for file routine with status: %s', $this->importJob->status));
if ($this->importJob->status !== 'running') {
throw new FireflyException('This file import job should not be started.'); // @codeCoverageIgnore
}
if ($this->importJob->stage === 'ready_to_run') {
// get processor, depending on file type
// is just CSV for now.
$processor = $this->getProcessor();
$processor->setJob($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 13:42:30 -05:00
}
throw new FireflyException(sprintf('Import routine cannot handle stage "%s"', $this->importJob->stage)); // @codeCoverageIgnore
2018-01-10 11:18:49 -06:00
}
/**
* @param ImportJob $job
2017-06-23 23:57:24 -05:00
*
2018-05-06 13:42:30 -05:00
* @return void
2017-06-23 23:57:24 -05:00
*/
2018-05-06 13:42:30 -05:00
public function setJob(ImportJob $job): void
2017-06-23 23:57:24 -05:00
{
2018-05-06 13:42:30 -05:00
$this->importJob = $job;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($job->user);
}
2018-05-06 13:42:30 -05: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 13:42:30 -05:00
}
2017-07-07 01:09:42 -05:00
}