range / 2, $this->limit * 2); // Variables used within the loop $processed = 0; $page = 1; $result = new Collection(); $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: $result = $result->merge($filtered); // Update counters $page++; $processed += count($set); // Check for conditions to finish the loop $reachedEndOfList = (count($set) < $pagesize); $foundEnoughTransactions = (count($result) >= $this->limit); $searchedEnoughTransactions = ($processed >= $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 $result = $result->slice(0, $this->limit); return $result; } /** * @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; } }