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

107 lines
3.9 KiB
PHP
Raw Normal View History

2018-03-10 07:16:38 +01:00
<?php
/**
* BunqRoutine.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-23 12:36:12 +02:00
use FireflyIII\Support\Import\Routine\Bunq\StageImportDataHandler;
use FireflyIII\Support\Import\Routine\Bunq\StageNewHandler;
2018-03-10 07:16:38 +01:00
use Log;
/**
* Class BunqRoutine
*/
class BunqRoutine implements RoutineInterface
{
2018-07-22 15:08:56 +02:00
/** @var ImportJob The import job */
2018-05-23 12:36:12 +02:00
private $importJob;
2018-07-22 15:08:56 +02:00
/** @var ImportJobRepositoryInterface Import job repository */
2018-05-23 12:36:12 +02:00
private $repository;
2018-03-10 07:16:38 +01:00
/**
* At the end of each run(), the import routine must set the job to the expected status.
2018-03-24 10:35:42 +01:00
*
* The final status of the routine must be "provider_finished".
2018-03-24 18:55:02 +01:00
*
2018-03-10 20:25:42 +01:00
* @throws FireflyException
*/
public function run(): void
2018-03-10 20:25:42 +01:00
{
2018-07-29 07:30:06 +02:00
Log::debug(sprintf('Now in BunqRoutine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
2018-05-23 12:36:12 +02:00
$valid = ['ready_to_run']; // should be only ready_to_run
if (\in_array($this->importJob->status, $valid, true)) {
switch ($this->importJob->stage) {
default:
2018-07-29 07:30:06 +02:00
throw new FireflyException(sprintf('BunqRoutine cannot handle stage "%s".', $this->importJob->stage)); // @codeCoverageIgnore
2018-05-23 12:36:12 +02:00
case 'new':
// list all of the users accounts.
$this->repository->setStatus($this->importJob, 'running');
/** @var StageNewHandler $handler */
$handler = app(StageNewHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
// make user choose accounts to import from.
$this->repository->setStage($this->importJob, 'choose-accounts');
$this->repository->setStatus($this->importJob, 'need_job_config');
return;
2018-05-23 12:36:12 +02:00
case 'go-for-import':
// list all of the users accounts.
$this->repository->setStatus($this->importJob, 'running');
/** @var StageImportDataHandler $handler */
$handler = app(StageImportDataHandler::class);
$handler->setImportJob($this->importJob);
$handler->run();
$transactions = $handler->getTransactions();
$this->repository->setTransactions($this->importJob, $transactions);
$this->repository->setStatus($this->importJob, 'provider_finished');
$this->repository->setStage($this->importJob, 'final');
return;
2018-05-23 12:36:12 +02:00
}
}
throw new FireflyException(sprintf('bunq import routine cannot handle status "%s"', $this->importJob->status)); // @codeCoverageIgnore
2018-03-10 20:25:42 +01:00
}
2018-05-12 15:50:01 +02:00
2018-03-10 07:16:38 +01:00
/**
2018-07-22 15:08:56 +02:00
* Set the import job.
*
2018-05-12 15:50:01 +02:00
* @param ImportJob $importJob
*
* @return void
2018-03-10 07:16:38 +01:00
*/
2018-05-12 15:50:01 +02:00
public function setImportJob(ImportJob $importJob): void
2018-03-10 07:16:38 +01:00
{
2018-05-23 12:36:12 +02:00
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
2018-03-10 07:16:38 +01:00
}
2018-05-23 12:36:12 +02:00
}