Failed Conditions
Push — master ( 6eb324...31d0cd )
by Adrien
09:17
created

Account::getFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 20
    public function __construct()
92
    {
93 20
        $this->balance = Money::CHF(0);
94 20
        $this->totalBalance = Money::CHF(0);
95 20
        $this->children = new ArrayCollection();
96 20
        $this->debitTransactionLines = new ArrayCollection();
97 20
        $this->creditTransactionLines = new ArrayCollection();
98 20
    }
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 12
    public function setOwner(?User $owner): void
116
    {
117 12
        if ($this->getOwner()) {
118 2
            $this->getOwner()->accountRemoved();
119
        }
120
121 12
        parent::setOwner($owner);
122
123 12
        if ($this->getOwner()) {
124 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

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