Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Behavioral/State/ContextOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DesignPatterns\Behavioral\State;

class ContextOrder extends StateOrder
{
public function getState():StateOrder
Copy link
Contributor

@gmsantos gmsantos May 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP 7 sintax should go to php7 branch #169 (comment). Master branch still PHP 5.6 compatible

Copy link
Contributor Author

@ko22009 ko22009 May 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@gmsantos gmsantos May 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, didn't know that php7 branch was merged on master #235

{
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();
}
}
29 changes: 5 additions & 24 deletions Behavioral/State/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,15 @@

namespace DesignPatterns\Behavioral\State;

class CreateOrder implements Order
class CreateOrder extends StateOrder
{
/**
* @var array
*/
private $details;

/**
* @param array $details
*/
public function __construct(array $details)
{
$this->details = $details;
}

public function shipOrder()
{
$this->details['status'] = 'shipping';
$this->details['updatedTime'] = time();
}

public function completeOrder()
public function __construct()
{
throw new \Exception('Can not complete the order which status is created');
$this->setStatus('created');
}

public function getStatus(): string
protected function done()
{
return $this->details['status'];
static::$state = new ShippingOrder();
}
}
18 changes: 0 additions & 18 deletions Behavioral/State/Order.php

This file was deleted.

34 changes: 0 additions & 34 deletions Behavioral/State/OrderRepository.php

This file was deleted.

8 changes: 4 additions & 4 deletions Behavioral/State/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
29 changes: 5 additions & 24 deletions Behavioral/State/ShippingOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
$this->details = $details;
}

public function shipOrder()
{
throw new \Exception('Can not ship the order which status is shipping!');
}

public function completeOrder()
public function __construct()
{
$this->details['status'] = 'completed';
$this->details['updatedTime'] = time();
$this->setStatus('shipping');
}

public function getStatus(): string
protected function done()
{
return $this->details['status'];
$this->setStatus('completed');
}
}
32 changes: 32 additions & 0 deletions Behavioral/State/StateOrder.php
Original file line number Diff line number Diff line change
@@ -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'];
}
}
29 changes: 13 additions & 16 deletions Behavioral/State/Tests/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why done() is fired twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's show, that's order from create go to shipping and after to completed. Our states is connected. It's normal behaviour, one order transform from one state to another.


$this->assertEquals('completed', $order->getStatus());
}

/**
* @expectedException \Exception
*/
public function testThrowsExceptionWhenTryingToCompleteCreatedOrder()
{
$order = (new OrderRepository())->findById(1);
$order->completeOrder();
$this->assertEquals('completed', $contextOrder->getStatus());
}
}
Binary file modified Behavioral/State/uml/uml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading