mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-13 11:26:22 +02:00
PHP7 State
This commit is contained in:
@ -2,46 +2,34 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class CreateOrder implements OrderInterface
|
class CreateOrder implements Order
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $order;
|
private $details;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $order
|
* @param array $details
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function __construct(array $order)
|
public function __construct(array $details)
|
||||||
{
|
{
|
||||||
if (empty($order)) {
|
$this->details = $details;
|
||||||
throw new \Exception('Order can not be empty!');
|
|
||||||
}
|
|
||||||
$this->order = $order;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function shipOrder()
|
public function shipOrder()
|
||||||
{
|
{
|
||||||
$this->order['status'] = 'shipping';
|
$this->details['status'] = 'shipping';
|
||||||
$this->order['updatedTime'] = time();
|
$this->details['updatedTime'] = time();
|
||||||
|
|
||||||
// Setting the new order status into database;
|
|
||||||
return $this->updateOrder($this->order);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \Exception
|
|
||||||
*
|
|
||||||
* @return mixed|void
|
|
||||||
*/
|
|
||||||
public function completeOrder()
|
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;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
interface OrderInterface
|
interface Order
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@ -13,4 +13,6 @@ interface OrderInterface
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function completeOrder();
|
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`_
|
You can also find these code on `GitHub`_
|
||||||
|
|
||||||
OrderController.php
|
OrderRepository.php
|
||||||
|
|
||||||
.. literalinclude:: OrderController.php
|
.. literalinclude:: OrderRepository.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
OrderFactory.php
|
Order.php
|
||||||
|
|
||||||
.. literalinclude:: OrderFactory.php
|
.. literalinclude:: Order.php
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
OrderInterface.php
|
|
||||||
|
|
||||||
.. literalinclude:: OrderInterface.php
|
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
@ -2,46 +2,34 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class ShippingOrder implements OrderInterface
|
class ShippingOrder implements Order
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $order;
|
private $details;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $order
|
* @param array $details
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function __construct(array $order)
|
public function __construct(array $details)
|
||||||
{
|
{
|
||||||
if (empty($order)) {
|
$this->details = $details;
|
||||||
throw new \Exception('Order can not be empty!');
|
|
||||||
}
|
|
||||||
$this->order = $order;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \Exception
|
|
||||||
*
|
|
||||||
* @return mixed|void
|
|
||||||
*/
|
|
||||||
public function shipOrder()
|
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!');
|
throw new \Exception('Can not ship the order which status is shipping!');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function completeOrder()
|
public function completeOrder()
|
||||||
{
|
{
|
||||||
$this->order['status'] = 'completed';
|
$this->details['status'] = 'completed';
|
||||||
$this->order['updatedTime'] = time();
|
$this->details['updatedTime'] = time();
|
||||||
|
}
|
||||||
|
|
||||||
// Setting the new order status into database;
|
public function getStatus(): string
|
||||||
return $this->updateOrder($this->order);
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user