Keywords : State Design Pattern, Workflow, Finite State Machine, Symfony2
A demonstration
project for StateWorkflowBundle.
A Booking Entity
being first incomplete
, then waiting for payment
, then paid
then to delete
or cancelled
.
- State : BookingStateInterface instance
- Transition : BookingStateInterface method
The following States are all implementing BookingStateInterface
All available transitions are defined in BookingStateInterface
interface BookingStateInterface extends StateInterface
{
public function setBookingAsWaitingForPayment(HasStateInterface $booking);
public function setBookingAsPaid(HasStateInterface $booking);
public function cancelBooking(HasStateInterface $booking);
public function setBookingToBeDeleted(HasStateInterface $booking);
}
All States
are implementing AbstractBookingState.
Hence all transitions
are disabled by default because of
throw $this->buildUnsupportedTransitionException(__METHOD__, $booking);
Transitions are enabled when a BookingStateInterface
transition
method is overridden.
public function setBookingAsPaid(HasStateInterface $booking)
{
$newState = $this->getStateFromStateId(StatePaid::KEY, __METHOD__, $booking);
if ($newState) {
$booking->changeState($this->getStateWorkflow(), $newState);
// Implement necessary relevant transition here
}
return $newState;
}
Inside these transition
methods you can do what ever your want. And since each State is a service.
You can inject whatever you want.
- Log
- Event Sourcing
- Assertion
- Send mail
- etc..
Here is the generated Workflow Specification generated using the SpecGen command CLI sf spec-gen:state-workflow:generate-specifications
:
- Simple workflow demo.booking_engine.state_workflow.html
- More complex workflow demo.quote_engine.state_workflow.html
Cytoscape generates the workflow layout randomly. If the layout doesn't suit well, refresh. Don't hesitate to drag and drop.