firefly-iii/app/Rules/UniqueIban.php

153 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* UniqueIban.php
2020-02-16 06:56:25 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Rules;
use FireflyIII\Models\Account;
2018-03-19 02:16:54 -05:00
use FireflyIII\Models\AccountType;
use Illuminate\Contracts\Validation\Rule;
2018-03-19 02:16:54 -05:00
use Log;
/**
* Class UniqueIban
*/
class UniqueIban implements Rule
{
/** @var Account */
private $account;
2018-03-19 02:16:54 -05:00
/** @var string */
private $expectedType;
/**
* Create a new rule instance.
*
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*
2018-03-11 10:24:07 -05:00
* @param Account|null $account
2018-03-19 02:16:54 -05:00
* @param string|null $expectedType
*/
2018-03-19 02:16:54 -05:00
public function __construct(?Account $account, ?string $expectedType)
{
2018-03-19 02:16:54 -05:00
$this->account = $account;
$this->expectedType = $expectedType;
}
/**
* Get the validation error message.
*
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*
* @return string
*/
2018-07-25 23:10:17 -05:00
public function message(): string
{
2018-07-15 02:38:49 -05:00
return (string)trans('validation.unique_iban_for_user');
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
*
* @return bool
2019-08-17 03:47:29 -05:00
*
*/
2018-07-25 23:10:17 -05:00
public function passes($attribute, $value): bool
{
if (!auth()->check()) {
return true; // @codeCoverageIgnore
}
2018-04-02 07:50:17 -05:00
if (null === $this->expectedType) {
2019-07-31 09:53:09 -05:00
return true; // @codeCoverageIgnore
2018-03-19 02:16:54 -05:00
}
2018-07-25 23:10:17 -05:00
$maxCounts = $this->getMaxOccurrences();
2018-03-19 02:16:54 -05:00
foreach ($maxCounts as $type => $max) {
2018-07-25 23:10:17 -05:00
$count = $this->countHits($type, $value);
2019-07-31 09:53:09 -05:00
Log::debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
2018-03-19 02:16:54 -05:00
if ($count > $max) {
Log::debug(
sprintf(
'IBAN "%s" is in use with %d account(s) of type "%s", which is too much for expected type "%s"',
$value, $count, $type, $this->expectedType
)
);
return false;
}
}
return true;
}
2018-07-25 23:10:17 -05:00
/**
* @param string $type
* @param string $iban
*
* @return int
*/
private function countHits(string $type, string $iban): int
{
2019-07-31 09:53:09 -05:00
$query
= auth()->user()
->accounts()
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.iban', $iban)
->where('account_types.type', $type);
2018-07-25 23:10:17 -05:00
if (null !== $this->account) {
$query->where('accounts.id', '!=', $this->account->id);
}
2019-07-31 09:53:09 -05:00
return $query->count();
2018-07-25 23:10:17 -05:00
}
/**
* @return array
2019-08-17 03:47:29 -05:00
*
2018-07-25 23:10:17 -05:00
*/
private function getMaxOccurrences(): array
{
$maxCounts = [
AccountType::ASSET => 0,
AccountType::EXPENSE => 0,
AccountType::REVENUE => 0,
];
if ('expense' === $this->expectedType || AccountType::EXPENSE === $this->expectedType) {
// IBAN should be unique amongst expense and asset accounts.
// may appear once in revenue accounts
$maxCounts[AccountType::REVENUE] = 1;
}
2020-01-21 10:58:31 -06:00
if ('revenue' === $this->expectedType || AccountType::REVENUE === $this->expectedType) {
2018-07-25 23:10:17 -05:00
// IBAN should be unique amongst revenue and asset accounts.
// may appear once in expense accounts
$maxCounts[AccountType::EXPENSE] = 1;
}
return $maxCounts;
}
2018-03-05 12:35:58 -06:00
}