-
Notifications
You must be signed in to change notification settings - Fork 4
/
behavioral-chain-of-responsibility.php
316 lines (281 loc) · 7.25 KB
/
behavioral-chain-of-responsibility.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* This example just to simulate how Chain of Responsibility can be work
* 2nees.com
*/
/**
* Interface OrderHandler - This is Handler
* 2nees.com
*/
interface OrderHandler {
public function setNextHandler(OrderHandler $orderHandler): OrderHandler;
public function handle(Order $order);
}
/**
* Class OrderBaseHandler - This is Base handler - Optional, but its useful to share common action for concrete handlers
*/
abstract class OrderBaseHandler implements OrderHandler {
private ?OrderHandler $nextHandler = null;
public function setNextHandler(OrderHandler $orderHandler): OrderHandler
{
$this->nextHandler = $orderHandler;
return $this->nextHandler;
}
public function handle(Order $order)
{
if($this->nextHandler){
return $this->nextHandler->handle($order);
}
return null;
}
}
#region Concrete Handlers Class contain actual code for processes request
class CheckIsLoggedInUser extends OrderBaseHandler {
/**
* @throws Exception
*/
public function handle(Order $order)
{
if($order->getUser()->getIsLoggedIn()){
parent::handle($order);
}else {
throw new Exception("User not Logged In");
}
}
}
class CheckUserRole extends OrderBaseHandler {
/**
* @throws Exception
*/
public function handle(Order $order)
{
if($order->getUser()->getRole() === 1){
parent::handle($order);
}else {
throw new Exception("{$order->getUser()->getName()} Cant Access This Order!");
}
}
}
class CheckProductQuantity extends OrderBaseHandler {
/**
* @throws Exception
*/
public function handle(Order $order)
{
$notAvailable = false;
foreach ($order->getProducts() as $product){
if($product->getQuantity() === 0){
$notAvailable = $product;
}
}
if($notAvailable === false){
parent::handle($order);
}else {
throw new Exception("{$notAvailable->getName()} not available since its contain ({$notAvailable->getQuantity()})Item");
}
}
}
class AddLog extends OrderBaseHandler {
/**
* @throws Exception
*/
public function handle(Order $order)
{
echo "Add Order Log for Order ID: #{$order->getOrderId()}" . PHP_EOL;
parent::handle($order);
}
}
class OrderSummery extends OrderBaseHandler {
/**
* @throws Exception
*/
public function handle(Order $order)
{
$products = "";
foreach ($order->getProducts() as $product){
if($product->getQuantity() === 0){
$products .= "#{$product->getId()} - {$product->getName()} - {$product->getQuantity()}" . PHP_EOL;
}
}
echo "Order Owner: #{$order->getUser()->getId()} - {$order->getUser()->getName()}" . PHP_EOL;
echo "Order Products: {$products}" . PHP_EOL;
echo "Order Id: #{$order->getOrderId()}".PHP_EOL;
parent::handle($order);
}
}
#endregion
#region Services Class - Some of classes is used in our system for make any order
class Order {
private Users $user;
/**
* @var Products[]
*/
private array $products;
private int $orderId;
/**
* Order constructor.
* @param Users $user
* @param Products[] $products
* @param int $orderId
*/
public function __construct(Users $user, array $products, int $orderId)
{
$this->user = $user;
$this->products = $products;
$this->orderId = $orderId;
}
/**
* @return Users
*/
public function getUser(): Users
{
return $this->user;
}
/**
* @return Products[]
*/
public function getProducts(): array
{
return $this->products;
}
/**
* @return int
*/
public function getOrderId(): int
{
return $this->orderId;
}
}
class Users {
private bool $isLoggedIn;
private string $name;
private int $id;
private int $role;
/**
* Users constructor.
* @param string $name
* @param int $id
* @param int $role
* @param bool $isLoggedIn
*/
public function __construct(string $name, int $id, int $role, bool $isLoggedIn)
{
$this->name = $name;
$this->id = $id;
$this->role = $role;
$this->isLoggedIn = $isLoggedIn;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return int
*/
public function getRole(): int
{
return $this->role;
}
/**
* @return bool
*/
public function getIsLoggedIn(): bool
{
return $this->isLoggedIn;
}
}
class Products {
private string $name;
private int $id;
private int $quantity;
/**
* Users constructor.
* @param string $name
* @param int $id
*/
public function __construct(string $name, int $id, int $quantity)
{
$this->name = $name;
$this->id = $id;
$this->quantity = $quantity;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return int
*/
public function getQuantity(): int
{
return $this->quantity;
}
}
#endregion
// Init Data For Simulate User Request order
$user1 = new Users("Anees", 1, 1, true);
$user2 = new Users("Taher", 2, 1, true);
$user3 = new Users("Saed", 3, 2, true);
$user4 = new Users("Ibraheem", 4, 1, false);
$prod1 = new Products("Car", 1, 40);
$prod2 = new Products("Computer", 2, 10);
$prod3 = new Products("Gold Ring", 3, 0);
$order1 = new Order($user1, [$prod1, $prod2], 1);
$order2 = new Order($user2, [$prod3], 2);
$order3 = new Order($user3, [$prod1], 3);
$order4 = new Order($user4, [$prod1, $prod2], 4);
// Here is the important part for client, we create instance from all concrete handlers needed to execute base on our business logic
$checkIsLoggedInUser = new CheckIsLoggedInUser();
$checkUserRole = new CheckUserRole();
$checkProductQuantity = new CheckProductQuantity();
$addLog = new AddLog();
$orderSummery = new OrderSummery();
// Set order of handler execute
$checkIsLoggedInUser
->setNextHandler($checkUserRole)
->setNextHandler($checkProductQuantity)
->setNextHandler($addLog)
->setNextHandler($orderSummery);
try {
$checkIsLoggedInUser->handle($order1);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
echo "==================================================" . PHP_EOL;
try {
$checkIsLoggedInUser->handle($order2);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
echo "==================================================" . PHP_EOL;
try {
$checkIsLoggedInUser->handle($order3);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
echo "==================================================" . PHP_EOL;
try {
$checkIsLoggedInUser->handle($order4);
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}