firefly-iii/app/Api/V1/Requests/RuleTestRequest.php

150 lines
4.1 KiB
PHP
Raw Normal View History

2019-06-09 01:26:23 -05:00
<?php
2019-08-17 05:13:02 -05:00
2019-06-09 01:26:23 -05:00
/**
* RuleTestRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2019-06-09 01:26:23 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-06-09 01:26:23 -05:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-09 01:26:23 -05:00
*
* This program is distributed in the hope that it will be useful,
2019-06-09 01:26:23 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-06-09 01:26:23 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-09 01:26:23 -05:00
*/
2019-08-17 05:13:02 -05:00
declare(strict_types=1);
2019-06-09 01:26:23 -05:00
namespace FireflyIII\Api\V1\Requests;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2020-07-18 01:34:00 -05:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
2019-06-09 01:26:23 -05:00
use Illuminate\Support\Collection;
use Log;
/**
* Class RuleTestRequest
*/
class RuleTestRequest extends FormRequest
2019-06-09 01:26:23 -05:00
{
2020-07-18 01:34:00 -05:00
use ConvertsDataTypes;
2019-06-09 01:26:23 -05:00
/**
* Authorize logged in users.
*
* @return bool
*/
public function authorize(): bool
{
// Only allow authenticated users
return auth()->check();
}
/**
* @return array
*/
public function getTestParameters(): array
{
2020-03-15 02:16:16 -05:00
return [
2019-06-09 01:26:23 -05:00
'page' => $this->getPage(),
'start_date' => $this->getDate('start_date'),
'end_date' => $this->getDate('end_date'),
'search_limit' => $this->getSearchLimit(),
'trigger_limit' => $this->getTriggerLimit(),
'accounts' => $this->getAccounts(),
];
}
/**
* @return array
*/
public function rules(): array
{
return [];
}
2019-09-04 10:39:39 -05:00
/**
* @return Collection
*/
private function getAccounts(): Collection
{
$accountList = '' === (string) $this->query('accounts') ? [] : explode(',', $this->query('accounts'));
2019-09-04 10:39:39 -05:00
$accounts = new Collection;
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
foreach ($accountList as $accountId) {
Log::debug(sprintf('Searching for asset account with id "%s"', $accountId));
$account = $accountRepository->findNull((int) $accountId);
2019-09-04 10:39:39 -05:00
if ($this->validAccount($account)) {
/** @noinspection NullPointerExceptionInspection */
Log::debug(sprintf('Found account #%d ("%s") and its an asset account', $account->id, $account->name));
$accounts->push($account);
}
}
return $accounts;
}
2019-06-09 01:26:23 -05:00
/**
* @param string $field
2019-09-04 10:39:39 -05:00
*
2019-06-09 01:26:23 -05:00
* @return Carbon|null
*/
private function getDate(string $field): ?Carbon
{
/** @var Carbon $result */
$result = null === $this->query($field) ? null : Carbon::createFromFormat('Y-m-d', $this->query($field));
return $result;
}
/**
* @return int
*/
private function getPage(): int
{
return 0 === (int) $this->query('page') ? 1 : (int) $this->query('page');
2019-06-09 01:26:23 -05:00
}
/**
* @return int
*/
private function getSearchLimit(): int
{
return 0 === (int) $this->query('search_limit') ? (int) config('firefly.test-triggers.limit') : (int) $this->query('search_limit');
2019-06-09 01:26:23 -05:00
}
/**
* @return int
*/
private function getTriggerLimit(): int
{
return 0 === (int) $this->query('triggered_limit') ? (int) config('firefly.test-triggers.range') : (int) $this->query('triggered_limit');
2019-06-09 01:26:23 -05:00
}
/**
* @param Account|null $account
2019-09-04 10:39:39 -05:00
*
2019-06-09 01:26:23 -05:00
* @return bool
*/
private function validAccount(?Account $account): bool
{
return null !== $account && AccountType::ASSET === $account->accountType->type;
}
2019-08-17 05:09:03 -05:00
}