Passed
Push — master ( 01d0a8...1f5da7 )
by Sylvain
07:16
created

Account   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 43
c 2
b 0
f 1
dl 0
loc 278
ccs 60
cts 62
cp 0.9677
rs 10
wmc 24

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setOwner() 0 10 3
A getFullName() 0 3 1
A getType() 0 3 1
A creditTransactionLineAdded() 0 3 1
A getBalance() 0 3 1
A getChildren() 0 3 1
A getTotalBalance() 0 3 1
A setParent() 0 10 3
A debitTransactionLineAdded() 0 3 1
A setCode() 0 3 1
A debitTransactionLineRemoved() 0 3 1
A getDebitTransactionLines() 0 3 1
A setBalance() 0 3 1
A creditTransactionLineRemoved() 0 3 1
A getParent() 0 3 1
A setType() 0 3 1
A getCode() 0 3 1
A getCreditTransactionLines() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Traits\HasIban;
8
use Application\Traits\HasName;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Common\Collections\Collection;
11
use Doctrine\ORM\Mapping as ORM;
12
use GraphQL\Doctrine\Annotation as API;
13
use Money\Money;
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 Money
34
     *
35
     * @ORM\Column(type="Money", options={"default" = 0})
36
     */
37
    private $balance;
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 int
64
     *
65
     * @ORM\Column(type="integer", nullable=false, unique=true, options={"unsigned" = 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
     * @var Money
83
     *
84
     * @ORM\Column(type="Money", options={"default" = 0})
85
     */
86
    private $totalBalance;
87
88
    /**
89
     * Constructor
90
     */
91 21
    public function __construct()
92
    {
93 21
        $this->balance = Money::CHF(0);
94 21
        $this->totalBalance = Money::CHF(0);
95 21
        $this->children = new ArrayCollection();
96 21
        $this->debitTransactionLines = new ArrayCollection();
97 21
        $this->creditTransactionLines = new ArrayCollection();
98 21
    }
99
100
    /**
101
     * Get full name including code and name
102
     *
103
     * @return string
104
     */
105
    public function getFullName(): string
106
    {
107
        return implode(' - ', array_filter([$this->getCode(), $this->getName()]));
108
    }
109
110
    /**
111
     * Assign the account to an user
112
     *
113
     * @param null|User $owner
114
     */
115 13
    public function setOwner(?User $owner): void
116
    {
117 13
        if ($this->getOwner()) {
118 1
            $this->getOwner()->accountRemoved();
119
        }
120
121 13
        parent::setOwner($owner);
122
123 13
        if ($this->getOwner()) {
124 13
            $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

124
            $owner->/** @scrutinizer ignore-call */ 
125
                    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...
125
        }
126 13
    }
127
128
    /**
129
     * Only members' liability accounts must have an owner
130
     * and there must be only an account per member
131
     *
132
     * @return null|User
133
     */
134 9
    public function getOwnerForCreation(): ?User
135
    {
136 9
        return null;
137
    }
138
139
    /**
140
     * Set balance
141
     *
142
     * @param Money $balance
143
     *
144
     * @API\Exclude
145
     */
146 2
    public function setBalance(Money $balance): void
147
    {
148 2
        $this->balance = $balance;
149 2
    }
150
151
    /**
152
     * @return Money
153
     */
154 8
    public function getBalance(): Money
155
    {
156 8
        return $this->balance;
157
    }
158
159
    /**
160
     * Total balance, recursively including all child account if this account is a group
161
     *
162
     * @return Money
163
     */
164 1
    public function getTotalBalance(): Money
165
    {
166 1
        return $this->totalBalance;
167
    }
168
169
    /**
170
     * Set parent
171
     *
172
     * @param null|Account $parent
173
     */
174 10
    public function setParent(?self $parent): void
175
    {
176 10
        if ($this->getParent()) {
177 1
            $this->getParent()->getChildren()->removeElement($this);
178
        }
179
180 10
        $this->parent = $parent;
181
182 10
        if ($this->getParent()) {
183 10
            $this->getParent()->getChildren()->add($this);
184
        }
185 10
    }
186
187
    /**
188
     * @return null|Account
189
     */
190 10
    public function getParent(): ?self
191
    {
192 10
        return $this->parent;
193
    }
194
195
    /**
196
     * @return Collection
197
     */
198 10
    public function getChildren(): Collection
199
    {
200 10
        return $this->children;
201
    }
202
203
    /**
204
     * Set type
205
     *
206
     * @API\Input(type="AccountType")
207
     *
208
     * @param string $type
209
     */
210 9
    public function setType(string $type): void
211
    {
212 9
        $this->type = $type;
213 9
    }
214
215
    /**
216
     * Get type
217
     *
218
     * @API\Field(type="AccountType")
219
     *
220
     * @return string
221
     */
222 2
    public function getType(): string
223
    {
224 2
        return $this->type;
225
    }
226
227
    /**
228
     * Set code
229
     *
230
     * @param int $code
231
     */
232 9
    public function setCode(int $code): void
233
    {
234 9
        $this->code = $code;
235 9
    }
236
237
    /**
238
     * Get code
239
     *
240
     * @return int
241
     */
242 8
    public function getCode(): int
243
    {
244 8
        return $this->code;
245
    }
246
247
    /**
248
     * Notify when a transaction line is added
249
     * This should only be called by TransactionLine::setDebit()
250
     *
251
     * @param TransactionLine $transactionLine
252
     */
253 17
    public function debitTransactionLineAdded(TransactionLine $transactionLine): void
254
    {
255 17
        $this->debitTransactionLines->add($transactionLine);
256 17
    }
257
258
    /**
259
     * Notify when a transaction line is removed
260
     * This should only be called by TransactionLine::setDebit()
261
     *
262
     * @param TransactionLine $transactionLine
263
     */
264 1
    public function debitTransactionLineRemoved(TransactionLine $transactionLine): void
265
    {
266 1
        $this->debitTransactionLines->removeElement($transactionLine);
267 1
    }
268
269
    /**
270
     * @return Collection
271
     */
272 5
    public function getDebitTransactionLines(): Collection
273
    {
274 5
        return $this->debitTransactionLines;
275
    }
276
277
    /**
278
     * Notify when a transaction line is added
279
     * This should only be called by TransactionLine::setCredit()
280
     *
281
     * @param TransactionLine $transactionLine
282
     */
283 17
    public function creditTransactionLineAdded(TransactionLine $transactionLine): void
284
    {
285 17
        $this->creditTransactionLines->add($transactionLine);
286 17
    }
287
288
    /**
289
     * Notify when a transaction line is removed
290
     * This should only be called by TransactionLine::setCredit()
291
     *
292
     * @param TransactionLine $transactionLine
293
     */
294 1
    public function creditTransactionLineRemoved(TransactionLine $transactionLine): void
295
    {
296 1
        $this->creditTransactionLines->removeElement($transactionLine);
297 1
    }
298
299
    /**
300
     * @return Collection
301
     */
302 5
    public function getCreditTransactionLines(): Collection
303
    {
304 5
        return $this->creditTransactionLines;
305
    }
306
}
307