2016-02-17 05:05:41 -06:00
|
|
|
<?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\TransactionType;
|
2016-02-17 10:32:02 -06:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2016-02-17 05:05:41 -06:00
|
|
|
|
|
|
|
/**
|
2016-02-17 10:32:02 -06:00
|
|
|
* Class TransactionMatcher is used to find a list of
|
2016-02-17 05:05:41 -06:00
|
|
|
* transaction matching a set of triggers
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Rules
|
|
|
|
*/
|
|
|
|
class TransactionMatcher
|
|
|
|
{
|
2016-02-17 10:32:02 -06:00
|
|
|
/** @var int Maximum number of transaction to search in (for performance reasons) * */
|
2016-02-17 13:24:59 -06:00
|
|
|
private $range = 200;
|
|
|
|
/** @var int */
|
|
|
|
private $limit = 10;
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
/** @var array */
|
2016-02-17 13:25:20 -06:00
|
|
|
private $transactionTypes = [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
|
2016-02-17 10:32:02 -06:00
|
|
|
/** @var array List of triggers to match */
|
2016-02-17 13:25:20 -06:00
|
|
|
private $triggers = [];
|
2016-02-17 05:05:41 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find matching transactions for the current set of triggers
|
2016-02-17 10:32:02 -06:00
|
|
|
*
|
|
|
|
* @param int $maxResults The maximum number of transactions returned
|
|
|
|
*
|
|
|
|
* @return array
|
2016-02-17 05:05:41 -06:00
|
|
|
*/
|
2016-02-17 13:25:20 -06:00
|
|
|
public function findMatchingTransactions()
|
2016-02-17 10:32:02 -06:00
|
|
|
{
|
2016-02-17 05:05:41 -06:00
|
|
|
/** @var JournalRepositoryInterface $repository */
|
|
|
|
$repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// We don't know the number of transaction to fetch from the database, in
|
|
|
|
// order to return the proper number of matching transactions. Since we don't want
|
|
|
|
// to fetch all transactions (as the first transactions already match, or the last
|
|
|
|
// transactions are irrelevant), we will fetch data in pages.
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// The optimal pagesize is somewhere between the maximum number of results to be returned
|
|
|
|
// and the maximum number of transactions to consider.
|
2016-02-17 13:24:27 -06:00
|
|
|
$pagesize = min($this->range / 2, $maxResults * 2);
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// Variables used within the loop
|
|
|
|
$numTransactionsProcessed = 0;
|
2016-02-17 10:32:02 -06:00
|
|
|
$page = 1;
|
|
|
|
$matchingTransactions = [];
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// Flags to indicate the end of the loop
|
2016-02-17 10:32:02 -06:00
|
|
|
$reachedEndOfList = false;
|
|
|
|
$foundEnoughTransactions = false;
|
2016-02-17 05:05:41 -06:00
|
|
|
$searchedEnoughTransactions = false;
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// 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
|
2016-02-17 10:32:02 -06:00
|
|
|
$offset = $page > 0 ? ($page - 1) * $pagesize : 0;
|
|
|
|
$transactions = $repository->getJournalsOfTypes($this->transactionTypes, $offset, $page, $pagesize)->getCollection()->all();
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// Filter transactions that match the rule
|
2016-02-17 10:32:02 -06:00
|
|
|
$matchingTransactions += array_filter(
|
|
|
|
$transactions, function ($transaction) {
|
2016-02-17 05:05:41 -06:00
|
|
|
$processor = new Processor(new Rule, $transaction);
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
return $processor->isTriggeredBy($this->triggers);
|
2016-02-17 10:32:02 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// Update counters
|
|
|
|
$page++;
|
|
|
|
$numTransactionsProcessed += count($transactions);
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// Check for conditions to finish the loop
|
|
|
|
$reachedEndOfList = (count($transactions) < $pagesize);
|
|
|
|
$foundEnoughTransactions = (count($matchingTransactions) >= $maxResults);
|
2016-02-17 13:24:27 -06:00
|
|
|
$searchedEnoughTransactions = ($numTransactionsProcessed >= $this->range);
|
2016-02-17 10:32:02 -06:00
|
|
|
} while (!$reachedEndOfList && !$foundEnoughTransactions && !$searchedEnoughTransactions);
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
// 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 = array_slice($matchingTransactions, 0, $maxResults);
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
return $matchingTransactions;
|
|
|
|
}
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
/**
|
2016-02-17 13:25:54 -06:00
|
|
|
* @return int
|
2016-02-17 05:05:41 -06:00
|
|
|
*/
|
2016-02-17 13:25:54 -06:00
|
|
|
public function getRange()
|
2016-02-17 10:32:02 -06:00
|
|
|
{
|
2016-02-17 13:24:27 -06:00
|
|
|
return $this->range;
|
2016-02-17 05:05:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 13:25:54 -06:00
|
|
|
* @param int $range
|
2016-02-17 05:05:41 -06:00
|
|
|
*/
|
2016-02-17 13:25:54 -06:00
|
|
|
public function setRange($range)
|
2016-02-17 10:32:02 -06:00
|
|
|
{
|
2016-02-17 13:25:54 -06:00
|
|
|
$this->range = $range;
|
2016-02-17 05:05:41 -06:00
|
|
|
}
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
/**
|
2016-02-17 13:25:54 -06:00
|
|
|
* @return int
|
2016-02-17 05:05:41 -06:00
|
|
|
*/
|
2016-02-17 13:25:54 -06:00
|
|
|
public function getLimit()
|
2016-02-17 10:32:02 -06:00
|
|
|
{
|
2016-02-17 13:25:54 -06:00
|
|
|
return $this->limit;
|
|
|
|
}
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 13:25:54 -06:00
|
|
|
/**
|
|
|
|
* @param int $limit
|
|
|
|
*/
|
|
|
|
public function setLimit($limit)
|
|
|
|
{
|
|
|
|
$this->limit = $limit;
|
2016-02-17 05:05:41 -06:00
|
|
|
}
|
2016-02-17 10:32:02 -06:00
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-02-17 10:32:02 -06:00
|
|
|
public function getTriggers()
|
|
|
|
{
|
|
|
|
return $this->triggers;
|
2016-02-17 05:05:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-17 10:32:02 -06:00
|
|
|
* @param array $triggers
|
2016-02-17 05:05:41 -06:00
|
|
|
*/
|
2016-02-17 10:32:02 -06:00
|
|
|
public function setTriggers($triggers)
|
|
|
|
{
|
|
|
|
$this->triggers = $triggers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 05:05:41 -06:00
|
|
|
}
|