firefly-iii/app/Rules/TransactionMatcher.php
James Cole 6c22bad77a Revamped a part of the rule test code.
- clean way of constructing triggers
- processor can be constructed in a number of ways.
- cleaner transaction matcher using collections instead of arrays
2016-02-17 21:03:59 +01:00

154 lines
4.5 KiB
PHP

<?php
declare(strict_types = 1);
/**
* TransactionMatcher.php
* Copyright (C) 2016 Robert Horlings
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules;
use FireflyIII\Models\Rule;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
/**
* Class TransactionMatcher is used to find a list of
* transaction matching a set of triggers
*
* @package FireflyIII\Rules
*/
class TransactionMatcher
{
/** @var int */
private $limit = 10;
/** @var int Maximum number of transaction to search in (for performance reasons) * */
private $range = 200;
/** @var array */
private $transactionTypes = [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
/** @var array List of triggers to match */
private $triggers = [];
/**
* Find matching transactions for the current set of triggers
*
* @param int $maxResults The maximum number of transactions returned
*
* @return array
*/
public function findMatchingTransactions()
{
/** @var JournalRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
$pagesize = min($this->range / 2, $this->limit * 2);
// Variables used within the loop
$numTransactionsProcessed = 0;
$page = 1;
$matchingTransactions = new Collection();
// Flags to indicate the end of the loop
$reachedEndOfList = false;
$foundEnoughTransactions = false;
$searchedEnoughTransactions = false;
$processor = Processor::makeFromStringArray($this->triggers);
// Start a loop to fetch batches of transactions. The loop will finish if:
// - all transactions have been fetched from the database
// - the maximum number of transactions to return has been found
// - the maximum number of transactions to search in have been searched
do {
// Fetch a batch of transactions from the database
$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
$set = $repository->getCollectionOfTypes($this->transactionTypes, $offset, $pagesize);
// Filter transactions that match the given triggers.
$filtered = $set->filter(
function (TransactionJournal $journal) use ($processor) {
return $processor->handleTransactionJournal($journal);
}
);
// merge:
$matchingTransactions = $matchingTransactions->merge($filtered);
// $matchingTransactions += array_filter(
// $set, function ($transaction) {
// $processor = new Processor(new Rule, $transaction);
//
// return $processor->isTriggeredBy($this->triggers);
// }
// );
// Update counters
$page++;
$numTransactionsProcessed += count($set);
// Check for conditions to finish the loop
$reachedEndOfList = (count($set) < $pagesize);
$foundEnoughTransactions = (count($matchingTransactions) >= $this->limit);
$searchedEnoughTransactions = ($numTransactionsProcessed >= $this->range);
} while (!$reachedEndOfList && !$foundEnoughTransactions && !$searchedEnoughTransactions);
// If the list of matchingTransactions is larger than the maximum number of results
// (e.g. if a large percentage of the transactions match), truncate the list
$matchingTransactions = $matchingTransactions->slice(0, $this->limit);
return $matchingTransactions;
}
/**
* @return int
*/
public function getLimit()
{
return $this->limit;
}
/**
* @param int $limit
*/
public function setLimit($limit)
{
$this->limit = $limit;
}
/**
* @return int
*/
public function getRange()
{
return $this->range;
}
/**
* @param int $range
*/
public function setRange($range)
{
$this->range = $range;
}
/**
* @return array
*/
public function getTriggers()
{
return $this->triggers;
}
/**
* @param array $triggers
*/
public function setTriggers($triggers)
{
$this->triggers = $triggers;
}
}