|
1
|
|
|
<?php namespace RayEmitter\Example\BankAccount; |
|
2
|
|
|
|
|
3
|
|
|
use C4tech\RayEmitter\Domain\Aggregate as AbstractAggregate; |
|
4
|
|
|
use Ramsey\Uuid\Uuid; |
|
5
|
|
|
|
|
6
|
|
|
final class Aggregate extends AbstractAggregate |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Minimum needed to open an account. |
|
10
|
|
|
* @var int|float |
|
11
|
|
|
*/ |
|
12
|
|
|
const MINIMUM_TO_OPEN = 50; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Account Was Created Event Handle |
|
16
|
|
|
* |
|
17
|
|
|
* @param AccountWasCreated $event Event Object |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function applyAccountWasCreated(AccountWasCreated $event) |
|
21
|
|
|
{ |
|
22
|
|
|
$data = $event->getPayload(); |
|
23
|
|
|
$identifier = new AccountId($event->getId()); |
|
24
|
|
|
$this->root = new AggregateRoot($identifier); |
|
|
|
|
|
|
25
|
|
|
$this->root->owner = $data->owner; |
|
|
|
|
|
|
26
|
|
|
$this->root->balance = $data->deposit; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Money Deposited Event Handle |
|
31
|
|
|
* |
|
32
|
|
|
* @param MoneyDeposited $event Event Object |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
protected function applyMoneyDeposited(MoneyDeposited $event) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$data = $event->getPayload(); |
|
38
|
|
|
$balance = $this->root->balance->getValue(); |
|
39
|
|
|
$balance += $data->deposit->getValue(); |
|
40
|
|
|
$this->root->balance = new UsDollar($balance); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Money Withdrawn Event Handle |
|
45
|
|
|
* |
|
46
|
|
|
* @param MoneyWithdrawn $event Event Object |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
protected function applyMoneyWithdrawn(MoneyWithdrawn $event) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$data = $event->getPayload(); |
|
52
|
|
|
$balance = $this->root->balance->getValue(); |
|
53
|
|
|
$balance -= $data->withdrawal->getValue(); |
|
54
|
|
|
$this->root->balance = new UsDollar($balance); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create Account Command Handle |
|
59
|
|
|
* |
|
60
|
|
|
* Processes business logic for the account creation command. |
|
61
|
|
|
* @param CreateAccount $command Command object. |
|
62
|
|
|
* @return AccountWasCreated Event object. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function handleCreateAccount(CreateAccount $command) |
|
65
|
|
|
{ |
|
66
|
|
|
$data = $command->getPayload(); |
|
67
|
|
|
|
|
68
|
|
|
$account_id = new AccountId(Uuid::uuid4()->toString()); |
|
69
|
|
|
$owner = new OwnerName($data->owner); |
|
70
|
|
|
$initial_deposit = new UsDollar($data->deposit); |
|
71
|
|
|
|
|
72
|
|
|
if ($initial_deposit->getValue() < self::MINIMUM_TO_OPEN) { |
|
73
|
|
|
throw new MinimumDepositException($initial_deposit->getValue(), self::MINIMUM_TO_OPEN); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new AccountWasCreated($account_id, compact('owner', 'initial_deposit')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Deposit Money Command Handle |
|
81
|
|
|
* |
|
82
|
|
|
* Processes business logic for the deposit money command. |
|
83
|
|
|
* @param DepositMoney $command Command object. |
|
84
|
|
|
* @return MoneyDeposited Event object. |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function handleDepositMoney(DepositMoney $command) |
|
87
|
|
|
{ |
|
88
|
|
|
$data = $command->getPayload(); |
|
89
|
|
|
|
|
90
|
|
|
$account_id = new AccountId($data->account_id); |
|
91
|
|
|
$deposit = new UsDollar($data->deposit); |
|
92
|
|
|
|
|
93
|
|
|
if ($deposit->getValue() < 0) { |
|
94
|
|
|
throw new MinimumDepositException($deposit->getValue()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return new MoneyDeposited($account_id, compact('deposit')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Withdraw Money Command Handle |
|
102
|
|
|
* |
|
103
|
|
|
* Processes business logic for the withdraw money command. |
|
104
|
|
|
* @param WithdrawMoney $command Command object. |
|
105
|
|
|
* @return MoneyWithdrawn Event object. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function handleWithdrawMoney(WithdrawMoney $command) |
|
108
|
|
|
{ |
|
109
|
|
|
$data = $command->getPayload(); |
|
110
|
|
|
|
|
111
|
|
|
$account_id = new AccountId($data->account_id); |
|
112
|
|
|
$withdrawal = new UsDollar($data->withdrawal); |
|
113
|
|
|
|
|
114
|
|
|
if ($withdrawal->getValue() < 0) { |
|
115
|
|
|
throw new MinimumWithdrawalException($withdrawal->getValue()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($withdrawal->getValue() > $this->root->balance->getValue()) { |
|
119
|
|
|
throw new InsufficientFundsException($withdrawal->getValue(), $this->balance->getValue()); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return new MoneyWithdrawn($account_id, compact('withdrawal')); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..