firefly-iii/app/Rules/TransactionMatcher.php

169 lines
4.5 KiB
PHP
Raw Normal View History

<?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\TransactionJournal;
use FireflyIII\Models\TransactionType;
2016-02-17 10:32:02 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
/**
2016-02-17 10:32:02 -06:00
* Class TransactionMatcher is used to find a list of
* transaction matching a set of triggers
*
* @package FireflyIII\Rules
*/
class TransactionMatcher
{
2016-02-17 13:24:59 -06:00
/** @var int */
private $limit = 10;
/** @var int Maximum number of transaction to search in (for performance reasons) * */
private $range = 200;
2016-02-17 23:54:50 -06:00
/** @var JournalRepositoryInterface */
private $repository;
/** @var array */
private $transactionTypes = [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
2016-02-17 10:32:02 -06:00
/** @var array List of triggers to match */
private $triggers = [];
/**
2016-02-17 23:54:50 -06:00
* TransactionMatcher constructor. Typehint the repository.
*
* @param JournalRepositoryInterface $repository
*/
public function __construct(JournalRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* This method will search the user's transaction journal (with an upper limit of $range) for
* transaction journals matching the given $triggers. This is accomplished by trying to fire these
* triggers onto each transaction journal until enough matches are found ($limit).
2016-02-17 10:32:02 -06:00
*
2016-02-17 23:24:39 -06:00
* @return Collection
2016-02-17 14:14:32 -06:00
*
*/
2016-02-17 23:24:39 -06:00
public function findMatchingTransactions(): Collection
2016-02-17 10:32:02 -06:00
{
2016-02-17 23:54:50 -06:00
if (count($this->triggers) === 0) {
return new Collection;
}
$pagesize = min($this->range / 2, $this->limit * 2);
2016-02-17 10:32:02 -06:00
// Variables used within the loop
$processed = 0;
$page = 1;
$result = new Collection();
$processor = Processor::makeFromStringArray($this->triggers);
2016-02-17 10:32:02 -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-05-01 02:42:08 -05:00
$paginator = $this->repository->getJournals($this->transactionTypes, $page, $pagesize);
$set = $paginator->getCollection();
// Filter transactions that match the given triggers.
$filtered = $set->filter(
function (TransactionJournal $journal) use ($processor) {
return $processor->handleTransactionJournal($journal);
}
);
2016-02-17 10:32:02 -06:00
// merge:
$result = $result->merge($filtered);
2016-02-17 10:32:02 -06:00
// Update counters
$page++;
$processed += count($set);
2016-02-17 10:32:02 -06:00
// Check for conditions to finish the loop
2016-02-17 23:15:01 -06:00
$reachedEndOfList = $set->count() < $pagesize;
$foundEnough = $result->count() >= $this->limit;
$searchedEnough = ($processed >= $this->range);
} while (!$reachedEndOfList && !$foundEnough && !$searchedEnough);
2016-02-17 10:32:02 -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
$result = $result->slice(0, $this->limit);
2016-02-17 10:32:02 -06:00
return $result;
}
2016-02-17 10:32:02 -06:00
/**
2016-02-17 13:25:54 -06:00
* @return int
*/
2016-02-17 23:24:39 -06:00
public function getLimit(): int
2016-02-17 10:32:02 -06:00
{
return $this->limit;
}
/**
* @param int $limit
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
*/
2016-04-05 15:00:03 -05:00
public function setLimit(int $limit): TransactionMatcher
2016-02-17 10:32:02 -06:00
{
$this->limit = $limit;
2016-02-17 23:24:39 -06:00
return $this;
}
2016-02-17 10:32:02 -06:00
/**
2016-02-17 13:25:54 -06:00
* @return int
*/
2016-02-17 23:24:39 -06:00
public function getRange(): int
2016-02-17 10:32:02 -06:00
{
return $this->range;
2016-02-17 13:25:54 -06:00
}
2016-02-17 10:32:02 -06:00
2016-02-17 13:25:54 -06:00
/**
* @param int $range
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
2016-02-17 13:25:54 -06:00
*/
2016-04-05 15:00:03 -05:00
public function setRange(int $range): TransactionMatcher
2016-02-17 13:25:54 -06:00
{
$this->range = $range;
2016-02-17 23:24:39 -06:00
return $this;
}
2016-02-17 10:32:02 -06:00
/**
* @return array
*/
2016-02-17 23:24:39 -06:00
public function getTriggers(): array
2016-02-17 10:32:02 -06:00
{
return $this->triggers;
}
/**
2016-02-17 10:32:02 -06:00
* @param array $triggers
2016-02-17 23:24:39 -06:00
*
* @return TransactionMatcher
*/
2016-04-05 15:00:03 -05:00
public function setTriggers(array $triggers): TransactionMatcher
2016-02-17 10:32:02 -06:00
{
$this->triggers = $triggers;
2016-02-17 23:24:39 -06:00
return $this;
2016-02-17 10:32:02 -06:00
}
}