First working version of a working Spectre import.

This commit is contained in:
James Cole
2018-05-19 21:13:00 +02:00
parent 04953b5645
commit 2c206bba64
17 changed files with 552 additions and 172 deletions

View File

@@ -26,8 +26,13 @@ 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\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 AuthenticateConfig
@@ -50,6 +55,8 @@ class AuthenticateConfig implements SpectreJobConfig
*/
public function configurationComplete(): bool
{
Log::debug('AuthenticateConfig::configurationComplete() will always return false');
return false;
}
@@ -62,7 +69,8 @@ class AuthenticateConfig implements SpectreJobConfig
*/
public function configureJob(array $data): MessageBag
{
// does nothing
Log::debug('AuthenticateConfig::configureJob() will do nothing.');
return new MessageBag;
}
@@ -74,15 +82,22 @@ class AuthenticateConfig implements SpectreJobConfig
*/
public function getNextData(): array
{
Log::debug('Now in AuthenticateConfig::getNextData()');
// next data only makes sure the job is ready for the next stage.
$this->repository->setStatus($this->importJob, 'ready_to_run');
$this->repository->setStage($this->importJob, 'authenticated');
$config = $this->importJob->configuration;
$token = isset($config['token']) ? new Token($config['token']) : null;
if (null !== $token) {
Log::debug(sprintf('Return "%s" from token in config.', $token->getConnectUrl()));
return ['token-url' => $token->getConnectUrl()];
}
throw new FireflyException('The import routine cannot continue without a Spectre token. Apologies.');
Log::debug('No existing token, get a new one.');
// get a new token from Spectre.
$customer = $this->getCustomer();
$token = $this->getToken($customer);
return ['token-url' => $token->getConnectUrl()];
}
/**
@@ -106,4 +121,75 @@ class AuthenticateConfig implements SpectreJobConfig
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @return Customer
* @throws FireflyException
*/
private function getCustomer(): Customer
{
Log::debug('Now in AuthenticateConfig::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
{
Log::debug('Now in AuthenticateConfig::getExistingCustomer()');
$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.');
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
break;
}
}
return $customer;
}
/**
* @param Customer $customer
*
* @throws FireflyException
* @return Token
*/
private function getToken(Customer $customer): Token
{
Log::debug('Now in AuthenticateConfig::getToken()');
$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();
}
}

View File

@@ -23,10 +23,10 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\JobConfiguration\Spectre;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Support\MessageBag;
use Log;
class AuthenticatedConfigHandler implements SpectreJobConfig
{
@@ -42,6 +42,8 @@ class AuthenticatedConfigHandler implements SpectreJobConfig
*/
public function configurationComplete(): bool
{
Log::debug('AuthenticatedConfigHandler::configurationComplete() always returns true');
return true;
}
@@ -54,6 +56,8 @@ class AuthenticatedConfigHandler implements SpectreJobConfig
*/
public function configureJob(array $data): MessageBag
{
Log::debug('AuthenticatedConfigHandler::configurationComplete() always returns empty message bag');
return new MessageBag();
}
@@ -64,6 +68,8 @@ class AuthenticatedConfigHandler implements SpectreJobConfig
*/
public function getNextData(): array
{
Log::debug('AuthenticatedConfigHandler::getNextData() always returns []');
return [];
}
@@ -74,6 +80,8 @@ class AuthenticatedConfigHandler implements SpectreJobConfig
*/
public function getNextView(): string
{
Log::debug('AuthenticatedConfigHandler::getNextView() always returns ""');
return '';
}

View File

@@ -35,6 +35,7 @@ use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Spectre\Object\Account as SpectreAccount;
use FireflyIII\Services\Spectre\Object\Login;
use Illuminate\Support\MessageBag;
use Log;
/**
* Class ChooseAccount
@@ -60,10 +61,12 @@ class ChooseAccount implements SpectreJobConfig
*/
public function configurationComplete(): bool
{
Log::debug('Now in ChooseAccount::configurationComplete()');
$config = $this->importJob->configuration;
$importAccounts = $config['account_mapping'] ?? [];
$complete = \count($importAccounts) > 0 && $importAccounts !== [0 => 0];
if ($complete) {
Log::debug('Looks like user has mapped import accounts to Firefly III accounts', $importAccounts);
$this->repository->setStage($this->importJob, 'go-for-import');
}
@@ -79,6 +82,7 @@ class ChooseAccount implements SpectreJobConfig
*/
public function configureJob(array $data): MessageBag
{
Log::debug('Now in ChooseAccount::configureJob()', $data);
$config = $this->importJob->configuration;
$mapping = $data['account_mapping'] ?? [];
$final = [];
@@ -89,11 +93,12 @@ class ChooseAccount implements SpectreJobConfig
$final[$spectreId] = $accountId;
}
Log::debug('Final mapping is:', $final);
$messages = new MessageBag;
$config['account_mapping'] = $final;
$this->repository->setConfiguration($this->importJob, $config);
if (\count($final) === 0 || $final === [0 => 0]) {
if ($final === [0 => 0] || \count($final) === 0) {
$messages->add('count', trans('import.spectre_no_mapping'));
}
@@ -108,6 +113,7 @@ class ChooseAccount implements SpectreJobConfig
*/
public function getNextData(): array
{
Log::debug('Now in ChooseAccount::getnextData()');
$config = $this->importJob->configuration;
$accounts = $config['accounts'] ?? [];
if (\count($accounts) === 0) {
@@ -125,14 +131,17 @@ class ChooseAccount implements SpectreJobConfig
if (\count($logins) === 0) {
throw new FireflyException('It seems you have no configured logins in this import job. The import cannot continue.');
}
Log::debug(sprintf('Selected login to use is %d', $selected));
if ($selected === 0) {
$login = new Login($logins[0]);
Log::debug(sprintf('Will use login %d (%s %s)', $login->getId(), $login->getProviderName(), $login->getCountryCode()));
}
if ($selected !== 0) {
foreach ($logins as $loginArray) {
$loginId = $loginArray['id'] ?? -1;
if ($loginId === $selected) {
$login = new Login($loginArray);
Log::debug(sprintf('Will use login %d (%s %s)', $login->getId(), $login->getProviderName(), $login->getCountryCode()));
}
}
}

View File

@@ -55,10 +55,14 @@ class ChooseLoginHandler implements SpectreJobConfig
*/
public function configurationComplete(): bool
{
Log::debug('Now in ChooseLoginHandler::configurationComplete()');
$config = $this->importJob->configuration;
if(isset($config['selected-login'])) {
if (isset($config['selected-login'])) {
Log::debug('config[selected-login] is set, return true.');
return true;
}
Log::debug('config[selected-login] is not set, return false.');
return false;
}
@@ -73,24 +77,30 @@ class ChooseLoginHandler implements SpectreJobConfig
*/
public function configureJob(array $data): MessageBag
{
Log::debug('Now in ChooseLoginHandler::configureJob()');
$selectedLogin = (int)$data['spectre_login_id'];
$config = $this->importJob->configuration;
$config['selected-login'] = $selectedLogin;
$this->repository->setConfiguration($this->importJob, $config);
Log::debug(sprintf('The selected login by the user is #%d', $selectedLogin));
// if selected login is zero, create a new one.
if ($selectedLogin === 0) {
Log::debug('Login is zero, get a new customer + token and store it in config.');
$customer = $this->getCustomer();
// get a token for the user and redirect to next stage
$token = $this->getToken($customer);
$config['token'] = $token->toArray();
$token = $this->getToken($customer);
$config['customer'] = $customer->toArray();
$config['token'] = $token->toArray();
$this->repository->setConfiguration($this->importJob, $config);
// move job to correct stage to redirect to Spectre:
$this->repository->setStage($this->importJob, 'authenticate');
return new MessageBag;
}
$this->repository->setStage($this->importJob, 'authenticated');
return new MessageBag;
}
@@ -101,9 +111,11 @@ class ChooseLoginHandler implements SpectreJobConfig
*/
public function getNextData(): array
{
Log::debug('Now in ChooseLoginHandler::getNextData()');
$config = $this->importJob->configuration;
$data = ['logins' => []];
$logins = $config['all-logins'] ?? [];
Log::debug(sprintf('Count of logins in configuration is %d.', \count($logins)));
foreach ($logins as $login) {
$data['logins'][] = new Login($login);
}
@@ -158,7 +170,7 @@ class ChooseLoginHandler implements SpectreJobConfig
*/
private function getExistingCustomer(): ?Customer
{
Log::debug('Now in getExistingCustomer()');
Log::debug('Now in ChooseLoginHandler::getExistingCustomer()');
$preference = app('preferences')->getForUser($this->importJob->user, 'spectre_customer');
if (null !== $preference) {
Log::debug('Customer is in user configuration');
@@ -178,6 +190,7 @@ class ChooseLoginHandler implements SpectreJobConfig
if ('default_ff3_customer' === $current->getIdentifier()) {
$customer = $current;
Log::debug('Found the correct customer.');
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
break;
}
}
@@ -193,7 +206,7 @@ class ChooseLoginHandler implements SpectreJobConfig
*/
private function getToken(Customer $customer): Token
{
Log::debug('Now in ChooseLoginsHandler::getToken()');
Log::debug('Now in ChooseLoginHandler::ChooseLoginsHandler::getToken()');
$request = new CreateTokenRequest($this->importJob->user);
$request->setUri(route('import.job.status.index', [$this->importJob->key]));
$request->setCustomer($customer);

View File

@@ -84,6 +84,5 @@ class NewConfig implements SpectreJobConfig
*/
public function setImportJob(ImportJob $importJob): void
{
return;
}
}

View File

@@ -27,6 +27,11 @@ namespace FireflyIII\Support\Import\JobConfiguration\Spectre;
use FireflyIII\Models\ImportJob;
use Illuminate\Support\MessageBag;
/**
* Interface SpectreJobConfig
*
* @package FireflyIII\Support\Import\JobConfiguration\Spectre
*/
interface SpectreJobConfig
{
/**