Refactor journal repository and fix tests.

This commit is contained in:
James Cole
2019-08-10 13:42:33 +02:00
parent b7f3c53688
commit 93f1854be0
14 changed files with 286 additions and 130 deletions

View File

@@ -0,0 +1,105 @@
<?php
/**
* JournalAPIRepository.php
* Copyright (c) 2019 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/>.
*/
namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* Class JournalAPIRepository
*/
class JournalAPIRepository implements JournalAPIRepositoryInterface
{
/** @var User */
private $user;
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/**
* Returns transaction by ID. Used to validate attachments.
*
* @param int $transactionId
*
* @return Transaction|null
*/
public function findTransaction(int $transactionId): ?Transaction
{
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.user_id', $this->user->id)
->where('transactions.id', $transactionId)
->first(['transactions.*']);
return $transaction;
}
/**
* Return all attachments for journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getAttachments(TransactionJournal $journal): Collection
{
return $journal->attachments;
}
/**
* Get all piggy bank events for a journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection
{
/** @var Collection $set */
$events = $journal->piggyBankEvents()->get();
$events->each(
function (PiggyBankEvent $event) {
$event->piggyBank = $event->piggyBank()->withTrashed()->first();
}
);
return $events;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}

View File

@@ -0,0 +1,65 @@
<?php
/**
* JournalAPIRepositoryInterface.php
* Copyright (c) 2019 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/>.
*/
namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
* Interface JournalAPIRepositoryInterface
*/
interface JournalAPIRepositoryInterface
{
/**
* Returns transaction by ID. Used to validate attachments.
*
* @param int $transactionId
*
* @return Transaction|null
*/
public function findTransaction(int $transactionId): ?Transaction;
/**
* Return all attachments for journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getAttachments(TransactionJournal $journal): Collection;
/**
* Get all piggy bank events for a journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection;
/**
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* JournalCLIRepositoryInterface.php
* Copyright (c) 2019 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/>.
*/
namespace FireflyIII\Repositories\Journal;
use FireflyIII\User;
/**
* Interface JournalCLIRepositoryInterface
*/
interface JournalCLIRepositoryInterface
{
/**
* @param User $user
*/
public function setUser(User $user);
}

View File

@@ -47,10 +47,6 @@ use stdClass;
/**
* Class JournalRepository.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class JournalRepository implements JournalRepositoryInterface
{
@@ -146,21 +142,6 @@ class JournalRepository implements JournalRepositoryInterface
return $this->user->transactionJournals()->where('id', $journalId)->first();
}
/**
* @param int $transactionid
*
* @return Transaction|null
*/
public function findTransaction(int $transactionid): ?Transaction
{
$transaction = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.user_id', $this->user->id)
->where('transactions.id', $transactionid)
->first(['transactions.*']);
return $transaction;
}
/**
* Get users first transaction journal or NULL.
*
@@ -178,31 +159,6 @@ class JournalRepository implements JournalRepositoryInterface
return $result;
}
/**
* Return all attachments for journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getAttachments(TransactionJournal $journal): Collection
{
return $journal->attachments;
}
/**
* Get all attachments connected to the transaction group.
*
* @param TransactionJournal $transactionJournal
*
* @return Collection
*/
public function getAttachmentsByJournal(TransactionJournal $transactionJournal): Collection
{
// TODO: Implement getAttachmentsByJournal() method.
throw new NotImplementedException;
}
/**
* Return the ID of the budget linked to the journal (if any) or the transactions (if any).
*
@@ -447,23 +403,7 @@ class JournalRepository implements JournalRepositoryInterface
return $note->text;
}
/**
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection
{
/** @var Collection $set */
$events = $journal->piggyBankEvents()->get();
$events->each(
function (PiggyBankEvent $event) {
$event->piggyBank = $event->piggyBank()->withTrashed()->first();
}
);
return $events;
}
/**
* Returns all journals with more than 2 transactions. Should only return empty collections

View File

@@ -23,16 +23,13 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Journal;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Models\TransactionJournalMeta;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
/**
* Interface JournalRepositoryInterface.
@@ -97,15 +94,6 @@ interface JournalRepositoryInterface
*/
public function findNull(int $journalId): ?TransactionJournal;
/**
* TODO maybe create API repository?
*
* @param int $transactionid
*
* @return Transaction|null
*/
public function findTransaction(int $transactionid): ?Transaction;
/**
* Get users very first transaction journal.
*
@@ -113,17 +101,6 @@ interface JournalRepositoryInterface
*/
public function firstNull(): ?TransactionJournal;
/**
* TODO maybe create API repository?
*
* Return all attachments for journal.
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getAttachments(TransactionJournal $journal): Collection;
/**
* TODO console repository?
*
@@ -235,15 +212,6 @@ interface JournalRepositoryInterface
*/
public function getNoteText(TransactionJournal $journal): ?string;
/**
* TODO used only in the API
*
* @param TransactionJournal $journal
*
* @return Collection
*/
public function getPiggyBankEvents(TransactionJournal $journal): Collection;
/**
* TODO used only on the console.
*