GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Rules/UniqueIban.php (1 issue)

Severity
1
<?php
2
/**
3
 * UniqueIban.php
4
 * Copyright (c) 2018 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
declare(strict_types=1);
23
24
namespace FireflyIII\Rules;
25
26
use FireflyIII\Exceptions\FireflyException;
27
use FireflyIII\Models\Account;
28
use FireflyIII\Models\AccountType;
29
use Illuminate\Contracts\Validation\Rule;
30
use Log;
31
32
/**
33
 * Class UniqueIban
34
 */
35
class UniqueIban implements Rule
36
{
37
    /** @var Account */
38
    private $account;
39
40
    /** @var string */
41
    private $expectedType;
42
43
    /**
44
     * Create a new rule instance.
45
     *
46
     * @param Account|null $account
47
     * @param string|null  $expectedType
48
     */
49
    public function __construct(?Account $account, ?string $expectedType)
50
    {
51
        $this->account      = $account;
52
        $this->expectedType = $expectedType;
53
    }
54
55
    /**
56
     * Get the validation error message.
57
     *
58
     * @return string
59
     */
60
    public function message()
61
    {
62
        return trans('validation.unique_iban_for_user');
63
    }
64
65
    /**
66
     * Determine if the validation rule passes.
67
     *
68
     * @param  string $attribute
69
     * @param  mixed  $value
70
     *
71
     * @return bool
72
     * @throws FireflyException
73
     */
74
    public function passes($attribute, $value)
75
    {
76
        if (!auth()->check()) {
77
            return true; // @codeCoverageIgnore
78
        }
79
        if (is_null($this->expectedType)) {
0 ignored issues
show
The condition is_null($this->expectedType) is always false.
Loading history...
80
            return true;
81
        }
82
        $maxCounts = [
83
            AccountType::ASSET   => 0,
84
            AccountType::EXPENSE => 0,
85
            AccountType::REVENUE => 0,
86
        ];
87
        switch ($this->expectedType) {
88
            case 'asset':
89
            case AccountType::ASSET:
90
                // iban should be unique amongst asset accounts
91
                // should not be in use with expense or revenue accounts.
92
                // ie: must be totally unique.
93
                break;
94
            case 'expense':
95
            case AccountType::EXPENSE:
96
                // should be unique amongst expense and asset accounts.
97
                // may appear once in revenue accounts
98
                $maxCounts[AccountType::REVENUE] = 1;
99
                break;
100
            case 'revenue':
101
            case AccountType::REVENUE:
102
                // should be unique amongst revenue and asset accounts.
103
                // may appear once in expense accounts
104
                $maxCounts[AccountType::EXPENSE] = 1;
105
                break;
106
            default:
107
108
                throw new FireflyException(sprintf('UniqueIban cannot handle type "%s"', $this->expectedType));
109
        }
110
111
        foreach ($maxCounts as $type => $max) {
112
            $count = 0;
113
            $query = auth()->user()
114
                           ->accounts()
115
                           ->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
116
                           ->where('account_types.type', $type);
117
            if (!is_null($this->account)) {
118
                $query->where('accounts.id', '!=', $this->account->id);
119
            }
120
            $result = $query->get(['accounts.*']);
121
            foreach ($result as $account) {
122
                if ($account->iban === $value) {
123
                    $count++;
124
                }
125
            }
126
            if ($count > $max) {
127
                Log::debug(
128
                    sprintf(
129
                        'IBAN "%s" is in use with %d account(s) of type "%s", which is too much for expected type "%s"',
130
                        $value, $count, $type, $this->expectedType
131
                    )
132
                );
133
134
                return false;
135
            }
136
        }
137
138
        return true;
139
    }
140
}
141