Skip to content

Commit c1f0fac

Browse files
author
Dominik Liebler
authored
Merge pull request DesignPatternsPHP#285 from ko22009/master
State pattern is maintainable
2 parents 138bdb2 + 590b15b commit c1f0fac

10 files changed

+567
-397
lines changed

Behavioral/State/ContextOrder.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
class ContextOrder extends StateOrder
6+
{
7+
public function getState():StateOrder
8+
{
9+
return static::$state;
10+
}
11+
12+
public function setState(StateOrder $state)
13+
{
14+
static::$state = $state;
15+
}
16+
17+
public function done()
18+
{
19+
static::$state->done();
20+
}
21+
22+
public function getStatus(): string
23+
{
24+
return static::$state->getStatus();
25+
}
26+
}

Behavioral/State/CreateOrder.php

+5-24
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,15 @@
22

33
namespace DesignPatterns\Behavioral\State;
44

5-
class CreateOrder implements Order
5+
class CreateOrder extends StateOrder
66
{
7-
/**
8-
* @var array
9-
*/
10-
private $details;
11-
12-
/**
13-
* @param array $details
14-
*/
15-
public function __construct(array $details)
16-
{
17-
$this->details = $details;
18-
}
19-
20-
public function shipOrder()
21-
{
22-
$this->details['status'] = 'shipping';
23-
$this->details['updatedTime'] = time();
24-
}
25-
26-
public function completeOrder()
7+
public function __construct()
278
{
28-
throw new \Exception('Can not complete the order which status is created');
9+
$this->setStatus('created');
2910
}
3011

31-
public function getStatus(): string
12+
protected function done()
3213
{
33-
return $this->details['status'];
14+
static::$state = new ShippingOrder();
3415
}
3516
}

Behavioral/State/Order.php

-18
This file was deleted.

Behavioral/State/OrderRepository.php

-34
This file was deleted.

Behavioral/State/README.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ Code
2020

2121
You can also find this code on `GitHub`_
2222

23-
OrderRepository.php
23+
ContextOrder.php
2424

25-
.. literalinclude:: OrderRepository.php
25+
.. literalinclude:: ContextOrder.php
2626
:language: php
2727
:linenos:
2828

29-
Order.php
29+
StateOrder.php
3030

31-
.. literalinclude:: Order.php
31+
.. literalinclude:: StateOrder.php
3232
:language: php
3333
:linenos:
3434

Behavioral/State/ShippingOrder.php

+5-24
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,15 @@
22

33
namespace DesignPatterns\Behavioral\State;
44

5-
class ShippingOrder implements Order
5+
class ShippingOrder extends StateOrder
66
{
7-
/**
8-
* @var array
9-
*/
10-
private $details;
11-
12-
/**
13-
* @param array $details
14-
*/
15-
public function __construct(array $details)
16-
{
17-
$this->details = $details;
18-
}
19-
20-
public function shipOrder()
21-
{
22-
throw new \Exception('Can not ship the order which status is shipping!');
23-
}
24-
25-
public function completeOrder()
7+
public function __construct()
268
{
27-
$this->details['status'] = 'completed';
28-
$this->details['updatedTime'] = time();
9+
$this->setStatus('shipping');
2910
}
3011

31-
public function getStatus(): string
12+
protected function done()
3213
{
33-
return $this->details['status'];
14+
$this->setStatus('completed');
3415
}
3516
}

Behavioral/State/StateOrder.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\State;
4+
5+
abstract class StateOrder
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $details;
11+
12+
/**
13+
* @var StateOrder $state
14+
*/
15+
protected static $state;
16+
17+
/**
18+
* @return mixed
19+
*/
20+
abstract protected function done();
21+
22+
protected function setStatus(string $status)
23+
{
24+
$this->details['status'] = $status;
25+
$this->details['updatedTime'] = time();
26+
}
27+
28+
protected function getStatus(): string
29+
{
30+
return $this->details['status'];
31+
}
32+
}

Behavioral/State/Tests/StateTest.php

+13-16
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@
22

33
namespace DesignPatterns\Behavioral\State\Tests;
44

5-
use DesignPatterns\Behavioral\State\OrderRepository;
5+
use DesignPatterns\Behavioral\State\ContextOrder;
6+
use DesignPatterns\Behavioral\State\CreateOrder;
67
use PHPUnit\Framework\TestCase;
78

89
class StateTest extends TestCase
910
{
1011
public function testCanShipCreatedOrder()
1112
{
12-
$order = (new OrderRepository())->findById(1);
13-
$order->shipOrder();
13+
$order = new CreateOrder();
14+
$contextOrder = new ContextOrder();
15+
$contextOrder->setState($order);
16+
$contextOrder->done();
1417

15-
$this->assertEquals('shipping', $order->getStatus());
18+
$this->assertEquals('shipping', $contextOrder->getStatus());
1619
}
1720

1821
public function testCanCompleteShippedOrder()
1922
{
20-
$order = (new OrderRepository())->findById(2);
21-
$order->completeOrder();
23+
$order = new CreateOrder();
24+
$contextOrder = new ContextOrder();
25+
$contextOrder->setState($order);
26+
$contextOrder->done();
27+
$contextOrder->done();
2228

23-
$this->assertEquals('completed', $order->getStatus());
24-
}
25-
26-
/**
27-
* @expectedException \Exception
28-
*/
29-
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder()
30-
{
31-
$order = (new OrderRepository())->findById(1);
32-
$order->completeOrder();
29+
$this->assertEquals('completed', $contextOrder->getStatus());
3330
}
3431
}

Behavioral/State/uml/uml.png

10.3 KB
Loading

0 commit comments

Comments
 (0)