Skip to content

Commit

Permalink
Authorizator: Add support for fulltext search.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Sep 21, 2019
1 parent 78f6e20 commit f6047c4
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/FioPaymentAuthorizator.php
Expand Up @@ -56,16 +56,30 @@ public function process(): TransactionResult
*/
public function authOrders(array $unauthorizedVariables, callable $callback, string $currency = 'CZK', float $tolerance = 1): void
{
foreach ($this->process()->getTransactions() as $transaction) {
if (($variable = $transaction->getVariableSymbol()) !== null && isset($unauthorizedVariables[$variable])) {
$price = (float) $unauthorizedVariables[$variable];
if ($transaction->getCurrency() !== $currency) { // Fix different currencies
$price = $this->convertCurrency($transaction->getCurrency(), $currency, $price);
}
if ($transaction->getPrice() - $price >= -$tolerance) { // Is price in tolerance?
$callback($transaction);
$transactions = $this->process()->getTransactions();
$variables = array_keys($unauthorizedVariables);

$process = static function (float $price, Transaction $transaction) use ($callback, $currency, $tolerance): void {
if ($transaction->getCurrency() !== $currency) { // Fix different currencies
$price = $this->convertCurrency($transaction->getCurrency(), $currency, $price);
}
if ($transaction->getPrice() - $price >= -$tolerance) { // Is price in tolerance?
$callback($transaction);
}
};

foreach ($transactions as $transaction) {
$variable = null;
foreach ($variables as $currentVariable) {
if ($transaction->isVariableSymbol((int) $currentVariable) === true) {
$variable = (int) $currentVariable;
break;
}
}

if ($variable !== null) {
$process((float) $unauthorizedVariables[$variable], $transaction);
}
}
}

Expand All @@ -74,6 +88,8 @@ public function authOrders(array $unauthorizedVariables, callable $callback, str
*/
private function loadData(): string
{
static $staticCache = [];

$year = (int) date('Y');
if (($month = (int) date('m') - 1) === 0) {
$year--;
Expand All @@ -84,12 +100,19 @@ private function loadData(): string
. '/' . $year . '-' . $month . '-01/' . date('Y-m-d')
. '/transactions.csv';

if (isset($staticCache[$url]) === true) {
return $staticCache[$url];
}

if ($this->cache !== null && ($cache = $this->cache->load($url)) !== null) {
$staticCache[$url] = $cache;

return $cache;
}

$data = file_get_contents($url);

$staticCache[$url] = $data;
if ($this->cache !== null) {
$this->cache->save($url, $data, [
Cache::EXPIRE => '15 minutes',
Expand Down

0 comments on commit f6047c4

Please sign in to comment.