mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 14:41:20 +00:00
First code for YNAB import #145
This commit is contained in:
125
app/Import/JobConfiguration/YnabJobConfiguration.php
Normal file
125
app/Import/JobConfiguration/YnabJobConfiguration.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* YnabJobConfiguration.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\Import\JobConfiguration;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class YnabJobConfiguration
|
||||
*/
|
||||
class YnabJobConfiguration implements JobConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob The import job */
|
||||
private $importJob;
|
||||
/** @var ImportJobRepositoryInterface Import job repository */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Returns true when the initial configuration for this job is complete.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function configurationComplete(): bool
|
||||
{
|
||||
// config is only needed when the job is in stage "new".
|
||||
if ($this->importJob->stage === 'new') {
|
||||
Log::debug('YNAB configurationComplete: stage is new, return false');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::debug('YNAB configurationComplete: stage is not new, return true');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store any data from the $data array into the job. Anything in the message bag will be flashed
|
||||
* as an error to the user, regardless of its content.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function configureJob(array $data): MessageBag
|
||||
{
|
||||
Log::debug('YNAB configureJob: nothing to do.');
|
||||
|
||||
// there is never anything to store from this job.
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data required for the next step in the job configuration.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$data = [];
|
||||
// here we update the job so it can redirect properly to YNAB
|
||||
if ($this->importJob->stage === 'new') {
|
||||
|
||||
// update stage to make sure we catch the token.
|
||||
$this->repository->setStage($this->importJob, 'catch-auth-code');
|
||||
$clientId = (string)config('import.options.ynab.client_id');
|
||||
$callBackUri = route('import.callback.ynab');
|
||||
$uri = sprintf(
|
||||
'https://app.youneedabudget.com/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=%s', $clientId, $callBackUri,
|
||||
$this->importJob->key
|
||||
);
|
||||
$data['token-url'] = $uri;
|
||||
Log::debug(sprintf('YNAB getNextData: URI to redirect to is %s', $uri));
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the view of the next step in the job configuration.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNextView(): string
|
||||
{
|
||||
Log::debug('Return YNAB redirect view.');
|
||||
return 'import.ynab.redirect';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set import job.
|
||||
*
|
||||
* @param ImportJob $importJob
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($importJob->user);
|
||||
}
|
||||
}
|
||||
145
app/Import/Prerequisites/YnabPrerequisites.php
Normal file
145
app/Import/Prerequisites/YnabPrerequisites.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* YnabPrerequisites.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\Import\Prerequisites;
|
||||
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class YnabPrerequisites
|
||||
*/
|
||||
class YnabPrerequisites implements PrerequisitesInterface
|
||||
{
|
||||
/** @var User The current user */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Returns view name that allows user to fill in prerequisites.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getView(): string
|
||||
{
|
||||
return 'import.ynab.prerequisites';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns any values required for the prerequisites-view.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getViewParameters(): array
|
||||
{
|
||||
Log::debug('Now in YnabPrerequisites::getViewParameters()');
|
||||
$clientId = '';
|
||||
$clientSecret = '';
|
||||
if ($this->hasClientId()) {
|
||||
$clientId = app('preferences')->getForUser($this->user, 'ynab_client_id', null)->data;
|
||||
}
|
||||
if ($this->hasClientSecret()) {
|
||||
$clientSecret = app('preferences')->getForUser($this->user, 'ynab_client_secret', null)->data;
|
||||
}
|
||||
|
||||
$callBackUri = route('import.callback.ynab');
|
||||
|
||||
return ['client_id' => $clientId, 'client_secret' => $clientSecret, 'callback_uri' => $callBackUri];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate if all prerequisites have been met.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isComplete(): bool
|
||||
{
|
||||
return $this->hasClientId() && $this->hasClientSecret();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method responds to the user's submission of an API key. Should do nothing but store the value.
|
||||
*
|
||||
* Errors must be returned in the message bag under the field name they are requested by.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function storePrerequisites(array $data): MessageBag
|
||||
{
|
||||
$clientId = $data['client_id'] ?? '';
|
||||
$clientSecret = $data['client_secret'] ?? '';
|
||||
Log::debug('Storing YNAB client data');
|
||||
app('preferences')->setForUser($this->user, 'ynab_client_id', $clientId);
|
||||
app('preferences')->setForUser($this->user, 'ynab_client_secret', $clientSecret);
|
||||
|
||||
return new MessageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have the client ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasClientId(): bool
|
||||
{
|
||||
$clientId = app('preferences')->getForUser($this->user, 'ynab_client_id', null);
|
||||
if (null === $clientId) {
|
||||
return false;
|
||||
}
|
||||
if ('' === (string)$clientId->data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we have the client secret
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasClientSecret(): bool
|
||||
{
|
||||
$clientSecret = app('preferences')->getForUser($this->user, 'ynab_client_secret', null);
|
||||
if (null === $clientSecret) {
|
||||
return false;
|
||||
}
|
||||
if ('' === (string)$clientSecret->data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -50,12 +50,12 @@ class BunqRoutine implements RoutineInterface
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Log::debug(sprintf('Now in SpectreRoutine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
|
||||
Log::debug(sprintf('Now in BunqRoutine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
|
||||
$valid = ['ready_to_run']; // should be only ready_to_run
|
||||
if (\in_array($this->importJob->status, $valid, true)) {
|
||||
switch ($this->importJob->stage) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('SpectreRoutine cannot handle stage "%s".', $this->importJob->stage)); // @codeCoverageIgnore
|
||||
throw new FireflyException(sprintf('BunqRoutine cannot handle stage "%s".', $this->importJob->stage)); // @codeCoverageIgnore
|
||||
case 'new':
|
||||
// list all of the users accounts.
|
||||
$this->repository->setStatus($this->importJob, 'running');
|
||||
|
||||
85
app/Import/Routine/YnabRoutine.php
Normal file
85
app/Import/Routine/YnabRoutine.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* YnabRoutine.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\Import\Routine;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Routine\Ynab\StageGetAccessHandler;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class YnabRoutine
|
||||
*/
|
||||
class YnabRoutine implements RoutineInterface
|
||||
{
|
||||
/** @var ImportJob The import job */
|
||||
private $importJob;
|
||||
|
||||
/** @var ImportJobRepositoryInterface Import job repository */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* At the end of each run(), the import routine must set the job to the expected status.
|
||||
*
|
||||
* The final status of the routine must be "provider_finished".
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Log::debug(sprintf('Now in YNAB routine::run() with status "%s" and stage "%s".', $this->importJob->status, $this->importJob->stage));
|
||||
$valid = ['ready_to_run']; // should be only ready_to_run
|
||||
if (\in_array($this->importJob->status, $valid, true)) {
|
||||
|
||||
// get access token from YNAB
|
||||
if ('get_access_token' === $this->importJob->stage) {
|
||||
// list all of the users accounts.
|
||||
$this->repository->setStatus($this->importJob, 'running');
|
||||
/** @var StageGetAccessHandler $handler */
|
||||
$handler = app(StageGetAccessHandler::class);
|
||||
$handler->setImportJob($this->importJob);
|
||||
$handler->run();
|
||||
$this->repository->setStage($this->importJob, 'get_transactions');
|
||||
return;
|
||||
}
|
||||
throw new FireflyException(sprintf('YNAB import routine cannot handle stage "%s"', $this->importJob->stage));
|
||||
}
|
||||
throw new FireflyException(sprintf('YNAB import routine cannot handle status "%s"', $this->importJob->status));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the import job.
|
||||
*
|
||||
* @param ImportJob $importJob
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setImportJob(ImportJob $importJob): void
|
||||
{
|
||||
$this->importJob = $importJob;
|
||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||
$this->repository->setUser($importJob->user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user