mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
State pattern is maintainable
state pattern change readme.rst
This commit is contained in:
26
Behavioral/State/ContextOrder.php
Normal file
26
Behavioral/State/ContextOrder.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class ContextOrder extends StateOrder
|
||||
{
|
||||
public function getState():StateOrder
|
||||
{
|
||||
return static::$state;
|
||||
}
|
||||
|
||||
public function setState(StateOrder $state)
|
||||
{
|
||||
static::$state = $state;
|
||||
}
|
||||
|
||||
public function done()
|
||||
{
|
||||
static::$state->done();
|
||||
}
|
||||
|
||||
public function getStatus(): string
|
||||
{
|
||||
return static::$state->getStatus();
|
||||
}
|
||||
}
|
@@ -2,34 +2,17 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class CreateOrder implements Order
|
||||
class CreateOrder extends StateOrder
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* @param array $details
|
||||
*/
|
||||
public function __construct(array $details)
|
||||
public function __construct()
|
||||
{
|
||||
$this->details = $details;
|
||||
$this->setStatus('created');
|
||||
}
|
||||
|
||||
public function shipOrder()
|
||||
protected function done()
|
||||
{
|
||||
$this->details['status'] = 'shipping';
|
||||
$this->details['updatedTime'] = time();
|
||||
}
|
||||
|
||||
public function completeOrder()
|
||||
{
|
||||
throw new \Exception('Can not complete the order which status is created');
|
||||
}
|
||||
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->details['status'];
|
||||
$order = new ShippingOrder();
|
||||
static::$state = $order;
|
||||
//$this->setStatus('shipping');
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
interface Order
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function shipOrder();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function completeOrder();
|
||||
|
||||
public function getStatus(): string;
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
<?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,15 +20,15 @@ Code
|
||||
|
||||
You can also find this code on `GitHub`_
|
||||
|
||||
OrderRepository.php
|
||||
ContextOrder.php
|
||||
|
||||
.. literalinclude:: OrderRepository.php
|
||||
.. literalinclude:: ContextOrder.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Order.php
|
||||
StateOrder.php
|
||||
|
||||
.. literalinclude:: Order.php
|
||||
.. literalinclude:: StateOrder.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
@@ -2,34 +2,15 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
class ShippingOrder implements Order
|
||||
class ShippingOrder extends StateOrder
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* @param array $details
|
||||
*/
|
||||
public function __construct(array $details)
|
||||
public function __construct()
|
||||
{
|
||||
$this->details = $details;
|
||||
$this->setStatus('shipping');
|
||||
}
|
||||
|
||||
public function shipOrder()
|
||||
protected function done()
|
||||
{
|
||||
throw new \Exception('Can not ship the order which status is shipping!');
|
||||
}
|
||||
|
||||
public function completeOrder()
|
||||
{
|
||||
$this->details['status'] = 'completed';
|
||||
$this->details['updatedTime'] = time();
|
||||
}
|
||||
|
||||
public function getStatus(): string
|
||||
{
|
||||
return $this->details['status'];
|
||||
$this->setStatus('completed');
|
||||
}
|
||||
}
|
||||
|
32
Behavioral/State/StateOrder.php
Normal file
32
Behavioral/State/StateOrder.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Behavioral\State;
|
||||
|
||||
abstract class StateOrder
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* @var StateOrder $state
|
||||
*/
|
||||
protected static $state;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function done();
|
||||
|
||||
protected function setStatus(string $status)
|
||||
{
|
||||
$this->details['status'] = $status;
|
||||
$this->details['updatedTime'] = time();
|
||||
}
|
||||
|
||||
protected function getStatus(): string
|
||||
{
|
||||
return $this->details['status'];
|
||||
}
|
||||
}
|
@@ -2,33 +2,30 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\State\Tests;
|
||||
|
||||
use DesignPatterns\Behavioral\State\OrderRepository;
|
||||
use DesignPatterns\Behavioral\State\ContextOrder;
|
||||
use DesignPatterns\Behavioral\State\CreateOrder;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StateTest extends TestCase
|
||||
{
|
||||
public function testCanShipCreatedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(1);
|
||||
$order->shipOrder();
|
||||
$order = new CreateOrder();
|
||||
$contextOrder = new ContextOrder();
|
||||
$contextOrder->setState($order);
|
||||
$contextOrder->done();
|
||||
|
||||
$this->assertEquals('shipping', $order->getStatus());
|
||||
$this->assertEquals('shipping', $contextOrder->getStatus());
|
||||
}
|
||||
|
||||
public function testCanCompleteShippedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(2);
|
||||
$order->completeOrder();
|
||||
$order = new CreateOrder();
|
||||
$contextOrder = new ContextOrder();
|
||||
$contextOrder->setState($order);
|
||||
$contextOrder->done();
|
||||
$contextOrder->done();
|
||||
|
||||
$this->assertEquals('completed', $order->getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder()
|
||||
{
|
||||
$order = (new OrderRepository())->findById(1);
|
||||
$order->completeOrder();
|
||||
$this->assertEquals('completed', $contextOrder->getStatus());
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 22 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 124 KiB |
Reference in New Issue
Block a user