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/Factory/AccountFactory.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
/**
4
 * AccountFactory.php
5
 * Copyright (c) 2018 [email protected]
6
 *
7
 * This file is part of Firefly III.
8
 *
9
 * Firefly III is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Firefly III is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
24
namespace FireflyIII\Factory;
25
26
use FireflyIII\Models\Account;
27
use FireflyIII\Models\AccountType;
28
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
29
use FireflyIII\User;
30
31
/**
32
 * Factory to create or return accounts.
33
 *
34
 * Class AccountFactory
35
 */
36
class AccountFactory
37
{
38
    use AccountServiceTrait;
39
    /** @var User */
40
    private $user;
41
42
    /**
43
     * @param array $data
44
     *
45
     * @return Account
46
     * @throws \FireflyIII\Exceptions\FireflyException
47
     */
48
    public function create(array $data): Account
49
    {
50
        $type         = $this->getAccountType($data['account_type_id'], $data['accountType']);
51
        $data['iban'] = $this->filterIban($data['iban']);
52
53
54
        // account may exist already:
55
        $existingAccount = $this->find($data['name'], $type->type);
56
        if (null !== $existingAccount) {
57
            return $existingAccount;
58
        }
59
60
61
        // create it:
62
        $databaseData
63
            = [
64
            'user_id'         => $this->user->id,
65
            'account_type_id' => $type->id,
66
            'name'            => $data['name'],
67
            'virtual_balance' => strlen(strval($data['virtualBalance'])) === 0 ? '0' : $data['virtualBalance'],
68
            'active'          => true === $data['active'] ? true : false,
69
            'iban'            => $data['iban'],
70
        ];
71
72
        // remove virtual balance when not an asset account:
73
        if ($type->type !== AccountType::ASSET) {
74
            $databaseData['virtual_balance'] = '0';
75
        }
76
77
        $newAccount = Account::create($databaseData);
78
        $this->updateMetaData($newAccount, $data);
79
80
        if ($this->validIBData($data) && $type->type === AccountType::ASSET) {
81
            $this->updateIB($newAccount, $data);
82
        }
83
        if (!$this->validIBData($data) && $type->type === AccountType::ASSET) {
84
            $this->deleteIB($newAccount);
85
        }
86
        // update note:
87
        if (isset($data['notes'])) {
88
            $this->updateNote($newAccount, $data['notes']);
0 ignored issues
show
It seems like $data['notes'] can also be of type null; however, parameter $note of FireflyIII\Factory\AccountFactory::updateNote() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
            $this->updateNote($newAccount, /** @scrutinizer ignore-type */ $data['notes']);
Loading history...
89
        }
90
91
        return $newAccount;
92
    }
93
94
    /**
95
     * @param string $accountName
96
     * @param string $accountType
97
     *
98
     * @return Account|null
99
     */
100
    public function find(string $accountName, string $accountType): ?Account
101
    {
102
        $type     = AccountType::whereType($accountType)->first();
103
        $accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
104
105
        /** @var Account $object */
106
        foreach ($accounts as $object) {
107
            if ($object->name === $accountName) {
108
                return $object;
109
            }
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * @param string $accountName
117
     * @param string $accountType
118
     *
119
     * @return Account
120
     * @throws \FireflyIII\Exceptions\FireflyException
121
     * @throws \FireflyIII\Exceptions\FireflyException
122
     */
123
    public function findOrCreate(string $accountName, string $accountType): Account
124
    {
125
        $type     = AccountType::whereType($accountType)->first();
126
        $accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
127
128
        /** @var Account $object */
129
        foreach ($accounts as $object) {
130
            if ($object->name === $accountName) {
131
                return $object;
132
            }
133
        }
134
135
        return $this->create(
136
            [
137
                'user_id'         => $this->user->id,
138
                'name'            => $accountName,
139
                'account_type_id' => $type->id,
140
                'accountType'     => null,
141
                'virtualBalance'  => '0',
142
                'iban'            => null,
143
                'active'          => true,
144
            ]
145
        );
146
    }
147
148
    /**
149
     * @param User $user
150
     */
151
    public function setUser(User $user): void
152
    {
153
        $this->user = $user;
154
    }
155
156
    /**
157
     * @param int|null    $accountTypeId
158
     * @param null|string $accountType
159
     *
160
     * @return AccountType|null
161
     */
162
    protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
163
    {
164
        $accountTypeId = intval($accountTypeId);
165
        if ($accountTypeId > 0) {
166
            return AccountType::find($accountTypeId);
167
        }
168
        $type   = config('firefly.accountTypeByIdentifier.' . strval($accountType));
169
        $result = AccountType::whereType($type)->first();
170
        if (is_null($result) && !is_null($accountType)) {
171
            // try as full name:
172
            $result = AccountType::whereType($accountType)->first();
173
        }
174
175
        return $result;
176
177
    }
178
179
}
180