Completed
Push — master ( 3ac846...67b859 )
by Adrien
07:43
created

Account::getTransactionLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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