diff --git a/State/CreateOrder.php b/State/CreateOrder.php new file mode 100644 index 0000000..eb494cc --- /dev/null +++ b/State/CreateOrder.php @@ -0,0 +1,49 @@ +order = $order; + } + + /** + * @return mixed + */ + public function shipOrder() + { + $this->order['status'] = 'shipping'; + $this->order['updatedTime'] = time(); + + // Setting the new order status into database; + return $this->updateOrder($order); + } + + /** + * @return mixed|void + * @throws \Exception + */ + public function completeOrder() + { + //Can not complete the order which status is created, throw exception; + throw new \Exception('Can not complete the order which status is created!'); + } +} diff --git a/State/OrderController.php b/State/OrderController.php new file mode 100644 index 0000000..a9690b4 --- /dev/null +++ b/State/OrderController.php @@ -0,0 +1,37 @@ +shipOrder($id); + } catch (Exception $e) { + //handle error! + } + // response to browser + } + + /** + * @param int $id + */ + public function completeAction($id) + { + $order = OrderFactory::getOrder($id); + try { + $order->completeOrder($id); + } catch (Exception $e) { + //handle error! + } + // response to browser + } +} diff --git a/State/OrderFactory.php b/State/OrderFactory.php new file mode 100644 index 0000000..55c7d80 --- /dev/null +++ b/State/OrderFactory.php @@ -0,0 +1,35 @@ +order = $order; + } + + /** + * @return mixed|void + * @throws \Exception + */ + public function shipOrder() + { + //Can not ship the order which status is shipping, throw exception; + throw new \Exception('Can not ship the order which status is shipping!'); + } + + /** + * @return mixed + */ + public function completeOrder() + { + $this->order['status'] = 'completed'; + $this->order['updatedTime'] = time(); + + // Setting the new order status into database; + return $this->updateOrder($order); + } +} diff --git a/State/State.php b/State/State.php deleted file mode 100644 index 6182812..0000000 --- a/State/State.php +++ /dev/null @@ -1,143 +0,0 @@ -order = $order; - } - - public function shipOrder() - { - $this->order['status'] = 'shipping'; - $this->order['updatedTime'] = time(); - - // Setting the new order status into database; - return $this->updateOrder($order); - } - - public function completeOrder() - { - //Can not complete the order which status is created, throw exception; - throw new \Exception('Can not complete the order which status is created!'); - } -} - -class ShippingOrder implements OrderInterface -{ - - private $order; - - public function __construct(array $order) - { - if (empty($order)) { - throw new \Exception('Order can not be empty!'); - } - $this->order = $order; - } - - public function shipOrder() - { - //Can not ship the order which status is shipping, throw exception; - throw new \Exception('Can not ship the order which status is shipping!'); - } - - public function completeOrder() - { - $this->order['status'] = 'completed'; - $this->order['updatedTime'] = time(); - - // Setting the new order status into database; - return $this->updateOrder($order); - } -} - -class OrderFactory -{ - - private function __construct() - { - throw Exception('Can not instance the OrderFactory class!'); - } - - public static function getOrder($id) - { - $order = 'Get Order From Database'; - - switch ($order['status']) { - case 'created': - return new CreateOrder($order); - case 'shipping': - return new ShippingOrder($order); - default: - throw new \Exception('Order status error!'); - break; - } - } -} - - -// Client - -class OrderController -{ - /** - * @param int $id - */ - public function shipAction($id) - { - $order = OrderFactory::getOrder($id); - try { - $order->shipOrder($id); - } catch (Exception $e) { - //handle error! - } - // response to browser - } - - public function completeAction($id) - { - $order = OrderFactory::getOrder($id); - try { - $order->completeOrder($id); - } catch (Exception $e) { - //handle error! - } - // response to browser - } -}