Remove a lot of deprecated code.

This commit is contained in:
James Cole
2018-07-25 06:45:25 +02:00
parent dbf019135a
commit 7c950c3022
39 changed files with 189 additions and 621 deletions

View File

@@ -1,213 +0,0 @@
<?php
/**
* BunqInformation.php
* Copyright (c) 2017 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\Information;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Services\Bunq\Object\Alias;
use FireflyIII\Services\Bunq\Object\MonetaryAccountBank;
use FireflyIII\Services\Bunq\Request\DeleteDeviceSessionRequest;
use FireflyIII\Services\Bunq\Request\DeviceSessionRequest;
use FireflyIII\Services\Bunq\Request\ListMonetaryAccountRequest;
use FireflyIII\Services\Bunq\Request\ListUserRequest;
use FireflyIII\Services\Bunq\Token\SessionToken;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* @codeCoverageIgnore
* @deprecated
* Class BunqInformation.
*/
class BunqInformation implements InformationInterface
{
/** @var User */
private $user;
/**
* Returns a collection of accounts. Preferrably, these follow a uniform Firefly III format so they can be managed over banks.
*
* The format for these bank accounts is basically this:
*
* id: bank specific id
* name: bank appointed name
* number: account number (usually IBAN)
* currency: ISO code of currency
* balance: current balance
*
*
* any other fields are optional but can be useful:
* image: logo or account specific thing
* color: any associated color.
*
* @return array
*
* @throws FireflyException
*/
public function getAccounts(): array
{
// cache for an hour:
$cache = new CacheProperties;
$cache->addProperty('bunq.get-accounts');
$cache->addProperty(date('dmy h'));
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
Log::debug('Now in getAccounts()');
$sessionToken = $this->startSession();
$userId = $this->getUserInformation($sessionToken);
// get list of Bunq accounts:
$accounts = $this->getMonetaryAccounts($sessionToken, $userId);
$return = [];
/** @var MonetaryAccountBank $account */
foreach ($accounts as $account) {
$current = [
'id' => $account->getId(),
'name' => $account->getDescription(),
'currency' => $account->getCurrency(),
'balance' => $account->getBalance()->getValue(),
'color' => $account->getSetting()->getColor(),
];
/** @var Alias $alias */
foreach ($account->getAliases() as $alias) {
if ('IBAN' === $alias->getType()) {
$current['number'] = $alias->getValue();
}
}
$return[] = $current;
}
$cache->store($return);
$this->closeSession($sessionToken);
return $return;
}
/**
* 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;
}
/**
* @param SessionToken $sessionToken
*
*/
private function closeSession(SessionToken $sessionToken): void
{
Log::debug('Going to close session');
$apiKey = app('preferences')->getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = app('preferences')->getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = app('preferences')->getForUser($this->user, 'bunq_private_key')->data;
$request = new DeleteDeviceSessionRequest();
$request->setSecret($apiKey);
$request->setPrivateKey($privateKey);
$request->setServerPublicKey($serverPublicKey);
$request->setSessionToken($sessionToken);
$request->call();
}
/**
* @param SessionToken $sessionToken
* @param int $userId
*
* @return Collection
* @throws FireflyException
*/
private function getMonetaryAccounts(SessionToken $sessionToken, int $userId): Collection
{
$apiKey = app('preferences')->getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = app('preferences')->getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = app('preferences')->getForUser($this->user, 'bunq_private_key')->data;
$request = new ListMonetaryAccountRequest;
$request->setSessionToken($sessionToken);
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->setUserId($userId);
$request->call();
return $request->getMonetaryAccounts();
}
/**
* @param SessionToken $sessionToken
*
* @return int
*
* @throws FireflyException
*/
private function getUserInformation(SessionToken $sessionToken): int
{
$apiKey = app('preferences')->getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = app('preferences')->getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = app('preferences')->getForUser($this->user, 'bunq_private_key')->data;
$request = new ListUserRequest;
$request->setSessionToken($sessionToken);
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->call();
// return the first that isn't null?
$company = $request->getUserCompany();
if ($company->getId() > 0) {
return $company->getId();
}
$user = $request->getUserPerson();
if ($user->getId() > 0) {
return $user->getId();
}
throw new FireflyException('Expected user or company from Bunq, but got neither.');
}
/**
* @return SessionToken
* @throws FireflyException
*/
private function startSession(): SessionToken
{
Log::debug('Now in startSession.');
$apiKey = app('preferences')->getForUser($this->user, 'bunq_api_key')->data;
$serverPublicKey = app('preferences')->getForUser($this->user, 'bunq_server_public_key')->data;
$privateKey = app('preferences')->getForUser($this->user, 'bunq_private_key')->data;
$installationToken = app('preferences')->getForUser($this->user, 'bunq_installation_token')->data;
$request = new DeviceSessionRequest();
$request->setSecret($apiKey);
$request->setServerPublicKey($serverPublicKey);
$request->setPrivateKey($privateKey);
$request->setInstallationToken($installationToken);
$request->call();
$sessionToken = $request->getSessionToken();
Log::debug(sprintf('Now have got session token: %s', serialize($sessionToken)));
return $sessionToken;
}
}

