Failed Conditions
Push — master ( 3e915b...b75901 )
by Sylvain
08:58
created

Account::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\DBAL\Types\AccountTypeType;
8
use Application\Traits\HasIban;
9
use Application\Traits\HasName;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Collection;
12
use Doctrine\ORM\Mapping as ORM;
13
use GraphQL\Doctrine\Annotation as API;
14
15
/**
16
 * Financial account
17
 *
18
 * @ORM\Entity(repositoryClass="Application\Repository\AccountRepository")
19
 * @ORM\AssociationOverrides({
20
 *     @ORM\AssociationOverride(
21
 *         name="owner",
22
 *         inversedBy="accounts",
23
 *         joinColumns=@ORM\JoinColumn(unique=true, onDelete="SET NULL")
24
 *     )
25
 * })
26
 */
27
class Account extends AbstractModel
28
{
29
    use HasName;
30
    use HasIban;
31
32
    /**
33
     * @var string
34
     *
35
     * @ORM\Column(type="decimal", precision=10, scale=2, options={"default" = "0.00"})
36
     */
37
    private $balance = '0.00';
38
39
    /**
40
     * @var Account
41
     * @ORM\ManyToOne(targetEntity="Account", inversedBy="children")
42
     * @ORM\JoinColumns({
43
     *     @ORM\JoinColumn(onDelete="CASCADE")
44
     * })
45
     */
46
    private $parent;
47
48
    /**
49
     * @var Collection
50
     * @ORM\OneToMany(targetEntity="Account", mappedBy="parent")
51
     * @ORM\OrderBy({"code" = "ASC"})
52
     */
53
    private $children;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(type="AccountType", length=10)
59
     */
60
    private $type;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(type="string", length=10, nullable=false, unique=true)
66
     */
67
    private $code;
68
69
    /**
70
     * @var Collection
71
     * @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="debit")
72
     */
73
    private $debitTransactionLines;
74
75
    /**
76
     * @var Collection
77
     * @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="credit")
78
     */
79
    private $creditTransactionLines;
80
81
    /**
82
     * Constructor
83
     */
84 20
    public function __construct()
85
    {
86 20
        $this->children = new ArrayCollection();
87 20
        $this->debitTransactionLines = new ArrayCollection();
88 20
        $this->creditTransactionLines = new ArrayCollection();
89 20
    }
90
91
    /**
92
     * Assign the account to an user
93
     *
94
     * @param null|User $owner
95
     */
96 12
    public function setOwner(User $owner = null): void
97
    {
98 12
        if ($this->getOwner()) {
99 2
            $this->getOwner()->accountRemoved();
100
        }
101
102 12
        parent::setOwner($owner);
103
104 12
        if ($this->getOwner()) {
105 12
            $owner->accountAdded($this);
0 ignored issues
show
Bug introduced by
The method accountAdded() does not exist on null. ( Ignorable by Annotation )

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

105
            $owner->/** @scrutinizer ignore-call */ 
106
                    accountAdded($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
        }
107 12
    }
108
109
    /**
110
     * Set balance
111
     *
112
     * @param string $balance
113
     *
114
     * @API\Exclude
115
     */
116 2
    public function setBalance(string $balance): void
117
    {
118 2
        $this->balance = $balance;
119 2
    }
120
121
    /**
122
     * @return string
123
     */
124 7
    public function getBalance(): string
125
    {
126 7
        if ($this->type === AccountTypeType::GROUP) {
127
            return _em()->getRepository(self::class)->totalBalanceByParent($this);
128
        }
129
130 7
        return $this->balance;
131
    }
132
133
    /**
134
     * Notify that an user was added
135
     *
136
     * @param null|User $user
137
     */
138
    public function userAdded(?User $user): void
139
    {
140
        $this->user = $user;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
141
    }
142
143
    /**
144
     * Set parent
145
     *
146
     * @param null|Account $parent
147
     */
148 9
    public function setParent(?self $parent): void
149
    {
150 9
        if ($this->getParent()) {
151 1
            $this->getParent()->getChildren()->removeElement($this);
152
        }
153
154 9
        $this->parent = $parent;
155
156 9
        if ($this->getParent()) {
157 9
            $this->getParent()->getChildren()->add($this);
158
        }
159 9
    }
160
161
    /**
162
     * @return null|Account
163
     */
164 9
    public function getParent(): ?self
165
    {
166 9
        return $this->parent;
167
    }
168
169
    /**
170
     * @return Collection
171
     */
172 9
    public function getChildren(): Collection
173
    {
174 9
        return $this->children;
175
    }
176
177
    /**
178
     * Set type
179
     *
180
     * @API\Input(type="AccountType")
181
     *
182
     * @param string $type
183
     */
184 8
    public function setType(string $type): void
185
    {
186 8
        $this->type = $type;
187 8
    }
188
189
    /**
190
     * Get type
191
     *
192
     * @API\Field(type="AccountType")
193
     *
194
     * @return string
195
     */
196 2
    public function getType(): string
197
    {
198 2
        return $this->type;
199
    }
200
201
    /**
202
     * Set code
203
     *
204
     * @param string $code
205
     */
206 8
    public function setCode(string $code): void
207
    {
208 8
        $this->code = $code;
209 8
    }
210
211
    /**
212
     * Get code
213
     *
214
     * @return string
215
     */
216 1
    public function getCode(): string
217
    {
218 1
        return $this->code;
219
    }
220
221
    /**
222
     * Notify when a transaction line is added
223
     * This should only be called by TransactionLine::setDebit()
224
     *
225
     * @param TransactionLine $transactionLine
226
     */
227 14
    public function debitTransactionLineAdded(TransactionLine $transactionLine): void
228
    {
229 14
        $this->debitTransactionLines->add($transactionLine);
230 14
    }
231
232
    /**
233
     * Notify when a transaction line is removed
234
     * This should only be called by TransactionLine::setDebit()
235
     *
236
     * @param TransactionLine $transactionLine
237
     */
238 1
    public function debitTransactionLineRemoved(TransactionLine $transactionLine): void
239
    {
240 1
        $this->debitTransactionLines->removeElement($transactionLine);
241 1
    }
242
243
    /**
244
     * @return Collection
245
     */
246 5
    public function getDebitTransactionLines(): Collection
247
    {
248 5
        return $this->debitTransactionLines;
249
    }
250
251
    /**
252
     * Notify when a transaction line is added
253
     * This should only be called by TransactionLine::setCredit()
254
     *
255
     * @param TransactionLine $transactionLine
256
     */
257 16
    public function creditTransactionLineAdded(TransactionLine $transactionLine): void
258
    {
259 16
        $this->creditTransactionLines->add($transactionLine);
260 16
    }
261
262
    /**
263
     * Notify when a transaction line is removed
264
     * This should only be called by TransactionLine::setCredit()
265
     *
266
     * @param TransactionLine $transactionLine
267
     */
268 1
    public function creditTransactionLineRemoved(TransactionLine $transactionLine): void
269
    {
270 1
        $this->creditTransactionLines->removeElement($transactionLine);
271 1
    }
272
273
    /**
274
     * @return Collection
275
     */
276 5
    public function getCreditTransactionLines(): Collection
277
    {
278 5
        return $this->creditTransactionLines;
279
    }
280
}
281