mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
PHP7 State
This commit is contained in:
parent
1c30978a70
commit
ea8c91ac68
@ -2,46 +2,34 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class CreateOrder implements OrderInterface
|
||||
class CreateOrder implements Order
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $order;
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* @param array $order
|
||||
*
|
||||
* @throws \Exception
|
||||
* @param array $details
|
||||
*/
|
||||
public function __construct(array $order)
|
||||
public function __construct(array $details)
|
||||
{
|
||||
if (empty($order)) {
|
||||
throw new \Exception('Order can not be empty!');
|
||||
}
|
||||
$this->order = $order;
|
||||
$this->details = $details;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function shipOrder()
|
||||
{
|
||||
$this->order['status'] = 'shipping';
|
||||
$this->order['updatedTime'] = time();
|
||||
|
||||
// Setting the new order status into database;
|
||||
return $this->updateOrder($this->order);
|
||||
$this->details['status'] = 'shipping';
|
||||
$this->details['updatedTime'] = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
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!');
|
||||
throw new \Exception('Can not complete the order which status is created');
|
||||
}
|
||||
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->details['status'];
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
interface OrderInterface
|
||||
interface Order
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
@ -13,4 +13,6 @@ interface OrderInterface
|
||||
* @return mixed
|
||||
*/
|
||||
public function completeOrder();
|
||||
|
||||
public function getStatus(): string;
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class OrderController
|
||||
{
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function shipAction($id)
|
||||
{
|
||||
$order = OrderFactory::getOrder($id);
|
||||
try {
|
||||
$order->shipOrder();
|
||||
} catch (Exception $e) {
|
||||
//handle error!
|
||||
}
|
||||
// response to browser
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function completeAction($id)
|
||||
{
|
||||
$order = OrderFactory::getOrder($id);
|
||||
try {
|
||||
$order->completeOrder();
|
||||
} catch (Exception $e) {
|
||||
//handle error!
|
||||
}
|
||||
// response to browser
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class OrderFactory
|
||||
{
|
||||
private function __construct()
|
||||
{
|
||||
throw new \Exception('Can not instance the OrderFactory class!');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return CreateOrder|ShippingOrder
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
34
Behavioral/State/OrderRepository.php
Normal file
34
Behavioral/State/OrderRepository.php
Normal file
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
}
|
@ -20,21 +20,15 @@ Code
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
OrderController.php
|
||||
OrderRepository.php
|
||||
|
||||
.. literalinclude:: OrderController.php
|
||||
.. literalinclude:: OrderRepository.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
OrderFactory.php
|
||||
Order.php
|
||||
|
||||
.. literalinclude:: OrderFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
OrderInterface.php
|
||||
|
||||
.. literalinclude:: OrderInterface.php
|
||||
.. literalinclude:: Order.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
@ -2,46 +2,34 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class ShippingOrder implements OrderInterface
|
||||
class ShippingOrder implements Order
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $order;
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* @param array $order
|
||||
*
|
||||
* @throws \Exception
|
||||
* @param array $details
|
||||
*/
|
||||
public function __construct(array $order)
|
||||
public function __construct(array $details)
|
||||
{
|
||||
if (empty($order)) {
|
||||
throw new \Exception('Order can not be empty!');
|
||||
}
|
||||
$this->order = $order;
|
||||
$this->details = $details;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
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();
|
||||
$this->details['status'] = 'completed';
|
||||
$this->details['updatedTime'] = time();
|
||||
}
|
||||
|
||||
// Setting the new order status into database;
|
||||
return $this->updateOrder($this->order);
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->details['status'];
|
||||
}
|
||||
}
|
||||
|
33
Behavioral/State/Tests/StateTest.php
Normal file
33
Behavioral/State/Tests/StateTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State\Tests;
|
||||
|
||||
use DesignPatterns\Behavioral\State\OrderRepository;
|
||||
|
||||
class StateTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCanShipCreatedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(1);
|
||||
$order->shipOrder();
|
||||
|
||||
$this->assertEquals('shipping', $order->getStatus());
|
||||
}
|
||||
|
||||
public function testCanCompleteShippedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(2);
|
||||
$order->completeOrder();
|
||||
|
||||
$this->assertEquals('completed', $order->getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(1);
|
||||
$order->completeOrder();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user