Small code clean up [skip ci]

This commit is contained in:
James Cole
2016-02-17 17:32:02 +01:00
parent f9e2a677d9
commit 8cfe25bfac

View File

@@ -12,6 +12,7 @@ namespace FireflyIII\Rules;
use FireflyIII\Models\Rule; use FireflyIII\Models\Rule;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
/** /**
* Class TransactionMatcher is used to find a list of * Class TransactionMatcher is used to find a list of
@@ -21,20 +22,17 @@ use FireflyIII\Models\TransactionType;
*/ */
class TransactionMatcher class TransactionMatcher
{ {
/** @var array List of triggers to match*/ /** @var int Maximum number of transaction to search in (for performance reasons) * */
protected $triggers = [];
/** @var int Maximum number of transaction to search in (for performance reasons) **/
protected $maxTransactionsToSearchIn = 1000; protected $maxTransactionsToSearchIn = 1000;
/** @var array */ /** @var array */
protected $transactionTypes = [ TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER ]; protected $transactionTypes = [TransactionType::DEPOSIT, TransactionType::WITHDRAWAL, TransactionType::TRANSFER];
/** @var array List of triggers to match */
protected $triggers = [];
/** /**
* Default constructor * Default constructor
* *
* @param Rule $rule * @param $triggers
* @param TransactionJournal $journal
*/ */
public function __construct($triggers) public function __construct($triggers)
{ {
@@ -43,9 +41,13 @@ class TransactionMatcher
/** /**
* Find matching transactions for the current set of triggers * Find matching transactions for the current set of triggers
* @param number $maxResults The maximum number of transactions returned *
* @param int $maxResults The maximum number of transactions returned
*
* @return array
*/ */
public function findMatchingTransactions($maxResults = 50) { public function findMatchingTransactions($maxResults = 50)
{
/** @var JournalRepositoryInterface $repository */ /** @var JournalRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); $repository = app('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
@@ -60,12 +62,12 @@ class TransactionMatcher
// Variables used within the loop // Variables used within the loop
$numTransactionsProcessed = 0; $numTransactionsProcessed = 0;
$page = 1; $page = 1;
$matchingTransactions = []; $matchingTransactions = [];
// Flags to indicate the end of the loop // Flags to indicate the end of the loop
$reachedEndOfList = false; $reachedEndOfList = false;
$foundEnoughTransactions = false; $foundEnoughTransactions = false;
$searchedEnoughTransactions = false; $searchedEnoughTransactions = false;
// Start a loop to fetch batches of transactions. The loop will finish if: // Start a loop to fetch batches of transactions. The loop will finish if:
@@ -74,14 +76,17 @@ class TransactionMatcher
// - the maximum number of transactions to search in have been searched // - the maximum number of transactions to search in have been searched
do { do {
// Fetch a batch of transactions from the database // Fetch a batch of transactions from the database
$offset = $page > 0 ? ($page - 1) * $pagesize : 0; $offset = $page > 0 ? ($page - 1) * $pagesize : 0;
$transactions = $repository->getJournalsOfTypes( $this->transactionTypes, $offset, $page, $pagesize)->getCollection()->all(); $transactions = $repository->getJournalsOfTypes($this->transactionTypes, $offset, $page, $pagesize)->getCollection()->all();
// Filter transactions that match the rule // Filter transactions that match the rule
$matchingTransactions += array_filter( $transactions, function($transaction) { $matchingTransactions += array_filter(
$transactions, function ($transaction) {
$processor = new Processor(new Rule, $transaction); $processor = new Processor(new Rule, $transaction);
return $processor->isTriggeredBy($this->triggers); return $processor->isTriggeredBy($this->triggers);
}); }
);
// Update counters // Update counters
$page++; $page++;
@@ -90,8 +95,8 @@ class TransactionMatcher
// Check for conditions to finish the loop // Check for conditions to finish the loop
$reachedEndOfList = (count($transactions) < $pagesize); $reachedEndOfList = (count($transactions) < $pagesize);
$foundEnoughTransactions = (count($matchingTransactions) >= $maxResults); $foundEnoughTransactions = (count($matchingTransactions) >= $maxResults);
$searchedEnoughTransactions = ($numTransactionsProcessed >= $this->maxTransactionsToSearchIn); $searchedEnoughTransactions = ($numTransactionsProcessed >= $this->maxTransactionsToSearchIn);
} while( !$reachedEndOfList && !$foundEnoughTransactions && !$searchedEnoughTransactions); } while (!$reachedEndOfList && !$foundEnoughTransactions && !$searchedEnoughTransactions);
// If the list of matchingTransactions is larger than the maximum number of results // 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 // (e.g. if a large percentage of the transactions match), truncate the list
@@ -103,45 +108,60 @@ class TransactionMatcher
/** /**
* @return array * @return array
*/ */
public function getTriggers() { public function getTransactionLimit()
return $this->triggers; {
}
/**
* @param array $triggers
*/
public function setTriggers($triggers) {
$this->triggers = $triggers;
return $this;
}
/**
* @return array
*/
public function getTransactionLimit() {
return $this->maxTransactionsToSearchIn; return $this->maxTransactionsToSearchIn;
} }
/**
* @param int $limit
*/
public function setTransactionLimit(int $limit) {
$this->maxTransactionsToSearchIn = $limit;
return $this;
}
/** /**
* @return array * @return array
*/ */
public function getTransactionTypes() { public function getTransactionTypes()
{
return $this->transactionTypes; return $this->transactionTypes;
} }
/** /**
* @param array $transactionTypes * @param array $transactionTypes
*
* @return $this
*/ */
public function setTransactionTypes(array $transactionTypes) { public function setTransactionTypes(array $transactionTypes)
{
$this->transactionTypes = $transactionTypes; $this->transactionTypes = $transactionTypes;
return $this;
}
/**
* @return array
*/
public function getTriggers()
{
return $this->triggers;
}
/**
* @param array $triggers
*
* @return $this
*/
public function setTriggers($triggers)
{
$this->triggers = $triggers;
return $this;
}
/**
* @param int $limit
*
* @return $this
*/
public function setTransactionLimit(int $limit)
{
$this->maxTransactionsToSearchIn = $limit;
return $this; return $this;
} }