gmorel/state-workflow-demo

dev-master 2015-09-13 12:18 UTC

This package is not auto-updated.

Last update: 2024-04-17 07:38:46 UTC


README

Symfony 2

State Workflow Demo ===================

Helping you implementing a complex yet easily maintainable workflow.

Keywords : State Design Pattern, Workflow, Finite State Machine, Symfony2

A demonstration project for StateWorkflowBundle.

Demo Booking Workflow simple

Context

A Booking Entity being first incomplete, then waiting for payment, then paid then to delete or cancelled.

State declaration

The following States are all implementing BookingStateInterface

Default transitions - disabled

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);

Enabled transitions

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..

Examples

Here is the generated Workflow Specification generated using the SpecGen command CLI sf spec-gen:state-workflow:generate-specifications :

Cytoscape generates the workflow layout randomly. If the layout doesn't suit well, refresh. Don't hesitate to drag and drop.