. */ declare(strict_types=1); namespace FireflyIII\Import\Routine; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\ImportJob; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Support\Import\Routine\File\FileProcessorInterface; use Log; /** * Class FileRoutine */ class FileRoutine implements RoutineInterface { /** @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". * * @throws FireflyException */ public function run(): void { 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; } throw new FireflyException(sprintf('Import routine cannot handle stage "%s"', $this->importJob->stage)); // @codeCoverageIgnore } /** * @param ImportJob $job * * @return void */ public function setJob(ImportJob $job): void { $this->importJob = $job; $this->repository = app(ImportJobRepositoryInterface::class); $this->repository->setUser($job->user); } /** * 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); } }