View File

@@ -1,56 +0,0 @@
<?php
/**
* InformationInterface.php
* Copyright (c) 2017 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\Information;
use FireflyIII\User;
/**
* @deprecated
* Interface InformationInterface.
*/
interface InformationInterface
{
/**
* Returns a collection of accounts. Preferrably, these follow a uniform Firefly III format so they can be managed over banks.
*
* The format for these bank accounts is basically this:
*
* id: bank specific id
* name: bank appointed name
* number: account number (usually IBAN)
* currency: ISO code of currency
*
* any other fields are optional but can be useful:
* image: logo or account specific thing
*
* @return array
*/
public function getAccounts(): array;
/**
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
*
* @param User $user
*/
public function setUser(User $user): void;
}

View File

@@ -1,176 +0,0 @@
<?php
/**
* TransactionJournalTrait.php
* Copyright (c) 2017 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\Models;
use Carbon\Carbon;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
* Class TransactionJournalTrait.
*
* @property int $id
* @property Carbon $date
* @property string $transaction_type_type
* @property TransactionType $transactionType
*/
trait TransactionJournalTrait
{
/**
* @param Builder $query
* @param string $table
*
* @return bool
*/
public static function isJoined(Builder $query, string $table): bool
{
$joins = $query->getQuery()->joins;
if (null === $joins) {
return false;
}
foreach ($joins as $join) {
if ($join->table === $table) {
return true;
}
}
return false;
}
/**
* @deprecated
* @return Collection
*/
public function destinationAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-list');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$transactions = $this->transactions()->where('amount', '>', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;
}
/**
* @deprecated
* @return Collection
*/
public function destinationTransactionList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-transaction-list');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$list = $this->transactions()->where('amount', '>', 0)->with('account')->get();
$cache->store($list);
return $list;
}
/**
* @deprecated
* @return Collection
*/
public function sourceAccountList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-list');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$transactions = $this->transactions()->where('amount', '<', 0)->orderBy('transactions.account_id')->with('account')->get();
$list = new Collection;
/** @var Transaction $t */
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;
}
/**
* @deprecated
* @return Collection
*/
public function sourceTransactionList(): Collection
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-transaction-list');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$list = $this->transactions()->where('amount', '<', 0)->with('account')->get();
$cache->store($list);
return $list;
}
/**
* @deprecated
* @return string
*/
public function transactionTypeStr(): string
{
$cache = new CacheProperties;
$cache->addProperty($this->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('type-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$typeStr = $this->transaction_type_type ?? $this->transactionType->type;
$cache->store($typeStr);
return $typeStr;
}
/**
* @return HasMany
*/
abstract public function transactions(): HasMany;
}

View File

@@ -26,6 +26,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Twig\Extension\TransactionJournal as TransactionJournalExtension;
use Twig_Extension;
@@ -53,8 +54,10 @@ class Journal extends Twig_Extension
return $cache->get(); // @codeCoverageIgnore
}
$list = $journal->destinationAccountList();
$array = [];
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$list = $repository->getJournalDestinationAccounts($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if (AccountType::CASH === $entry->accountType->type) {
@@ -119,7 +122,10 @@ class Journal extends Twig_Extension
return $cache->get(); // @codeCoverageIgnore
}
$list = $journal->sourceAccountList();
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$list = $repository->getJournalSourceAccounts($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {