-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
913 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order; | ||
|
||
use DesiredPatterns\State\StateMachineTrait; | ||
use Examples\State\Order\States\{ | ||
PendingState, | ||
ProcessingState, | ||
ShippedState, | ||
DeliveredState, | ||
CancelledState | ||
}; | ||
|
||
class Order | ||
{ | ||
use StateMachineTrait; | ||
|
||
private string $orderId; | ||
|
||
public function __construct(string $orderId) | ||
{ | ||
$this->orderId = $orderId; | ||
|
||
// Initialize all possible states | ||
$this->addState(new PendingState(), true) // Initial state | ||
->addState(new ProcessingState()) | ||
->addState(new ShippedState()) | ||
->addState(new DeliveredState()) | ||
->addState(new CancelledState()); | ||
|
||
// Set initial context | ||
$this->updateContext([ | ||
'order_id' => $orderId, | ||
'created_at' => date('Y-m-d H:i:s') | ||
]); | ||
} | ||
|
||
public function getOrderId(): string | ||
{ | ||
return $this->orderId; | ||
} | ||
|
||
public function process(array $paymentDetails): array | ||
{ | ||
$this->transitionTo('processing', $paymentDetails); | ||
return $this->getCurrentState()->handle($this->getContext()); | ||
} | ||
|
||
public function ship(array $shippingDetails): array | ||
{ | ||
$this->transitionTo('shipped', $shippingDetails); | ||
return $this->getCurrentState()->handle($this->getContext()); | ||
} | ||
|
||
public function deliver(array $deliveryDetails): array | ||
{ | ||
$this->transitionTo('delivered', $deliveryDetails); | ||
return $this->getCurrentState()->handle($this->getContext()); | ||
} | ||
|
||
public function cancel(string $reason): array | ||
{ | ||
$this->transitionTo('cancelled', ['cancellation_reason' => $reason]); | ||
return $this->getCurrentState()->handle($this->getContext()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order\States; | ||
|
||
use DesiredPatterns\State\AbstractState; | ||
|
||
class CancelledState extends AbstractState | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'cancelled'; | ||
} | ||
|
||
protected array $allowedTransitions = []; // Final state | ||
|
||
protected array $validationRules = [ | ||
'order_id' => 'required', | ||
'cancellation_reason' => 'required' | ||
]; | ||
|
||
public function handle(array $context): array | ||
{ | ||
// Process refund and cleanup | ||
return [ | ||
'status' => 'cancelled', | ||
'message' => 'Order has been cancelled', | ||
'order_id' => $context['order_id'], | ||
'reason' => $context['cancellation_reason'], | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order\States; | ||
|
||
use DesiredPatterns\State\AbstractState; | ||
|
||
class DeliveredState extends AbstractState | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'delivered'; | ||
} | ||
|
||
protected array $allowedTransitions = []; // Final state | ||
|
||
protected array $validationRules = [ | ||
'order_id' => 'required', | ||
'delivery_date' => 'required', | ||
'signature' => 'required' | ||
]; | ||
|
||
public function handle(array $context): array | ||
{ | ||
// Complete the order and trigger post-delivery actions | ||
return [ | ||
'status' => 'delivered', | ||
'message' => 'Order has been delivered', | ||
'order_id' => $context['order_id'], | ||
'delivery_date' => $context['delivery_date'], | ||
'signature' => $context['signature'], | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order\States; | ||
|
||
use DesiredPatterns\State\AbstractState; | ||
|
||
class PendingState extends AbstractState | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'pending'; | ||
} | ||
|
||
protected array $allowedTransitions = ['processing', 'cancelled']; | ||
|
||
protected array $validationRules = [ | ||
'order_id' => 'required', | ||
'total_amount' => 'type:double', | ||
'items' => 'type:array' | ||
]; | ||
|
||
public function handle(array $context): array | ||
{ | ||
// Validate order and check inventory | ||
return [ | ||
'status' => 'pending', | ||
'message' => 'Order is being validated', | ||
'order_id' => $context['order_id'], | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order\States; | ||
|
||
use DesiredPatterns\State\AbstractState; | ||
|
||
class ProcessingState extends AbstractState | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'processing'; | ||
} | ||
|
||
protected array $allowedTransitions = ['shipped', 'cancelled']; | ||
|
||
protected array $validationRules = [ | ||
'order_id' => 'required', | ||
'payment_id' => 'required', | ||
'payment_status' => 'required' | ||
]; | ||
|
||
public function handle(array $context): array | ||
{ | ||
// Process payment and prepare for shipping | ||
return [ | ||
'status' => 'processing', | ||
'message' => 'Payment verified, preparing shipment', | ||
'order_id' => $context['order_id'], | ||
'payment_id' => $context['payment_id'], | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\State\Order\States; | ||
|
||
use DesiredPatterns\State\AbstractState; | ||
|
||
class ShippedState extends AbstractState | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'shipped'; | ||
} | ||
|
||
protected array $allowedTransitions = ['delivered']; | ||
|
||
protected array $validationRules = [ | ||
'order_id' => 'required', | ||
'tracking_number' => 'required', | ||
'shipping_address' => 'required' | ||
]; | ||
|
||
public function handle(array $context): array | ||
{ | ||
// Generate shipping label and notify courier | ||
return [ | ||
'status' => 'shipped', | ||
'message' => 'Order has been shipped', | ||
'order_id' => $context['order_id'], | ||
'tracking_number' => $context['tracking_number'], | ||
'shipping_address' => $context['shipping_address'], | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]; | ||
} | ||
} |
Oops, something went wrong.