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