-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.php
More file actions
197 lines (167 loc) · 5.97 KB
/
playground.php
File metadata and controls
197 lines (167 loc) · 5.97 KB
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
<?php
declare(strict_types=1);
use Vaened\DeltaOrchestrator\Action;
use Vaened\DeltaOrchestrator\Comparison\DateTimeComparator;
use Vaened\DeltaOrchestrator\Field;
use Vaened\DeltaOrchestrator\Orchestrator;
use Vaened\DeltaOrchestrator\Patch\PatchInput;
use function Vaened\DeltaOrchestrator\Rules\all;
use function Vaened\DeltaOrchestrator\Rules\any;
use function Vaened\DeltaOrchestrator\Rules\present;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/Support/UserProfile.php';
require __DIR__ . '/Support/ScenarioReporter.php';
require __DIR__ . '/Support/UserProfileApplicationService.php';
$currentUser = new UserProfile(
displayName : 'Pedro',
email : 'pedro@old.test',
birthDate : new DateTimeImmutable('1990-10-20'),
marketingConsent: false,
timezone : 'America/Lima',
);
$payload = new PatchInput(
input : [
// Present but equal: activates presence-based actions, but produces no delta.
'displayName' => 'Pedro',
// Present and different: produces an effective delta.
'email' => 'pedro@new.test',
// Present with semantic DateTime comparison.
'birthDate' => '1990-10-20 00:00:00',
// Present with null: useful to demonstrate a contract failure in a controlled action.
'marketingConsent' => null,
],
expectedKeys: [
'displayName',
'email',
'birthDate',
'marketingConsent',
],
);
$displayName = Field::from(
patch : $payload->string('displayName'),
current: $currentUser->displayName,
);
$email = Field::from(
patch : $payload->string('email'),
current: $currentUser->email,
);
$birthDate = Field::from(
patch : $payload->dateTimeImmutable('birthDate'),
current: $currentUser->birthDate,
comparator: DateTimeComparator::create(),
);
$marketingConsent = Field::from(
patch : $payload->bool('marketingConsent'),
current: $currentUser->marketingConsent,
);
$timezone = Field::from(
patch : $payload->string('timezone'),
current: $currentUser->timezone,
);
$reporter = new ScenarioReporter();
$service = new UserProfileApplicationService($reporter);
$orchestrator = new Orchestrator();
$orchestrator->register(new Action(
fields : [$displayName->required()],
apply : function (Field $displayName) use ($service): void {
$service->rename($displayName->value());
},
description: 'Skipped because displayName is present but unchanged',
));
$orchestrator->register(new Action(
fields : [$email->required()],
apply : function (Field $email) use ($service): void {
$service->changeEmail($email->value());
},
description: 'Runs because email changed',
));
$orchestrator->register(new Action(
fields : [
$displayName->optional(),
$birthDate->optional(),
],
apply : function (Field $displayName, Field $birthDate) use ($service): void {
$nextDisplayName = $displayName->isPresent()
? $displayName->value()
: $displayName->current();
$nextBirthDate = $birthDate->isPresent()
? $birthDate->value()
: $birthDate->current();
$service->rebuildPersonalData(
displayName: $nextDisplayName,
birthDate : $nextBirthDate,
);
},
when : static fn(Field $displayName, Field $birthDate) => any([
present($displayName),
present($birthDate),
]),
description: 'Runs only if one of the personal-data fields is present and changed',
));
$orchestrator->register(new Action(
fields : [
$displayName->optional(),
$email->required(),
],
apply : function (Field $displayName, Field $email) use ($service): void {
$nextDisplayName = $displayName->isPresent()
? $displayName->value()
: $displayName->current();
$service->synchronizeIdentityProvider(
displayName: $nextDisplayName,
email : $email->value(),
);
},
when : static fn(Field $displayName, Field $email) => any([
present($displayName),
present($email),
]),
description: 'Runs because email changed and displayName can fallback to current value',
));
$orchestrator->register(new Action(
fields : [
$email->required(),
$birthDate->optional(),
$displayName->optional(),
],
apply : function (Field $email, Field $birthDate, Field $displayName) use ($service): void {
$nextDisplayName = $displayName->isPresent()
? $displayName->value()
: $displayName->current();
$nextBirthDate = $birthDate->isPresent()
? $birthDate->value()
: $birthDate->current();
$service->rebuildSearchIndex(
displayName: $nextDisplayName,
email : $email->value(),
birthDate : $nextBirthDate,
);
},
when : static fn(Field $email, Field $birthDate, Field $displayName) => any([
present($email),
present($birthDate),
present($displayName),
]),
description: 'Runs because one searchable field changed',
));
$orchestrator->register(new Action(
fields : [$timezone->required()],
apply : function (Field $timezone) use ($service): void {
$service->updateTimezone($timezone->value());
},
description: 'Skipped because timezone is absent',
));
$orchestrator->register(new Action(
fields : [$marketingConsent->required()],
apply : function (Field $marketingConsent) use ($service): void {
$service->updateMarketingConsent($marketingConsent->value());
},
when : static fn(Field $marketingConsent) => all([$marketingConsent]),
description: 'Fails because marketingConsent is present but null',
));
try {
$orchestrator->execute();
} catch (Throwable $exception) {
$reporter->failure('ContractFailure: ' . $exception::class);
}
$reporter->dump();