mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-03 20:14:31 +00:00
Lots of new code for the Spectre routine.
This commit is contained in:
153
app/Import/JobConfiguration/SpectreJobConfiguration.php
Normal file
153
app/Import/JobConfiguration/SpectreJobConfiguration.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* SpectreJobConfiguration.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\JobConfiguration;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\AuthenticateConfig;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\AuthenticatedConfigHandler;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseAccount;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\ChooseLoginHandler;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\NewConfig;
|
||||
use FireflyIII\Support\Import\JobConfiguration\Spectre\SpectreJobConfig;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
/**
|
||||
* Class SpectreJobConfiguration
|
||||
*
|
||||
* @package FireflyIII\Import\JobConfiguration
|
||||
*/
|
||||
class SpectreJobConfiguration implements JobConfigurationInterface
|
||||
{
|
||||
/** @var SpectreJobConfig */
|
||||
private $handler;
|
||||
/** @var ImportJob */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* ConfiguratorInterface constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the initial configuration for this job is complete.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function configurationComplete(): bool
|
||||
{
|
||||
return $this->handler->configurationComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store any data from the $data array into the job. Anything in the message bag will be flashed
|
||||
* as an error to the user, regardless of its content.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
return $this->handler->configureJob($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data required for the next step in the job configuration.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
return $this->handler->getNextData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the view of the next step in the job configuration.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNextView(): string
|
||||
{
|
||||
return $this->handler->getNextView();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $importJob
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($importJob->user);
|
||||
$this->handler = $this->getHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SpectreJobConfig
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getHandler(): SpectreJobConfig
|
||||
{
|
||||
$handler = null;
|
||||
switch ($this->importJob->stage) {
|
||||
case 'new':
|
||||
$handler = app(NewConfig::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
break;
|
||||
case 'authenticate':
|
||||
/** @var SpectreJobConfig $handler */
|
||||
$handler = app(AuthenticateConfig::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
break;
|
||||
case 'choose-login':
|
||||
/** @var SpectreJobConfig $handler */
|
||||
$handler = app(ChooseLoginHandler::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
break;
|
||||
case 'authenticated':
|
||||
/** @var AuthenticatedConfigHandler $handler */
|
||||
$handler = app(AuthenticatedConfigHandler::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
break;
|
||||
case 'choose-account':
|
||||
/** @var ChooseAccount $handler */
|
||||
$handler = app(ChooseAccount::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException(sprintf('Firefly III cannot create a configuration handler for stage "%s"', $this->importJob->stage));
|
||||
|
||||
}
|
||||
|
||||
return $handler;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,9 @@ namespace FireflyIII\Import\Routine;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\Spectre\ImportDataHandler;
|
||||
use FireflyIII\Support\Import\Routine\Spectre\ManageLoginsHandler;
|
||||
use FireflyIII\Support\Import\Routine\Spectre\StageAuthenticatedHandler;
|
||||
use FireflyIII\Support\Import\Routine\Spectre\StageNewHandler;
|
||||
|
||||
/**
|
||||
@@ -54,21 +57,59 @@ class SpectreRoutine implements RoutineInterface
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$valid = ['ready_to_run','error']; // should be only ready_to_run
|
||||
if(in_array($this->importJob->status, $valid)) {
|
||||
$valid = ['ready_to_run']; // should be only ready_to_run
|
||||
if (in_array($this->importJob->status, $valid)) {
|
||||
switch ($this->importJob->stage) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('SpectreRoutine cannot handle stage "%s".', $this->importJob->stage));
|
||||
case 'new':
|
||||
case 'authenticate':
|
||||
/** @var StageNewHandler $handler */
|
||||
$handler = app(StageNewHandler::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
$handler->run();
|
||||
$this->repository->setStage($this->importJob, 'authenticate');
|
||||
var_dump($this->repository->getConfiguration($this->importJob));
|
||||
exit;
|
||||
$this->repository->setStage($this->importJob, 'manage-logins');
|
||||
break;
|
||||
case 'authenticate':
|
||||
// set job to require config.
|
||||
$this->repository->setStatus($this->importJob, 'need_job_config');
|
||||
|
||||
return;
|
||||
case 'manage-logins':
|
||||
// list all of the users logins.
|
||||
$handler = new ManageLoginsHandler;
|
||||
$handler->setImportJob($this->importJob);
|
||||
$handler->run();
|
||||
|
||||
// if count logins is zero, go to authenticate stage
|
||||
if ($handler->countLogins === 0) {
|
||||
$this->repository->setStage($this->importJob, 'authenticate');
|
||||
$this->repository->setStatus($this->importJob, 'ready_to_run');
|
||||
|
||||
return;
|
||||
}
|
||||
// or return to config to select login.
|
||||
$this->repository->setStage($this->importJob, 'choose-login');
|
||||
$this->repository->setStatus($this->importJob, 'need_job_config');
|
||||
break;
|
||||
case 'authenticated':
|
||||
// get accounts from login, store in job.
|
||||
$handler = new StageAuthenticatedHandler;
|
||||
$handler->setImportJob($this->importJob);
|
||||
$handler->run();
|
||||
|
||||
// return to config to select account(s).
|
||||
$this->repository->setStage($this->importJob, 'choose-account');
|
||||
$this->repository->setStatus($this->importJob, 'need_job_config');
|
||||
break;
|
||||
case 'go-for-import':
|
||||
// user has chosen account mapping. Should now be ready to import data.
|
||||
//$this->repository->setStatus($this->importJob, 'running');
|
||||
//$this->repository->setStage($this->importJob, 'do_import');
|
||||
$handler = new ImportDataHandler;
|
||||
$handler->setImportJob($this->importJob);
|
||||
$handler->run();
|
||||
$this->repository->setStatus($this->importJob, 'provider_finished');
|
||||
$this->repository->setStage($this->importJob, 'final');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user