Removed more getJournals functions in favour of the collector.

This commit is contained in:
James Cole
2016-11-06 08:08:06 +01:00
parent 0edffd8ea1
commit 77e2cf40df
14 changed files with 163 additions and 161 deletions

View File

@@ -16,6 +16,7 @@ namespace FireflyIII\Rules;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Rules\Actions\ActionInterface;
use FireflyIII\Rules\Factory\ActionFactory;
@@ -144,6 +145,36 @@ final class Processor
return $this->rule;
}
/**
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal
* matches all of the triggers (regardless of whether the Processor could act on it).
*
* @param Transaction $transaction
*
* @return bool
*/
public function handleTransaction(Transaction $transaction): bool
{
Log::debug(sprintf('handleTransactionJournal for journal #%d (transaction #%d)', $transaction->transaction_journal_id, $transaction->id));
// grab the actual journal.
$journal = $transaction->transactionJournal()->first();
$this->journal = $journal;
// get all triggers:
$triggered = $this->triggered();
if ($triggered) {
if ($this->actions->count() > 0) {
$this->actions();
}
return true;
}
return false;
}
/**
* This method will scan the given transaction journal and check if it matches the triggers found in the Processor
* If so, it will also attempt to run the given actions on the journal. It returns a bool indicating if the transaction journal

View File

@@ -13,7 +13,8 @@ declare(strict_types = 1);
namespace FireflyIII\Rules;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use Illuminate\Support\Collection;
@@ -62,7 +63,7 @@ class TransactionMatcher
if (count($this->triggers) === 0) {
return new Collection;
}
$pagesize = min($this->range / 2, $this->limit * 2);
$pageSize = min($this->range / 2, $this->limit * 2);
// Variables used within the loop
$processed = 0;
@@ -76,31 +77,42 @@ class TransactionMatcher
// - the maximum number of transactions to search in have been searched
do {
// Fetch a batch of transactions from the database
$paginator = $this->tasker->getJournals($this->transactionTypes, $page, $pagesize);
$set = $paginator->getCollection();
$collector = new JournalCollector(auth()->user());
$collector->setAllAssetAccounts()->setLimit($pageSize * 2)->setPage($page)->setTypes($this->transactionTypes);
$set = $collector->getPaginatedJournals();
Log::debug(sprintf('Found %d journals to check. ', $set->count()));
// Filter transactions that match the given triggers.
$filtered = $set->filter(
function (TransactionJournal $journal) use ($processor) {
Log::debug(sprintf('Test these triggers on #%d', $journal->id));
function (Transaction $transaction) use ($processor) {
Log::debug(sprintf('Test these triggers on journal #%d (transaction #%d)', $transaction->transaction_journal_id, $transaction->id));
return $processor->handleTransactionJournal($journal);
return $processor->handleTransaction($transaction);
}
);
Log::debug(sprintf('Found %d journals that match.', $filtered->count()));
// merge:
/** @var Collection $result */
$result = $result->merge($filtered);
Log::debug(sprintf('Total count is now %d', $result->count()));
// Update counters
$page++;
$processed += count($set);
Log::debug(sprintf('Page is now %d, processed is %d', $page, $processed));
// Check for conditions to finish the loop
$reachedEndOfList = $set->count() < $pagesize;
$reachedEndOfList = $set->count() < $pageSize;
$foundEnough = $result->count() >= $this->limit;
$searchedEnough = ($processed >= $this->range);
Log::debug(sprintf('reachedEndOfList: %s', var_export($reachedEndOfList, true)));
Log::debug(sprintf('foundEnough: %s', var_export($foundEnough, true)));
Log::debug(sprintf('searchedEnough: %s', var_export($searchedEnough, true)));
} while (!$reachedEndOfList && !$foundEnough && !$searchedEnough);
// If the list of matchingTransactions is larger than the maximum number of results