Improve transformers and tests

This commit is contained in:
James Cole
2018-12-19 06:06:01 +01:00
parent 06c3362332
commit 03b4a50317
5 changed files with 137 additions and 86 deletions

View File

@@ -24,15 +24,10 @@ declare(strict_types=1);
namespace FireflyIII\Transformers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class BillTransformer
@@ -66,28 +61,30 @@ class BillTransformer extends AbstractTransformer
{
$paidData = $this->paidData($bill);
$payDates = $this->payDates($bill);
$currency = $bill->transactionCurrency;
$notes = $this->repository->getNoteText($bill);
$notes = '' === $notes ? null : $notes;
$this->repository->setUser($bill->user);
$data = [
'id' => (int)$bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => $bill->transaction_currency_id,
'currency_code' => $bill->transactionCurrency->code,
'currency_symbol' => $bill->transactionCurrency->symbol,
'currency_decimal_places' => $bill->transactionCurrency->decimal_places,
'name' => $bill->name,
'amount_min' => round((float)$bill->amount_min, 2),
'amount_max' => round((float)$bill->amount_max, 2),
'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
'active' => $bill->active,
'notes' => $this->repository->getNoteText($bill),
'next_expected_match' => $paidData['next_expected_match'],
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
'links' => [
'id' => (int)$bill->id,
'created_at' => $bill->created_at->toAtomString(),
'updated_at' => $bill->updated_at->toAtomString(),
'currency_id' => $bill->transaction_currency_id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'name' => $bill->name,
'amount_min' => round((float)$bill->amount_min, $currency->decimal_places),
'amount_max' => round((float)$bill->amount_max, $currency->decimal_places),
'date' => $bill->date->format('Y-m-d'),
'repeat_freq' => $bill->repeat_freq,
'skip' => (int)$bill->skip,
'active' => $bill->active,
'notes' => $notes,
'next_expected_match' => $paidData['next_expected_match'],
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
'links' => [
[
'rel' => 'self',
'uri' => '/bills/' . $bill->id,
@@ -161,10 +158,7 @@ class BillTransformer extends AbstractTransformer
];
}
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user);
$set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
$set = $this->repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
$simple = $set->map(
function (Carbon $date) {
@@ -179,7 +173,7 @@ class BillTransformer extends AbstractTransformer
$nextMatch = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
}
$end = app('navigation')->addPeriod($nextMatch, $bill->repeat_freq, $bill->skip);
$journalCount = $repository->getPaidDatesInRange($bill, $nextMatch, $end)->count();
$journalCount = $this->repository->getPaidDatesInRange($bill, $nextMatch, $end)->count();
if ($journalCount > 0) {
$nextMatch = clone $end;
}