firefly-iii/app/Transformers/PiggyBankEventTransformer.php

110 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* PiggyBankEventTransformer.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\Transformers;
use FireflyIII\Models\PiggyBankEvent;
2018-07-22 09:35:46 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-02-17 03:47:06 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-12-19 22:46:05 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
/**
* Class PiggyBankEventTransformer
*/
class PiggyBankEventTransformer extends AbstractTransformer
{
2018-12-19 22:46:05 -06:00
/** @var CurrencyRepositoryInterface */
private $currencyRepos;
/** @var PiggyBankRepositoryInterface */
private $piggyRepos;
/** @var AccountRepositoryInterface */
private $repository;
/**
2018-02-17 03:47:06 -06:00
* PiggyBankEventTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
2018-12-19 22:46:05 -06:00
$this->repository = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
$this->piggyRepos = app(PiggyBankRepositoryInterface::class);
if ('testing' === config('app.env')) {
2019-06-07 11:20:15 -05:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
2018-02-17 03:47:06 -06:00
/**
* Convert piggy bank event.
*
* @param PiggyBankEvent $event
*
* @return array
*/
public function transform(PiggyBankEvent $event): array
{
2018-12-19 22:46:05 -06:00
// get account linked to piggy bank
2018-07-22 09:35:46 -05:00
$account = $event->piggyBank->account;
2018-12-19 22:46:05 -06:00
// set up repositories.
$this->repository->setUser($account->user);
$this->currencyRepos->setUser($account->user);
$this->piggyRepos->setUser($account->user);
// get associated currency or fall back to the default:
2019-08-15 23:20:07 -05:00
// TODO we can use getAccountCurrency() instead
2018-12-19 22:46:05 -06:00
$currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (null === $currency) {
2018-12-04 12:36:54 -06:00
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
}
2018-12-19 22:46:05 -06:00
// get associated journal and transaction, if any:
2019-08-03 03:50:43 -05:00
$journalId = (int)$event->transaction_journal_id;
2018-02-16 09:44:52 -06:00
$data = [
2018-12-19 22:46:05 -06:00
'id' => (int)$event->id,
'created_at' => $event->created_at->toAtomString(),
'updated_at' => $event->updated_at->toAtomString(),
'amount' => round($event->amount, $currency->decimal_places),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
2019-08-03 03:50:43 -05:00
'transaction_journal_id' => $journalId,
2018-12-19 22:46:05 -06:00
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id,
],
],
];
return $data;
}
2018-03-05 12:35:58 -06:00
}