mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
Merge pull request #285 from ko22009/master
State pattern is maintainable
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,15 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class CreateOrder implements Order
|
class CreateOrder extends StateOrder
|
||||||
{
|
{
|
||||||
/**
|
public function __construct()
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $details;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $details
|
|
||||||
*/
|
|
||||||
public function __construct(array $details)
|
|
||||||
{
|
{
|
||||||
$this->details = $details;
|
$this->setStatus('created');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shipOrder()
|
protected function done()
|
||||||
{
|
{
|
||||||
$this->details['status'] = 'shipping';
|
static::$state = new ShippingOrder();
|
||||||
$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'];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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`_
|
You can also find this code on `GitHub`_
|
||||||
|
|
||||||
OrderRepository.php
|
ContextOrder.php
|
||||||
|
|
||||||
.. literalinclude:: OrderRepository.php
|
.. literalinclude:: ContextOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
Order.php
|
StateOrder.php
|
||||||
|
|
||||||
.. literalinclude:: Order.php
|
.. literalinclude:: StateOrder.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
@@ -2,34 +2,15 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\State;
|
namespace DesignPatterns\Behavioral\State;
|
||||||
|
|
||||||
class ShippingOrder implements Order
|
class ShippingOrder extends StateOrder
|
||||||
{
|
{
|
||||||
/**
|
public function __construct()
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $details;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $details
|
|
||||||
*/
|
|
||||||
public function __construct(array $details)
|
|
||||||
{
|
{
|
||||||
$this->details = $details;
|
$this->setStatus('shipping');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function shipOrder()
|
protected function done()
|
||||||
{
|
{
|
||||||
throw new \Exception('Can not ship the order which status is shipping!');
|
$this->setStatus('completed');
|
||||||
}
|
|
||||||
|
|
||||||
public function completeOrder()
|
|
||||||
{
|
|
||||||
$this->details['status'] = 'completed';
|
|
||||||
$this->details['updatedTime'] = time();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getStatus(): string
|
|
||||||
{
|
|
||||||
return $this->details['status'];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
namespace DesignPatterns\Behavioral\State\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Behavioral\State\OrderRepository;
|
use DesignPatterns\Behavioral\State\ContextOrder;
|
||||||
|
use DesignPatterns\Behavioral\State\CreateOrder;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class StateTest extends TestCase
|
class StateTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCanShipCreatedOrder()
|
public function testCanShipCreatedOrder()
|
||||||
{
|
{
|
||||||
$order = (new OrderRepository())->findById(1);
|
$order = new CreateOrder();
|
||||||
$order->shipOrder();
|
$contextOrder = new ContextOrder();
|
||||||
|
$contextOrder->setState($order);
|
||||||
|
$contextOrder->done();
|
||||||
|
|
||||||
$this->assertEquals('shipping', $order->getStatus());
|
$this->assertEquals('shipping', $contextOrder->getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCanCompleteShippedOrder()
|
public function testCanCompleteShippedOrder()
|
||||||
{
|
{
|
||||||
$order = (new OrderRepository())->findById(2);
|
$order = new CreateOrder();
|
||||||
$order->completeOrder();
|
$contextOrder = new ContextOrder();
|
||||||
|
$contextOrder->setState($order);
|
||||||
|
$contextOrder->done();
|
||||||
|
$contextOrder->done();
|
||||||
|
|
||||||
$this->assertEquals('completed', $order->getStatus());
|
$this->assertEquals('completed', $contextOrder->getStatus());
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException \Exception
|
|
||||||
*/
|
|
||||||
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder()
|
|
||||||
{
|
|
||||||
$order = (new OrderRepository())->findById(1);
|
|
||||||
$order->completeOrder();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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