User Workflow Mechanism in PHP

I am looking to implement user / workflow state processing in a PHP application.

Currently there are:

  • a user system with approximately 10 different states in which any user can be turned on (on, off, pre-registered, canceled, deleted, etc.).
  • certain rules regarding which state the user can switch to based on various system events
  • a rather dirty (but mostly working) system full of IF and SWITCH scattered around the place.

Want to:

  • replace the current IFfy state processing with a smart state machine that would define user transition rules
  • allow you to visualize these specific rules.
  • make the system more secure and bulletproof, making sure that the user can only be in legitimate states

My research:

I checked both SO and other places for implementing PHP workflows and machine states, and promising candidates seem to be

I would be grateful for any comments regarding any experience with any of the libraries above and / or opinions regarding suitability for what I need, or for tips in other places where I need to search.

+5
source share
1 answer

, , , factory, ?

, (, , ).

, - :

class StateFactory {
    $currentState;

    function __construct(){
        if(!isset($_SESSION['currentState'])){
            $this->currentState = 'StateOne';
        }
        else{
            $this->currentState = $_SESSION['currentState'];
        }

        $this->currentState = new {$this->currentState}->processState(); // I think something like this will work
    }

    function __deconstruct(){
        $_SESSION['currentState'] = $this->currentState;
    }
}

abstract class State{
    abstract function processState();
}

class StateOne extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateTwo';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateTwo extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateThree';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateThree extends State{
    ...
}

, , , -, , , , , .

+1

All Articles