mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
New code for YNAB import.
This commit is contained in:
80
app/Support/Import/Routine/Ynab/GetAccountsHandler.php
Normal file
80
app/Support/Import/Routine/Ynab/GetAccountsHandler.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user