davispeixoto/workflow

A PHP package for managing state transitions

1.0.2 2019-01-24 08:49 UTC

README

A PHP package for dealing with state transitions.

Latest Stable Version Total Downloads Scrutinizer Code Quality Code Coverage Build Status

State transitions are a good way to manage lifecycles and pipelines on applications, as for example:

  • Order and payment status on an e-commerce
  • Invoice Status on a finance system
  • Ticket Status on ticket service desk system
  • Sales status on a CRM

Installation

The workflow package can be installed via Composer by requiring the davispeixoto/workflow package in your project's composer.json.

{
    "require": {
        "davispeixoto/workflow": "~1.0"
    }
}

Or

$ php composer.phar require davispeixoto/workflow

And running a composer update from your terminal:

php composer.phar update

Usage

To use it, first you need to create the status you are going to use for representing your states.

<?php
use MyCLabs\Enum\Enum;

class SalesStates extends Enum
{
    public const NEW = 'new';
    public const DEALING = 'dealing';
    public const WON = 'won';
    public const LOST = 'lost';
}

Then you can create your workflow based on valid transitions

<?php
use Davispeixoto\WorkflowInterface\Transition;
use Davispeixoto\WorkflowInterface\WorkflowInterface;

class SalesWorkflow extends WorkflowInterface
{
    public function __construct(SalesStates $initialStatus)
    {
        parent::__construct($initialStatus);

        // setup the transitions
        $transitions = [];

        $transitions[] = new Transition(
            new SalesStates(SalesStates::NEW),
            new SalesStates(SalesStates::DEALING)
        );

        $transitions[] = new Transition(
            new SalesStates(SalesStates::DEALING),
            new SalesStates(SalesStates::WON)
        );

        $transitions[] = new Transition(
            new SalesStates(SalesStates::DEALING),
            new SalesStates(SalesStates::LOST)
        );

        $this->allowedTransitions = $transitions;

        // setup the finished status, if any/needed
        $finished = [];

        $finished[] = new SalesStates(SalesStates::WON);
        $finished[] = new SalesStates(SalesStates::LOST);

        $this->finishedStatus = $finished;
    }
}

Now you can use this workflow to manage state transitions on you applications.

License

This software is licensed under the MIT license

Versioning

This project follows the Semantic Versioning

Thanks

For all PHP community