1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace App\Entity;
use App\Entity\EntityTrait\EnablableEntityTrait;
use App\Flux\AccountInterface;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Ramsey\Uuid\Doctrine\UuidGenerator;
/**
* @ORM\MappedSuperclass
* @ORM\HasLifecycleCallbacks
*/
abstract class Account implements AccountInterface
{
use EnablableEntityTrait;
use TimeStampableEntity;
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
protected $id;
/**
* @ORM\Column(name="balance", type="decimal", scale=2, options={"default": 0.00})
*/
protected $balance = 0.00;
/**
* @var string
*
* @ORM\Column(name="currency", type="string", length=25, options={"default" : ""})
*/
protected $currency = '';
/**
* Hash => permet de vérifier l'intégrité des données.
*
* @var text
*
* @ORM\Column(name="hash", type="text", options={"default" : "tmp"})
*/
protected $hash = 'tmp';
protected $operations;
public function getId()
{
return $this->id;
}
public function getBalance(): ?float
{
return $this->balance;
}
public function setBalance(float $balance): self
{
$this->balance = $balance;
return $this;
}
/**
* Can be negative amount.
*
* @param float $montant [description]
*/
public function addAmount(float $montant): self
{
$this->balance += $montant;
return $this;
}
public function getCurrency(): string
{
return $this->currency;
}
/**
* Set currency.
*
* @return $this
*/
public function setCurrency(string $currency): self
{
$this->currency = $currency;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(string $hash): self
{
$this->hash = $hash;
return $this;
}
/**
* @ORM\PreUpdate
*
* @param LifecycleEventArgs $event
*/
public function preUpdate(LifecycleEventArgs $event): void
{
$this->updateHash($event);
}
/**
* @ORM\PostPersist
*
* @param LifecycleEventArgs $event
*/
public function postPersist(LifecycleEventArgs $event): void
{
$this->updateHash($event);
$account = $event->getEntity();
$event->getEntityManager()->persist($account);
$event->getEntityManager()->flush();
}
private function updateHash(LifecycleEventArgs $event): void
{
$account = $event->getEntity();
if ($account->getBalance() < 0) {
throw new \Exception('[ACCOUNT] Opération impossible ! Balance inférieure ou égal à zéro !');
}
// @TODO : generation du hash du account gourmand en ressource => voir pour optimiser ou le faire en async !
$hash = password_hash($account->getAllInfosUncrypted(), PASSWORD_BCRYPT, ['cost' => 5]);
$account->setHash($hash);
}
public function getAllInfosUncrypted()
{
return $_ENV['APP_SECRET'] . $this->getId() . (0 == $this->getBalance() ? 0 : $this->getBalance()) . $this->getCurrency();
}
/**
* Get operations.
*
* @return Operation[]|ArrayCollection
*/
public function getOperations()
{
return $this->operations;
}
/**
* Set operations.
*
* @return $this
*/
public function setOperations($operations)
{
$this->operations = $operations;
return $this;
}
/**
* @param Operation $operation
*
* @return $this
*/
public function addOperation(Operation $operation)
{
if (!$this->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->setAccount($this);
}
return $this;
}
/**
* @param Operation $operation
*
* @return $this
*/
public function removeOperation(Operation $operation)
{
if ($this->operations->contains($operation)) {
$this->operations->removeElement($operation);
$operation->setAccount(null);
}
return $this;
}
}