forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderRepository.php
34 lines (29 loc) · 848 Bytes
/
OrderRepository.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
<?php
namespace DesignPatterns\Behavioral\State;
class OrderRepository
{
/**
* @var array
*/
private static $orders = [
1 => ['status' => 'created'],
2 => ['status' => 'shipping'],
3 => ['status' => 'completed'],
];
public static function findById(int $id): Order
{
if (!isset(self::$orders[$id])) {
throw new \InvalidArgumentException(sprintf('Order with id %d does not exist', $id));
}
$order = self::$orders[$id];
switch ($order['status']) {
case 'created':
return new CreateOrder($order);
case 'shipping':
return new ShippingOrder($order);
default:
throw new \InvalidArgumentException('Invalid order status given');
break;
}
}
}