Clean up code.

This commit is contained in:
James Cole
2020-10-24 07:40:56 +02:00
parent 048e5eeb31
commit ab4bcd3703
20 changed files with 126 additions and 245 deletions

View File

@@ -81,6 +81,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* OperatorQuerySearch constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
@@ -194,6 +195,7 @@ class OperatorQuerySearch implements SearchInterface
if (0 === count($this->getWords()) && 0 === count($this->getOperators())) {
return new LengthAwarePaginator;
}
return $this->collector->getPaginatedGroups();
}
@@ -212,12 +214,13 @@ class OperatorQuerySearch implements SearchInterface
$this->collector->setUser($this->user);
$this->collector->withAccountInformation()->withCategoryInformation()->withBudgetInformation();
$this->setLimit((int) app('preferences')->getForUser($user, 'listPageSize', 50)->data);
$this->setLimit((int)app('preferences')->getForUser($user, 'listPageSize', 50)->data);
}
/**
* @param Node $searchNode
*
* @throws FireflyException
*/
private function handleSearchNode(Node $searchNode): void
@@ -237,7 +240,7 @@ class OperatorQuerySearch implements SearchInterface
case Emoji::class:
case Mention::class:
Log::debug(sprintf('Now handle %s', $class));
$this->words[] = (string) $searchNode->getValue();
$this->words[] = (string)$searchNode->getValue();
break;
case Field::class:
Log::debug(sprintf('Now handle %s', $class));
@@ -246,15 +249,13 @@ class OperatorQuerySearch implements SearchInterface
$operator = strtolower($searchNode->getValue());
$value = $searchNode->getNode()->getValue();
// must be valid operator:
if (in_array($operator, $this->validOperators, true)) {
if ($this->updateCollector($operator, (string) $value)) {
$this->operators->push(
[
'type' => self::getRootOperator($operator),
'value' => (string) $value,
]
);
}
if (in_array($operator, $this->validOperators, true) && $this->updateCollector($operator, (string)$value)) {
$this->operators->push(
[
'type' => self::getRootOperator($operator),
'value' => (string)$value,
]
);
}
break;
}
@@ -264,6 +265,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $operator
* @param string $value
*
* @return bool
* @throws FireflyException
*/
@@ -283,6 +285,7 @@ class OperatorQuerySearch implements SearchInterface
// some search operators are ignored, basically:
case 'user_action':
Log::info(sprintf('Ignore search operator "%s"', $operator));
return false;
//
// all account related searches:
@@ -312,7 +315,7 @@ class OperatorQuerySearch implements SearchInterface
$this->searchAccount($value, 1, 3);
break;
case 'source_account_id':
$account = $this->accountRepository->findNull((int) $value);
$account = $this->accountRepository->findNull((int)$value);
if (null !== $account) {
$this->collector->setSourceAccounts(new Collection([$account]));
}
@@ -346,7 +349,7 @@ class OperatorQuerySearch implements SearchInterface
$this->searchAccount($value, 2, 3);
break;
case 'destination_account_id':
$account = $this->accountRepository->findNull((int) $value);
$account = $this->accountRepository->findNull((int)$value);
if (null !== $account) {
$this->collector->setDestinationAccounts(new Collection([$account]));
}
@@ -355,7 +358,7 @@ class OperatorQuerySearch implements SearchInterface
$parts = explode(',', $value);
$collection = new Collection;
foreach ($parts as $accountId) {
$account = $this->accountRepository->findNull((int) $accountId);
$account = $this->accountRepository->findNull((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
@@ -390,6 +393,7 @@ class OperatorQuerySearch implements SearchInterface
break;
case 'description_contains':
$this->words[] = $value;
return false;
case 'description_is':
$this->collector->descriptionIs($value);
@@ -494,17 +498,17 @@ class OperatorQuerySearch implements SearchInterface
// amount
//
case 'amount_exactly':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountIs($amount);
break;
case 'amount_less':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountLess($amount);
break;
case 'amount_more':
$amount = app('steam')->positive((string) $value);
$amount = app('steam')->positive((string)$value);
Log::debug(sprintf('Set "%s" using collector with value "%s"', $operator, $amount));
$this->collector->amountMore($amount);
break;
@@ -520,7 +524,12 @@ class OperatorQuerySearch implements SearchInterface
//
case 'date_is':
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
$this->collector->setRange($range['start'], $range['end']);
// add to operators manually:
@@ -531,7 +540,12 @@ class OperatorQuerySearch implements SearchInterface
case 'date_before':
Log::debug(sprintf('Value for date_before is "%s"', $value));
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
// add to operators manually:
$this->operators->push(['type' => 'date_before', 'value' => $range['start']->format('Y-m-d'),]);
@@ -541,7 +555,12 @@ class OperatorQuerySearch implements SearchInterface
case 'date_after':
Log::debug(sprintf('Value for date_after is "%s"', $value));
$range = $this->parseDateRange($value);
Log::debug(sprintf('Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'), $range['end']->format('Y-m-d')));
Log::debug(
sprintf(
'Set "%s" using collector with value "%s" (%s - %s)', $operator, $value, $range['start']->format('Y-m-d'),
$range['end']->format('Y-m-d')
)
);
// add to operators manually:
$this->operators->push(['type' => 'date_after', 'value' => $range['end']->format('Y-m-d'),]);
@@ -568,12 +587,14 @@ class OperatorQuerySearch implements SearchInterface
$this->collector->setInternalReference($value);
break;
}
return true;
}
/**
* searchDirection: 1 = source (default), 2 = destination
* stringPosition: 1 = start (default), 2 = end, 3 = contains, 4 = is
*
* @param string $value
* @param int $searchDirection
* @param int $stringPosition
@@ -617,15 +638,18 @@ class OperatorQuerySearch implements SearchInterface
return;
}
Log::debug(sprintf('Found %d accounts, will filter.', $accounts->count()));
$filtered = $accounts->filter(function (Account $account) use ($value, $stringMethod) {
return $stringMethod(strtolower($account->name), strtolower($value));
});
$filtered = $accounts->filter(
function (Account $account) use ($value, $stringMethod) {
return $stringMethod(strtolower($account->name), strtolower($value));
}
);
if (0 === $filtered->count()) {
Log::debug('Left with zero accounts, search for invalid account.');
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
Log::debug(sprintf('Left with %d, set as %s().', $filtered->count(), $collectorMethod));
@@ -636,6 +660,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* searchDirection: 1 = source (default), 2 = destination
* stringPosition: 1 = start (default), 2 = end, 3 = contains, 4 = is
*
* @param string $value
* @param int $searchDirection
* @param int $stringPosition
@@ -675,29 +700,34 @@ class OperatorQuerySearch implements SearchInterface
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
// if found, do filter
Log::debug(sprintf('Found %d accounts, will filter.', $accounts->count()));
$filtered = $accounts->filter(function (Account $account) use ($value, $stringMethod) {
// either IBAN or account number!
$ibanMatch = $stringMethod(strtolower((string) $account->iban), strtolower((string) $value));
$accountNrMatch = false;
/** @var AccountMeta $meta */
foreach ($account->accountMeta as $meta) {
if ('account_number' === $meta->name && $stringMethod(strtolower($meta->data), strtolower($value))) {
$accountNrMatch = true;
$filtered = $accounts->filter(
function (Account $account) use ($value, $stringMethod) {
// either IBAN or account number!
$ibanMatch = $stringMethod(strtolower((string)$account->iban), strtolower((string)$value));
$accountNrMatch = false;
/** @var AccountMeta $meta */
foreach ($account->accountMeta as $meta) {
if ('account_number' === $meta->name && $stringMethod(strtolower($meta->data), strtolower($value))) {
$accountNrMatch = true;
}
}
return $ibanMatch || $accountNrMatch;
}
return $ibanMatch || $accountNrMatch;
});
);
if (0 === $filtered->count()) {
Log::debug('Left with zero, search for invalid account');
$account = new Account;
$account->id = 0;
$this->collector->$collectorMethod(new Collection([$account]));
return;
}
Log::debug(sprintf('Left with %d, set as %s().', $filtered->count(), $collectorMethod));
@@ -706,6 +736,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $value
*
* @return TransactionCurrency|null
*/
private function findCurrency(string $value): ?TransactionCurrency
@@ -721,11 +752,13 @@ class OperatorQuerySearch implements SearchInterface
if (null === $result) {
$result = $this->currencyRepository->findByNameNull($value);
}
return $result;
}
/**
* @param string $operator
*
* @return string
* @throws FireflyException
*/
@@ -737,6 +770,7 @@ class OperatorQuerySearch implements SearchInterface
}
if (true === $config['alias']) {
Log::debug(sprintf('"%s" is an alias for "%s", so return that instead.', $operator, $config['alias_for']));
return $config['alias_for'];
}
Log::debug(sprintf('"%s" is not an alias.', $operator));
@@ -746,6 +780,7 @@ class OperatorQuerySearch implements SearchInterface
/**
* @param string $value
*
* @return array
* @throws FireflyException
*/
@@ -756,6 +791,7 @@ class OperatorQuerySearch implements SearchInterface
return $parser->parseRange($value, today(config('app.timezone')));
}
$date = $parser->parseDate($value);
return [
'start' => $date,
'end' => $date,

View File

@@ -49,9 +49,6 @@ class Steam
*/
public function balance(Account $account, Carbon $date, ?TransactionCurrency $currency = null): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -105,9 +102,6 @@ class Steam
*/
public function balanceIgnoreVirtual(Account $account, Carbon $date): string
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -177,9 +171,6 @@ class Steam
*/
public function balanceInRange(Account $account, Carbon $start, Carbon $end, ?TransactionCurrency $currency = null): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -264,9 +255,6 @@ class Steam
*/
public function balancePerCurrency(Account $account, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
@@ -300,9 +288,6 @@ class Steam
*/
public function balancesByAccounts(Collection $accounts, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties;
@@ -335,9 +320,6 @@ class Steam
*/
public function balancesPerCurrencyByAccounts(Collection $accounts, Carbon $date): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$ids = $accounts->pluck('id')->toArray();
// cache this property.
$cache = new CacheProperties;
@@ -497,9 +479,6 @@ class Steam
*/
public function getLastActivities(array $accounts): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$list = [];
$set = auth()->user()->transactions()

View File

@@ -77,7 +77,7 @@ class Rule extends AbstractExtension
return new TwigFunction(
'allRuleTriggers',
static function () {
$ruleTriggers = array_keys(config('firefly.search.operators'));;
$ruleTriggers = array_keys(config('firefly.search.operators'));
$possibleTriggers = [];
foreach ($ruleTriggers as $key) {
if ('user_action' !== $key) {