mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 13:07:27 +02:00
cs State
This commit is contained in:
49
State/CreateOrder.php
Normal file
49
State/CreateOrder.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CreateOrder
|
||||||
|
*/
|
||||||
|
class CreateOrder implements OrderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $order;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $order
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __construct(array $order)
|
||||||
|
{
|
||||||
|
if (empty($order)) {
|
||||||
|
throw new \Exception('Order can not be empty!');
|
||||||
|
}
|
||||||
|
$this->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!');
|
||||||
|
}
|
||||||
|
}
|
37
State/OrderController.php
Normal file
37
State/OrderController.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OrderController
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
*/
|
||||||
|
public function completeAction($id)
|
||||||
|
{
|
||||||
|
$order = OrderFactory::getOrder($id);
|
||||||
|
try {
|
||||||
|
$order->completeOrder($id);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
//handle error!
|
||||||
|
}
|
||||||
|
// response to browser
|
||||||
|
}
|
||||||
|
}
|
35
State/OrderFactory.php
Normal file
35
State/OrderFactory.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OrderFactory
|
||||||
|
*/
|
||||||
|
class OrderFactory
|
||||||
|
{
|
||||||
|
private function __construct()
|
||||||
|
{
|
||||||
|
throw Exception('Can not instance the OrderFactory class!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
*
|
||||||
|
* @return CreateOrder|ShippingOrder
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
State/OrderInterface.php
Normal file
19
State/OrderInterface.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OrderInterface
|
||||||
|
*/
|
||||||
|
interface OrderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function shipOrder();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function completeOrder();
|
||||||
|
}
|
49
State/ShippingOrder.php
Normal file
49
State/ShippingOrder.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ShippingOrder
|
||||||
|
*/
|
||||||
|
class ShippingOrder implements OrderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $order;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $order
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function __construct(array $order)
|
||||||
|
{
|
||||||
|
if (empty($order)) {
|
||||||
|
throw new \Exception('Order can not be empty!');
|
||||||
|
}
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
143
State/State.php
143
State/State.php
@@ -1,143 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Status;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The order has two status: created, shipping, completed.
|
|
||||||
* When the order's status is created, what can do is just 'shipping' handle;
|
|
||||||
* When the order's status is shipping, what can do is just 'completed' handle;
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
interface OrderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function shipOrder();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function completeOrder();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class CreateOrder
|
|
||||||
*/
|
|
||||||
class CreateOrder implements OrderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $order;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $order
|
|
||||||
*/
|
|
||||||
public function __construct(array $order)
|
|
||||||
{
|
|
||||||
if (empty($order)) {
|
|
||||||
throw new \Exception('Order can not be empty!');
|
|
||||||
}
|
|
||||||
$this->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
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user