New code for YNAB import.

This commit is contained in:
James Cole
2018-07-29 21:02:03 +02:00
parent 7ad09da4e9
commit dfd9cf0874
19 changed files with 1341 additions and 264 deletions

View File

@@ -0,0 +1,80 @@
<?php
/**
* GetAccountsHandler.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\Ynab;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Ynab\Request\GetAccountsRequest;
/**
* Class GetAccountsHandler
*/
class GetAccountsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
* Get list of accounts for the selected budget.
*
* @throws FireflyException
*/
public function run(): void
{
$config = $this->repository->getConfiguration($this->importJob);
$selectedBudget = $config['selected_budget'] ?? '';
if ('' === $selectedBudget) {
$firstBudget = $config['budgets'][0] ?? false;
if (false === $firstBudget) {
throw new FireflyException('The configuration contains no budget. Erroring out.');
}
$selectedBudget = $firstBudget['id'];
$config['selected_budget'] = $selectedBudget;
}
$token = $config['access_token'];
$request = new GetAccountsRequest;
$request->budgetId = $selectedBudget;
$request->setAccessToken($token);
$request->call();
$config['accounts'] = $request->accounts;
$this->repository->setConfiguration($this->importJob, $config);
if (0 === \count($config['accounts'])) {
throw new FireflyException('This budget contains zero accounts.');
}
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@@ -30,6 +30,7 @@ use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Log;
use RuntimeException;
/**
* Class StageGetAccessHandler
@@ -66,12 +67,31 @@ class StageGetAccessHandler
throw new FireflyException($e->getMessage());
}
$statusCode = $res->getStatusCode();
$content = trim($res->getBody()->getContents());
try {
$content = trim($res->getBody()->getContents());
} catch(RuntimeException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
throw new FireflyException($e->getMessage());
}
$json = json_decode($content, true) ?? [];
Log::debug(sprintf('Status code from YNAB is %d', $statusCode));
Log::debug(sprintf('Body of result is %s', $content), $json);
Log::error('Hard exit');
exit;
// store refresh token (if present?) as preference
// store token in job:
$configuration = $this->repository->getConfiguration($this->importJob);
$configuration['access_token'] = $json['access_token'];
$configuration['access_token_expires'] = (int)$json['created_at'] + (int)$json['expires_in'];
$this->repository->setConfiguration($this->importJob, $configuration);
Log::debug('end of StageGetAccessHandler::run()');
$refreshToken = (string)($json['refresh_token'] ?? '');
if ('' !== $refreshToken) {
app('preferences')->set('ynab_refresh_token', $refreshToken);
}
}
/**

View File

@@ -0,0 +1,75 @@
<?php
/**
* StageGetBudgetsHandler.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\Ynab;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use FireflyIII\Services\Ynab\Request\GetBudgetsRequest;
use Log;
/**
* Class StageGetBudgetsHandler
*/
class StageGetBudgetsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
* @throws FireflyException
*/
public function run(): void
{
Log::debug('Now in StageGetBudgetsHandler::run()');
// grab access token from job:
$configuration = $this->repository->getConfiguration($this->importJob);
$token = $configuration['access_token'];
$request = new GetBudgetsRequest;
$request->setAccessToken($token);
$request->call();
// store budgets in users preferences.
$configuration['budgets'] = $request->budgets;
$this->repository->setConfiguration($this->importJob, $configuration);
Log::debug(sprintf('Found %d budgets', \count($request->budgets)));
if (\count($request->budgets) === 0) {
throw new FireflyException('It seems this user has zero budgets or an error prevented Firefly III from reading them.');
}
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* StageGetTransactionsHandler.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\Ynab;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class StageGetTransactionsHandler
*/
class StageGetTransactionsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
public function run(): void
{
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* StageMatchAccountsHandler.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\Ynab;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
/**
* Class StageMatchAccountsHandler
*/
class StageMatchAccountsHandler
{
/** @var ImportJob */
private $importJob;
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
public function run(): void
{
}
/**
* @param ImportJob $importJob
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
}
}