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

111 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* FakeRoutine.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* 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;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-05-01 20:47:38 +02:00
use FireflyIII\Support\Import\Routine\Fake\StageAhoyHandler;
use FireflyIII\Support\Import\Routine\Fake\StageFinalHandler;
use FireflyIII\Support\Import\Routine\Fake\StageNewHandler;
2018-05-01 20:47:38 +02:00
use Log;
/**
* Class FakeRoutine
*/
class FakeRoutine implements RoutineInterface
{
/** @var ImportJob */
private $job;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* FakeRoutine constructor.
*/
public function __construct()
{
$this->repository = app(ImportJobRepositoryInterface::class);
}
/**
* Fake import routine has three stages:
*
2018-05-01 20:47:38 +02:00
* "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
{
2018-05-01 20:47:38 +02:00
Log::debug(sprintf('Now in run() for fake routine with status: %s', $this->job->status));
if ($this->job->status !== 'running') {
throw new FireflyException('This fake job should not be started.'); // @codeCoverageIgnore
2018-05-01 20:47:38 +02:00
}
switch ($this->job->stage) {
default:
throw new FireflyException(sprintf('Fake routine cannot handle stage "%s".', $this->job->stage)); // @codeCoverageIgnore
case 'new':
/** @var StageNewHandler $handler */
$handler = app(StageNewHandler::class);
$handler->run();
2018-05-01 20:47:38 +02:00
$this->repository->setStage($this->job, 'ahoy');
// set job finished this step:
$this->repository->setStatus($this->job, 'ready_to_run');
return;
case 'ahoy':
/** @var StageAhoyHandler $handler */
$handler = app(StageAhoyHandler::class);
2018-05-01 20:47:38 +02:00
$handler->run();
$this->repository->setStatus($this->job, 'need_job_config');
$this->repository->setStage($this->job, 'final');
break;
case 'final':
/** @var StageFinalHandler $handler */
$handler = app(StageFinalHandler::class);
2018-05-03 17:23:16 +02:00
$handler->setJob($this->job);
2018-05-01 20:47:38 +02:00
$transactions = $handler->getTransactions();
$this->repository->setStatus($this->job, 'provider_finished');
$this->repository->setStage($this->job, 'final');
2018-05-03 17:23:16 +02:00
$this->repository->setTransactions($this->job, $transactions);
}
}
/**
* @param ImportJob $job
*
* @return mixed
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
$this->repository->setUser($job->user);
}
}