. */ 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\Fake\StageAhoyHandler; use FireflyIII\Support\Import\Routine\Fake\StageFinalHandler; use FireflyIII\Support\Import\Routine\Fake\StageNewHandler; use Log; /** * Class FakeRoutine */ class FakeRoutine implements RoutineInterface { /** @var ImportJob */ private $importJob; /** @var ImportJobRepositoryInterface */ private $repository; /** * Fake import routine has three stages: * * "new": will quietly log gibberish for 15 seconds, then switch to stage "ahoy". * will also set status to "ready_to_run" so it will arrive here again. * "ahoy": will log some nonsense and then drop job into status:"need_job_config" to force it back to the job config routine. * "final": will do some logging, sleep for 10 seconds and then finish. Generates 5 random transactions. * * @return bool * @throws FireflyException */ public function run(): void { Log::debug(sprintf('Now in run() for fake routine with status: %s', $this->importJob->status)); if ($this->importJob->status !== 'running') { throw new FireflyException('This fake job should not be started.'); // @codeCoverageIgnore } switch ($this->importJob->stage) { default: throw new FireflyException(sprintf('Fake routine cannot handle stage "%s".', $this->importJob->stage)); // @codeCoverageIgnore case 'new': /** @var StageNewHandler $handler */ $handler = app(StageNewHandler::class); $handler->run(); $this->repository->setStage($this->importJob, 'ahoy'); // set job finished this step: $this->repository->setStatus($this->importJob, 'ready_to_run'); return; case 'ahoy': /** @var StageAhoyHandler $handler */ $handler = app(StageAhoyHandler::class); $handler->run(); $this->repository->setStatus($this->importJob, 'need_job_config'); $this->repository->setStage($this->importJob, 'final'); break; case 'final': /** @var StageFinalHandler $handler */ $handler = app(StageFinalHandler::class); $handler->setJob($this->importJob); $transactions = $handler->getTransactions(); $this->repository->setStatus($this->importJob, 'provider_finished'); $this->repository->setStage($this->importJob, 'final'); $this->repository->setTransactions($this->importJob, $transactions); } } /** * @param ImportJob $job * * @return */ public function setJob(ImportJob $job): void { $this->importJob = $job; $this->repository = app(ImportJobRepositoryInterface::class); $this->repository->setUser($job->user); } }