. */ 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\Ynab\StageGetAccessHandler; use Log; /** * Class YnabRoutine */ class YnabRoutine implements RoutineInterface { /** @var ImportJob The import job */ private $importJob; /** @var ImportJobRepositoryInterface Import job repository */ 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 YNAB routine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage)); $valid = ['ready_to_run']; // should be only ready_to_run if (\in_array($this->importJob->status, $valid, true)) { // get access token from YNAB if ('get_access_token' === $this->importJob->stage) { // list all of the users accounts. $this->repository->setStatus($this->importJob, 'running'); /** @var StageGetAccessHandler $handler */ $handler = app(StageGetAccessHandler::class); $handler->setImportJob($this->importJob); $handler->run(); $this->repository->setStage($this->importJob, 'get_transactions'); return; } throw new FireflyException(sprintf('YNAB import routine cannot handle stage "%s"', $this->importJob->stage)); } throw new FireflyException(sprintf('YNAB import routine cannot handle status "%s"', $this->importJob->status)); } /** * Set the import job. * * @param ImportJob $importJob * * @return void */ public function setImportJob(ImportJob $importJob): void { $this->importJob = $importJob; $this->repository = app(ImportJobRepositoryInterface::class); $this->repository->setUser($importJob->user); } }