2018-05-19 03:44:33 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ChooseLoginHandler.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\Customer;
|
|
|
|
use FireflyIII\Services\Spectre\Object\Login;
|
|
|
|
use FireflyIII\Services\Spectre\Object\Token;
|
|
|
|
use FireflyIII\Services\Spectre\Request\CreateTokenRequest;
|
|
|
|
use FireflyIII\Services\Spectre\Request\ListCustomersRequest;
|
|
|
|
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
|
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ChooseLoginHandler
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
|
|
|
|
*/
|
|
|
|
class ChooseLoginHandler implements SpectreJobConfig
|
|
|
|
{
|
|
|
|
/** @var ImportJob */
|
|
|
|
private $importJob;
|
|
|
|
/** @var ImportJobRepositoryInterface */
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true when this stage is complete.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function configurationComplete(): bool
|
|
|
|
{
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Now in ChooseLoginHandler::configurationComplete()');
|
2018-05-19 03:44:33 -05:00
|
|
|
$config = $this->importJob->configuration;
|
2018-05-19 14:13:00 -05:00
|
|
|
if (isset($config['selected-login'])) {
|
|
|
|
Log::debug('config[selected-login] is set, return true.');
|
|
|
|
|
2018-05-19 03:44:33 -05:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('config[selected-login] is not set, return false.');
|
2018-05-19 03:44:33 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the job configuration.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return MessageBag
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function configureJob(array $data): MessageBag
|
|
|
|
{
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Now in ChooseLoginHandler::configureJob()');
|
2018-05-19 03:44:33 -05:00
|
|
|
$selectedLogin = (int)$data['spectre_login_id'];
|
|
|
|
$config = $this->importJob->configuration;
|
|
|
|
$config['selected-login'] = $selectedLogin;
|
|
|
|
$this->repository->setConfiguration($this->importJob, $config);
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug(sprintf('The selected login by the user is #%d', $selectedLogin));
|
2018-05-19 03:44:33 -05:00
|
|
|
|
|
|
|
// if selected login is zero, create a new one.
|
|
|
|
if ($selectedLogin === 0) {
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Login is zero, get a new customer + token and store it in config.');
|
2018-05-19 03:44:33 -05:00
|
|
|
$customer = $this->getCustomer();
|
|
|
|
// get a token for the user and redirect to next stage
|
2018-05-19 14:13:00 -05:00
|
|
|
$token = $this->getToken($customer);
|
|
|
|
$config['customer'] = $customer->toArray();
|
|
|
|
$config['token'] = $token->toArray();
|
2018-05-19 03:44:33 -05:00
|
|
|
$this->repository->setConfiguration($this->importJob, $config);
|
|
|
|
// move job to correct stage to redirect to Spectre:
|
|
|
|
$this->repository->setStage($this->importJob, 'authenticate');
|
2018-05-19 14:13:00 -05:00
|
|
|
|
2018-05-19 03:44:33 -05:00
|
|
|
return new MessageBag;
|
|
|
|
|
|
|
|
}
|
|
|
|
$this->repository->setStage($this->importJob, 'authenticated');
|
2018-05-19 14:13:00 -05:00
|
|
|
|
2018-05-19 03:44:33 -05:00
|
|
|
return new MessageBag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data for config view.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getNextData(): array
|
|
|
|
{
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Now in ChooseLoginHandler::getNextData()');
|
2018-05-19 03:44:33 -05:00
|
|
|
$config = $this->importJob->configuration;
|
|
|
|
$data = ['logins' => []];
|
|
|
|
$logins = $config['all-logins'] ?? [];
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug(sprintf('Count of logins in configuration is %d.', \count($logins)));
|
2018-05-19 03:44:33 -05:00
|
|
|
foreach ($logins as $login) {
|
|
|
|
$data['logins'][] = new Login($login);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the view for this stage.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNextView(): string
|
|
|
|
{
|
|
|
|
return 'import.spectre.choose-login';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Customer
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function getCustomer(): Customer
|
|
|
|
{
|
|
|
|
Log::debug('Now in stageNewHandler::getCustomer()');
|
|
|
|
$customer = $this->getExistingCustomer();
|
|
|
|
if (null === $customer) {
|
|
|
|
Log::debug('The customer is NULL, will fire a newCustomerRequest.');
|
|
|
|
$newCustomerRequest = new NewCustomerRequest($this->importJob->user);
|
|
|
|
$customer = $newCustomerRequest->getCustomer();
|
|
|
|
|
|
|
|
}
|
|
|
|
Log::debug('The customer is not null.');
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Customer|null
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function getExistingCustomer(): ?Customer
|
|
|
|
{
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Now in ChooseLoginHandler::getExistingCustomer()');
|
2018-05-19 03:44:33 -05:00
|
|
|
$preference = app('preferences')->getForUser($this->importJob->user, 'spectre_customer');
|
|
|
|
if (null !== $preference) {
|
|
|
|
Log::debug('Customer is in user configuration');
|
|
|
|
$customer = new Customer($preference->data);
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
}
|
|
|
|
Log::debug('Customer is not in user config');
|
|
|
|
$customer = null;
|
|
|
|
$getCustomerRequest = new ListCustomersRequest($this->importJob->user);
|
|
|
|
$getCustomerRequest->call();
|
|
|
|
$customers = $getCustomerRequest->getCustomers();
|
|
|
|
|
|
|
|
Log::debug(sprintf('Found %d customer(s)', \count($customers)));
|
|
|
|
/** @var Customer $current */
|
|
|
|
foreach ($customers as $current) {
|
|
|
|
if ('default_ff3_customer' === $current->getIdentifier()) {
|
|
|
|
$customer = $current;
|
|
|
|
Log::debug('Found the correct customer.');
|
2018-05-19 14:13:00 -05:00
|
|
|
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
|
2018-05-19 03:44:33 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Customer $customer
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
* @return Token
|
|
|
|
*/
|
|
|
|
private function getToken(Customer $customer): Token
|
|
|
|
{
|
2018-05-19 14:13:00 -05:00
|
|
|
Log::debug('Now in ChooseLoginHandler::ChooseLoginsHandler::getToken()');
|
2018-05-19 03:44:33 -05:00
|
|
|
$request = new CreateTokenRequest($this->importJob->user);
|
|
|
|
$request->setUri(route('import.job.status.index', [$this->importJob->key]));
|
|
|
|
$request->setCustomer($customer);
|
|
|
|
$request->call();
|
|
|
|
Log::debug('Call to get token is finished');
|
|
|
|
|
|
|
|
return $request->getToken();
|
|
|
|
}
|
|
|
|
}
|