Completed
Push — master ( 3853ec...f78c46 )
by Adrien
07:49
created

Account::userAdded()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
use Money\Money;
15
16
/**
17
 * Financial account
18
 *
19
 * @ORM\Entity(repositoryClass="Application\Repository\AccountRepository")
20
 * @ORM\AssociationOverrides({
21
 *     @ORM\AssociationOverride(
22
 *         name="owner",
23
 *         inversedBy="accounts",
24
 *         joinColumns=@ORM\JoinColumn(unique=true, onDelete="SET NULL")
25
 *     )
26
 * })
27
 */
28
class Account extends AbstractModel
29
{
30
    use HasName;
31
    use HasIban;
32
33
    /**
34
     * @var Money
35
     *
36
     * @ORM\Column(type="Money", options={"default" = 0})
37
     */
38
    private $balance;
39
40
    /**
41
     * @var Account
42
     * @ORM\ManyToOne(targetEntity="Account", inversedBy="children")
43
     * @ORM\JoinColumns({
44
     *     @ORM\JoinColumn(onDelete="CASCADE")
45
     * })
46
     */
47
    private $parent;
48
49
    /**
50
     * @var Collection
51
     * @ORM\OneToMany(targetEntity="Account", mappedBy="parent")
52
     * @ORM\OrderBy({"code" = "ASC"})
53
     */
54
    private $children;
55
56
    /**
57
     * @var string
58
     *
59
     * @ORM\Column(type="AccountType", length=10)
60
     */
61
    private $type;
62
63
    /**
64
     * @var string
65
     *
66
     * @ORM\Column(type="string", length=10, nullable=false, unique=true)
67
     */
68
    private $code;
69
70
    /**
71
     * @var Collection
72
     * @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="debit")
73
     */
74
    private $debitTransactionLines;
75
76
    /**
77
     * @var Collection
78
     * @ORM\OneToMany(targetEntity="TransactionLine", mappedBy="credit")
79
     */
80
    private $creditTransactionLines;
81
82
    /**
83
     * Constructor
84
     */
85 20
    public function __construct()
86
    {
87 20
        $this->balance = Money::CHF(0);
88 20
        $this->children = new ArrayCollection();
89 20
        $this->debitTransactionLines = new ArrayCollection();
90 20
        $this->creditTransactionLines = new ArrayCollection();
91 20
    }
92
93
    /**
94
     * Assign the account to an user
95
     *
96
     * @param null|User $owner
97
     */
98 12
    public function setOwner(User $owner = null): void
99
    {
100 12
        if ($this->getOwner()) {
101 2
            $this->getOwner()->accountRemoved();
102
        }
103
104 12
        parent::setOwner($owner);
105
106 12
        if ($this->getOwner()) {
107 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

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