Commit 0c4bb6dd authored by Jan Kuchař's avatar Jan Kuchař

example order-state: renamed preparing -> processing

parent 4ffbcd20
......@@ -19,7 +19,7 @@ class OrderService
{
public const STATE_RECEIVED = 'received';
public const STATE_PREPARING = 'preparing';
public const STATE_PROCESSING = 'processing';
public const STATE_FINISHED = 'finished';
public const STATE_CANCELLED = 'cancelled';
......@@ -31,8 +31,8 @@ class OrderService
switch ($currentState) {
case self::STATE_RECEIVED:
return $desiredState === self::STATE_PREPARING || $desiredState === self::STATE_CANCELLED;
case self::STATE_PREPARING:
return $desiredState === self::STATE_PROCESSING || $desiredState === self::STATE_CANCELLED;
case self::STATE_PROCESSING:
return $desiredState === self::STATE_FINISHED;
case self::STATE_FINISHED:
return FALSE;
......@@ -51,12 +51,12 @@ $orderService = new OrderService();
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_RECEIVED,
OrderService::STATE_PREPARING
OrderService::STATE_PROCESSING
)
);
Assert::true(
$orderService->canDoTransition(
OrderService::STATE_PREPARING,
OrderService::STATE_PROCESSING,
OrderService::STATE_FINISHED
)
);
......@@ -91,7 +91,7 @@ Assert::false(
);
Assert::false(
$orderService->canDoTransition(
OrderService::STATE_PREPARING,
OrderService::STATE_PROCESSING,
OrderService::STATE_CANCELLED
)
);
......
......@@ -15,7 +15,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PREPARING()
* @method static OrderState PROCESSING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
......@@ -26,14 +26,13 @@ final class OrderState extends Enum
protected const
RECEIVED = 'received',
PREPARING = 'preparing',
PROCESSING = 'processing',
FINISHED = 'finished',
CANCELLED = 'cancelled';
}
class OrderService
{
......@@ -41,10 +40,10 @@ class OrderService
{
if ($currentState === OrderState::RECEIVED()) {
return $desiredState === OrderState::PREPARING() || $desiredState === OrderState::CANCELLED();
return $desiredState === OrderState::PROCESSING() || $desiredState === OrderState::CANCELLED();
}
if ($currentState === OrderState::PREPARING()) {
if ($currentState === OrderState::PROCESSING()) {
return $desiredState === OrderState::FINISHED();
}
......@@ -67,12 +66,12 @@ $orderService = new OrderService();
Assert::true(
$orderService->canDoTransition(
OrderState::RECEIVED(),
OrderState::PREPARING()
OrderState::PROCESSING()
)
);
Assert::true(
$orderService->canDoTransition(
OrderState::PREPARING(),
OrderState::PROCESSING(),
OrderState::FINISHED()
)
);
......@@ -107,7 +106,7 @@ Assert::false(
);
Assert::false(
$orderService->canDoTransition(
OrderState::PREPARING(),
OrderState::PROCESSING(),
OrderState::CANCELLED()
)
);
......
......@@ -16,7 +16,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PREPARING()
* @method static OrderState PROCESSING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
......@@ -27,7 +27,7 @@ final class OrderState extends Enum
protected const
RECEIVED = 'received',
PREPARING = 'preparing',
PROCESSING = 'processing',
FINISHED = 'finished',
CANCELLED = 'cancelled';
......@@ -36,10 +36,10 @@ final class OrderState extends Enum
public function canDoTransition(OrderState $desiredState): bool
{
if ($this === self::RECEIVED()) {
return $desiredState === self::PREPARING() || $desiredState === self::CANCELLED();
return $desiredState === self::PROCESSING() || $desiredState === self::CANCELLED();
}
if ($this === self::PREPARING()) {
if ($this === self::PROCESSING()) {
return $desiredState === self::FINISHED();
}
......@@ -59,11 +59,11 @@ final class OrderState extends Enum
// Standard order flow:
Assert::true(
OrderState::RECEIVED()->canDoTransition(
OrderState::PREPARING()
OrderState::PROCESSING()
)
);
Assert::true(
OrderState::PREPARING()->canDoTransition(
OrderState::PROCESSING()->canDoTransition(
OrderState::FINISHED()
)
);
......@@ -94,7 +94,7 @@ Assert::false(
)
);
Assert::false(
OrderState::PREPARING()->canDoTransition(
OrderState::PROCESSING()->canDoTransition(
OrderState::CANCELLED()
)
);
......@@ -104,3 +104,21 @@ Assert::false(
)
);
$state1 = OrderState::RECEIVED();
$state2 = OrderState::RECEIVED();
Assert::true($state1 === $state2);
$state3 = OrderState::PROCESSING();
Assert::true($state1 !== $state3);
Assert::true($state2 !== $state3);
......@@ -16,7 +16,7 @@ final class InvalidTransitionException extends \RuntimeException {}
/**
* @method static OrderState RECEIVED()
* @method static OrderState PREPARING()
* @method static OrderState PROCESSING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
......@@ -25,7 +25,7 @@ final class OrderState extends Enum
protected const
RECEIVED = 'received',
PREPARING = 'preparing',
PROCESSING = 'processing',
FINISHED = 'finished',
CANCELLED = 'cancelled'; // domain logic: can be cancelled before preparation is started
......@@ -57,8 +57,8 @@ final class OrderState extends Enum
// again and you will get infinite loop.
return [
self::RECEIVED => new self([self::PREPARING, self::CANCELLED]),
self::PREPARING => new self([self::FINISHED]),
self::RECEIVED => new self([self::PROCESSING, self::CANCELLED]),
self::PROCESSING => new self([self::FINISHED]),
self::FINISHED => new self([]),
self::CANCELLED => new self([]),
];
......@@ -70,11 +70,11 @@ final class OrderState extends Enum
// Standard order flow:
Assert::true(
OrderState::RECEIVED()->canDoTransition(
OrderState::PREPARING()
OrderState::PROCESSING()
)
);
Assert::true(
OrderState::PREPARING()->canDoTransition(
OrderState::PROCESSING()->canDoTransition(
OrderState::FINISHED()
)
);
......@@ -104,7 +104,7 @@ Assert::false(
)
);
Assert::false(
OrderState::PREPARING()->canDoTransition(
OrderState::PROCESSING()->canDoTransition(
OrderState::CANCELLED()
)
);
......
......@@ -44,7 +44,7 @@ class Order
/**
* @method static OrderState RECEIVED()
* @method static OrderState PREPARING()
* @method static OrderState PROCESSING()
* @method static OrderState FINISHED()
* @method static OrderState CANCELLED()
*/
......@@ -52,7 +52,7 @@ abstract class OrderState extends Enum {
protected const
RECEIVED = 'received',
PREPARING = 'preparing',
PROCESSING = 'processing',
FINISHED = 'finished',
CANCELLED = 'cancelled'; // domain logic: can be cancelled before preparation is started
......@@ -83,19 +83,17 @@ abstract class OrderState extends Enum {
public function canDoTransition(OrderState $nextState): bool
{
return $nextState === $this::PREPARING() || $nextState === $this::CANCELLED();
return $nextState === $this::PROCESSING() || $nextState === $this::CANCELLED();
}
},
self::PREPARING => new class extends OrderState {
self::PROCESSING => new class extends OrderState {
public function canDoTransition(OrderState $nextState): bool
{
return $nextState === $this::FINISHED();
}
},
......@@ -134,7 +132,7 @@ abstract class OrderState extends Enum {
(function() {
$order = new Order();
Assert::same('employee', $order->getEmployee());
$order->changeState(OrderState::PREPARING());
$order->changeState(OrderState::PROCESSING());
Assert::same('employee', $order->getEmployee());
$order->changeState(OrderState::FINISHED());
Assert::null($order->getEmployee());
......@@ -159,13 +157,13 @@ Assert::exception(function () {
Assert::exception(function () {
$order = new Order();
$order->changeState(OrderState::PREPARING());
$order->changeState(OrderState::PROCESSING());
$order->changeState(OrderState::CANCELLED()); // not allowed
}, InvalidTransitionException::class);
Assert::exception(function () {
$order = new Order();
$order->changeState(OrderState::PREPARING());
$order->changeState(OrderState::PROCESSING());
$order->changeState(OrderState::FINISHED());
$order->changeState(OrderState::CANCELLED()); // not allowed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment