Lots of new code for the Spectre routine.

This commit is contained in:
James Cole
2018-05-19 10:44:33 +02:00
parent 1732ce63f3
commit 04953b5645
26 changed files with 1729 additions and 242 deletions

View File

@@ -0,0 +1,62 @@
<?php
/**
* ImportDataHandler.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\Routine\Spectre;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class ImportDataHandler
*
* @package FireflyIII\Support\Import\Routine\Spectre
*/
class ImportDataHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
public function run()
{
die('here we are');
}
/**
* @param ImportJob $importJob
*
* @return void
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@@ -0,0 +1,133 @@
<?php
/**
* ManageLoginsHandler.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\Routine\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\Request\ListCustomersRequest;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use Log;
class ManageLoginsHandler
{
public $countLogins = 0;
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Tasks for this stage:
*
* - List all of the users logins.
* - If zero, return to "get-token" stage and make user make a login. That stage redirects here.
* - If one or more, list and let user select.
*
* @throws FireflyException
*/
public function run(): void
{
$customer = $this->getCustomer();
$request = new ListLoginsRequest($this->importJob->user);
$request->setCustomer($customer);
$request->call();
$list = $request->getLogins();
// count is zero?
$this->countLogins = \count($list);
if ($this->countLogins > 0) {
$store = [];
/** @var Login $login */
foreach ($list as $login) {
$store[] = $login->toArray();
}
$config = $this->repository->getConfiguration($this->importJob);
$config['all-logins'] = $store;
$this->repository->setConfiguration($this->importJob, $config);
}
}
/**
* @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 manageLoginsHandler::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 getExistingCustomer()');
$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.');
break;
}
}
return $customer;
}
}

View File

@@ -0,0 +1,105 @@
<?php
/**
* StageAuthenticatedHandler.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\Routine\Spectre;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Spectre\Object\Account;
use FireflyIII\Services\Spectre\Object\Login;
use FireflyIII\Services\Spectre\Request\ListAccountsRequest;
class StageAuthenticatedHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* User has selected a login (a bank). Will grab all accounts from this bank.
* Then make user pick some to import from.
*
* @throws FireflyException
*/
public function run()
{
// grab a list of logins.
$config = $this->importJob->configuration;
$logins = $config['all-logins'] ?? [];
if (\count($logins) === 0) {
throw new FireflyException('StageAuthenticatedHandler expects more than 0 logins. Apologies, the import has stopped.');
}
$selectedLogin = $config['selected-login'];
$login = null;
foreach ($logins as $loginArray) {
$loginId = $loginArray['id'] ?? -1;
if ($loginId === $selectedLogin) {
$login = new Login($loginArray);
}
}
if (null === $login) {
$login = new Login($logins[0]);
}
// with existing login we can grab accounts from this login.
$accounts = $this->getAccounts($login);
$config['accounts'] = [];
/** @var Account $account */
foreach ($accounts as $account) {
$config['accounts'][] = $account->toArray();
}
$this->repository->setConfiguration($this->importJob, $config);
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
/**
* @param Login $login
*
* @return array
* @throws FireflyException
*/
private function getAccounts(Login $login): array
{
$request = new ListAccountsRequest($this->importJob->user);
$request->setLogin($login);
$request->call();
$accounts = $request->getAccounts();
return $accounts;
}
}

View File

@@ -61,11 +61,14 @@ class StageNewHandler
{
Log::debug('Now in stageNewHandler::run()');
$customer = $this->getCustomer();
// get token using customer.
$token = $this->getToken($customer);
// get token using customer.
app('preferences')->setForUser($this->importJob->user, 'spectre_customer', $customer->toArray());
app('preferences')->setForUser($this->importJob->user, 'spectre_token', $token->toArray());
// store token in the job.
$config = $this->repository->getConfiguration($this->importJob);
$this->repository->setConfiguration($this->importJob, $config);
}
/**
@@ -122,23 +125,6 @@ class StageNewHandler
return $customer;
}
/**
* @param Customer $customer
*
* @throws FireflyException
* @return Token
*/
private function getToken(Customer $customer): Token
{
Log::debug('Now in 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();
}
}