mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-10 12:24:50 +00:00
First working version of a working Spectre import.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user