firefly-iii/app/Transformers/PiggyBankEventTransformer.php

108 lines
3.4 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;
use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class PiggyBankEventTransformer
*/
class PiggyBankEventTransformer extends TransformerAbstract
{
/** @var ParameterBag */
protected $parameters;
/**
2018-02-17 03:47:06 -06:00
* PiggyBankEventTransformer constructor.
*
* @codeCoverageIgnore
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
}
2018-02-17 03:47:06 -06:00
/**
* Convert piggy bank event.
*
* @param PiggyBankEvent $event
*
* @return array
*/
public function transform(PiggyBankEvent $event): array
{
2018-07-22 09:35:46 -05:00
$account = $event->piggyBank->account;
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$accountRepos->setUser($account->user);
$currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id');
2018-12-04 12:36:54 -06:00
$journal = $event->transactionJournal;
$transactionId = null;
2018-02-16 09:44:52 -06:00
$decimalPlaces = 2;
if ($currencyId > 0) {
2018-02-17 03:47:06 -06:00
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
$repository->setUser($account->user);
2018-08-06 12:14:30 -05:00
$currency = $repository->findNull($currencyId);
/** @noinspection NullPointerExceptionInspection */
2018-02-16 09:44:52 -06:00
$decimalPlaces = $currency->decimal_places;
}
2018-12-04 12:36:54 -06:00
if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
}
if (null !== $journal) {
$transactionId = $journal->transactions()->first()->id;
}
2018-02-16 09:44:52 -06:00
$data = [
2018-12-04 12:36:54 -06:00
'id' => (int)$event->id,
'created_at' => $event->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $event->updated_at->toAtomString(),
2018-12-04 12:36:54 -06:00
'amount' => round($event->amount, $decimalPlaces),
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_dp' => $currency->decimal_places,
'transaction_id' => $transactionId,
'links' => [
[
'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id,
],
],
];
return $data;
}
2018-03-05 12:35:58 -06:00
}