2018-05-19 10:44:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AuthenticateConfig.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\Support\Import\JobConfiguration\Spectre;
|
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
|
|
|
use FireflyIII\Services\Spectre\Object\Token;
|
2018-05-21 17:28:09 +02:00
|
|
|
use FireflyIII\Support\Import\Information\GetSpectreCustomerTrait;
|
|
|
|
use FireflyIII\Support\Import\Information\GetSpectreTokenTrait;
|
2018-05-19 10:44:33 +02:00
|
|
|
use Illuminate\Support\MessageBag;
|
2018-05-19 21:13:00 +02:00
|
|
|
use Log;
|
2018-05-19 10:44:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AuthenticateConfig
|
|
|
|
*
|
|
|
|
*/
|
2018-05-21 09:40:19 +02:00
|
|
|
class DoAuthenticateHandler implements SpectreConfigurationInterface
|
2018-05-19 10:44:33 +02:00
|
|
|
{
|
2018-05-21 17:28:09 +02:00
|
|
|
use GetSpectreTokenTrait, GetSpectreCustomerTrait;
|
2018-05-19 10:44:33 +02:00
|
|
|
/** @var ImportJob */
|
|
|
|
private $importJob;
|
|
|
|
/** @var ImportJobRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
2018-05-21 17:28:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-05-19 10:44:33 +02:00
|
|
|
* Return true when this stage is complete.
|
|
|
|
*
|
|
|
|
* always returns false.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function configurationComplete(): bool
|
|
|
|
{
|
2018-05-21 17:28:09 +02:00
|
|
|
Log::debug('DoAuthenticateHandler::configurationComplete() will always return false');
|
2018-05-19 21:13:00 +02:00
|
|
|
|
2018-05-19 10:44:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-21 17:28:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-05-19 10:44:33 +02:00
|
|
|
* Store the job configuration.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return MessageBag
|
|
|
|
*/
|
|
|
|
public function configureJob(array $data): MessageBag
|
|
|
|
{
|
2018-05-21 17:28:09 +02:00
|
|
|
Log::debug('DoAuthenticateHandler::configureJob() will do nothing.');
|
2018-05-19 21:13:00 +02:00
|
|
|
|
2018-05-19 10:44:33 +02:00
|
|
|
return new MessageBag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data for config view.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getNextData(): array
|
|
|
|
{
|
2018-05-21 17:28:09 +02:00
|
|
|
Log::debug('Now in DoAuthenticateHandler::getNextData()');
|
|
|
|
|
|
|
|
// getNextData() only makes sure the job is ready for the next stage.
|
2018-05-19 10:44:33 +02:00
|
|
|
$this->repository->setStatus($this->importJob, 'ready_to_run');
|
|
|
|
$this->repository->setStage($this->importJob, 'authenticated');
|
2018-05-21 17:28:09 +02:00
|
|
|
|
|
|
|
// get token from configuration:
|
2018-05-19 10:44:33 +02:00
|
|
|
$config = $this->importJob->configuration;
|
|
|
|
$token = isset($config['token']) ? new Token($config['token']) : null;
|
2018-05-19 21:13:00 +02:00
|
|
|
|
2018-05-21 17:28:09 +02:00
|
|
|
if (null === $token) {
|
|
|
|
// get a new one from Spectre:
|
|
|
|
Log::debug('No existing token, get a new one.');
|
|
|
|
// get a new token from Spectre.
|
|
|
|
$customer = $this->getCustomer($this->importJob);
|
|
|
|
$token = $this->getToken($this->importJob, $customer);
|
2018-05-19 10:44:33 +02:00
|
|
|
}
|
2018-05-21 17:28:09 +02:00
|
|
|
|
2018-05-19 21:13:00 +02:00
|
|
|
return ['token-url' => $token->getConnectUrl()];
|
2018-05-19 10:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-21 17:28:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-05-19 10:44:33 +02:00
|
|
|
* Get the view for this stage.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNextView(): string
|
|
|
|
{
|
|
|
|
return 'import.spectre.redirect';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-21 17:28:09 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-05-19 10:44:33 +02:00
|
|
|
* Set the import job.
|
|
|
|
*
|
|
|
|
* @param ImportJob $importJob
|
|
|
|
*/
|
|
|
|
public function setImportJob(ImportJob $importJob): void
|
|
|
|
{
|
|
|
|
$this->importJob = $importJob;
|
|
|
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
|
|
|
$this->repository->setUser($importJob->user);
|
|
|
|
}
|
|
|
|
}
|