firefly-iii/app/Import/Configuration/SpectreConfigurator.php

234 lines
7.0 KiB
PHP
Raw Normal View History

2017-12-09 12:13:00 -06:00
<?php
/**
* SpectreConfigurator.php
* Copyright (c) 2017 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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-12-09 12:13:00 -06:00
*/
declare(strict_types=1);
namespace FireflyIII\Import\Configuration;
2017-12-09 12:13:00 -06:00
2018-01-03 12:17:30 -06:00
use FireflyIII\Exceptions\FireflyException;
2017-12-09 12:13:00 -06:00
use FireflyIII\Models\ImportJob;
2018-01-10 11:18:49 -06:00
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-01-03 12:17:30 -06:00
use FireflyIII\Support\Import\Configuration\Spectre\HaveAccounts;
2018-01-02 08:17:31 -06:00
use Log;
2017-12-09 12:13:00 -06:00
/**
* Class SpectreConfigurator.
*/
class SpectreConfigurator implements ConfiguratorInterface
{
/** @var ImportJob */
private $job;
2018-01-10 11:18:49 -06:00
/** @var ImportJobRepositoryInterface */
private $repository;
2017-12-09 12:13:00 -06:00
/** @var string */
private $warning = '';
/**
* ConfiguratorInterface constructor.
*/
public function __construct()
{
}
/**
* Store any data from the $data array into the job.
*
* @param array $data
*
* @return bool
2018-01-10 11:18:49 -06:00
* @throws FireflyException
2017-12-09 12:13:00 -06:00
*/
public function configureJob(array $data): bool
{
2018-01-10 11:18:49 -06:00
if (is_null($this->job)) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
2018-01-03 12:17:30 -06:00
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
switch ($stage) {
case 'have-accounts':
/** @var HaveAccounts $class */
$class = app(HaveAccounts::class);
$class->setJob($this->job);
$class->storeConfiguration($data);
// update job for next step and set to "configured".
2018-01-10 11:18:49 -06:00
$config = $this->getConfig();
$config['stage'] = 'have-account-mapping';
$this->repository->setConfiguration($this->job, $config);
2018-01-03 12:17:30 -06:00
return true;
default:
throw new FireflyException(sprintf('Cannot store configuration when job is in state "%s"', $stage));
break;
}
2017-12-09 12:13:00 -06:00
}
/**
* Return the data required for the next step in the job configuration.
*
* @return array
2018-01-10 11:18:49 -06:00
* @throws FireflyException
2017-12-09 12:13:00 -06:00
*/
public function getNextData(): array
{
2018-01-10 11:18:49 -06:00
if (is_null($this->job)) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
2018-01-25 11:41:27 -06:00
$config = $this->getConfig();
$stage = $config['stage'] ?? 'initial';
2018-01-03 12:17:30 -06:00
Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
switch ($stage) {
case 'has-token':
// simply redirect to Spectre.
$config['is-redirected'] = true;
$config['stage'] = 'user-logged-in';
$status = 'configured';
2018-01-10 11:18:49 -06:00
// update config and status:
$this->repository->setConfiguration($this->job, $config);
$this->repository->setStatus($this->job, $status);
return $this->repository->getConfiguration($this->job);
2018-01-03 12:17:30 -06:00
case 'have-accounts':
/** @var HaveAccounts $class */
$class = app(HaveAccounts::class);
$class->setJob($this->job);
$data = $class->getData();
return $data;
default:
return [];
}
2017-12-09 12:13:00 -06:00
}
/**
* @return string
2018-01-10 11:18:49 -06:00
* @throws FireflyException
2017-12-09 12:13:00 -06:00
*/
public function getNextView(): string
{
2018-01-10 11:18:49 -06:00
if (is_null($this->job)) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
2018-01-03 12:17:30 -06:00
Log::debug(sprintf('in getNextView(), for stage "%s".', $stage));
switch ($stage) {
case 'has-token':
// redirect to Spectre.
2018-01-10 09:49:32 -06:00
Log::info('User is being redirected to Spectre.');
2018-01-10 11:18:49 -06:00
2018-01-03 12:17:30 -06:00
return 'import.spectre.redirect';
case 'have-accounts':
return 'import.spectre.accounts';
default:
return '';
2018-01-02 08:17:31 -06:00
2018-01-03 12:17:30 -06:00
}
2017-12-09 12:13:00 -06:00
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return $this->warning;
}
/**
* @return bool
2018-01-10 11:18:49 -06:00
* @throws FireflyException
2017-12-09 12:13:00 -06:00
*/
public function isJobConfigured(): bool
{
2018-01-10 11:18:49 -06:00
if (is_null($this->job)) {
throw new FireflyException('Cannot call configureJob() without a job.');
}
$stage = $this->getConfig()['stage'] ?? 'initial';
2018-01-03 12:17:30 -06:00
Log::debug(sprintf('in isJobConfigured(), for stage "%s".', $stage));
switch ($stage) {
case 'has-token':
case 'have-accounts':
Log::debug('isJobConfigured returns false');
return false;
default:
Log::debug('isJobConfigured returns true');
return true;
2017-12-09 12:13:00 -06:00
}
}
/**
* @param ImportJob $job
*/
2018-01-10 11:18:49 -06:00
public function setJob(ImportJob $job): void
2017-12-09 12:13:00 -06:00
{
2018-01-10 11:18:49 -06:00
// make repository
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($job->user);
// set default config:
$defaultConfig = [
2018-01-03 12:17:30 -06:00
'has-token' => false,
'token' => '',
'token-expires' => 0,
'token-url' => '',
'is-redirected' => false,
'customer' => null,
'login' => null,
'stage' => 'initial',
'accounts' => '',
'accounts-mapped' => '',
'auto-start' => true,
2018-01-10 11:18:49 -06:00
'apply-rules' => true,
'match-bills' => false,
2017-12-28 11:39:30 -06:00
];
2018-01-10 11:18:49 -06:00
$currentConfig = $this->repository->getConfiguration($job);
$finalConfig = array_merge($defaultConfig, $currentConfig);
2018-01-03 00:35:29 -06:00
2018-01-10 11:18:49 -06:00
// set default extended status:
$extendedStatus = $this->repository->getExtendedStatus($job);
$extendedStatus['steps'] = 6;
2017-12-28 11:39:30 -06:00
2018-01-10 11:18:49 -06:00
// save to job:
$job = $this->repository->setConfiguration($job, $finalConfig);
$job = $this->repository->setExtendedStatus($job, $extendedStatus);
2017-12-09 12:13:00 -06:00
$this->job = $job;
2018-01-10 11:18:49 -06:00
return;
}
/**
* Shorthand method.
*
* @return array
*/
private function getConfig(): array
{
return $this->repository->getConfiguration($this->job);
2017-12-09 12:13:00 -06:00
}
}