Failed Conditions
Push — master ( a0775a...0669e5 )
by Adrien
09:15
created

Account::setOwner()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
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\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
     * Assign the account to an user
102
     *
103
     * @param null|User $owner
104
     */
105 12
    public function setOwner(?User $owner): void
106
    {
107 12
        if ($this->getOwner()) {
108 2
            $this->getOwner()->accountRemoved();
109
        }
110
111 12
        parent::setOwner($owner);
112
113 12
        if ($this->getOwner()) {
114 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

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