Pause and Continue

The process could be stopped at some point and continued later (with some additional info).
In the example we cannot proceed with the purchase till customer provide us with a credit card. So we stop the process till user fill the card. It could be a web form or other way to get the card number. Continue the once a user gives us the card.

Setup


mkdir ~/pvm-pause-and-continue
cd ~/pvm-pause-and-continue
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php pause-and-continue.php

Result

pause-and-continue.php

need a credit card 
ask customer to fill credit card
credit card is provided
purchased 

pause-and-continue.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\Exception\WaitExecutionException;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;

require_once 
__DIR__.'/vendor/autoload.php';

$process = (new ProcessBuilder())
    ->
createNode('purchase''purchase')->end()
    ->
createStartTransition('purchase')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'purchase' => function(Token $token) {
        if (
false ==$token->getValue('credit_card'false)) {
            echo 
'need a credit card '.PHP_EOL;
            throw new 
WaitExecutionException();
        }

        echo 
'purchased '.PHP_EOL;
    },
]));

$token $engine->createTokenFor($process->getStartTransition());

$waitTokens $engine->proceed($token);

// the process was paused because one of the tasks needs something.
echo 'ask customer to fill credit card'.PHP_EOL;

$waitTokens[0]->setValue('credit_card''4111 1111 1111 1111');

echo 
'credit card is provided'.PHP_EOL;

$engine->proceed($waitTokens[0]);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "f6de353c-3e0d-4004-abdb-f86f984c7df3",
    "nodes": {
        "purchase": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "purchase",
            "behavior": "purchase"
        }
    },
    "transitions": {
        "3541d21a-296a-4edb-9f20-d52a3322267a": {
            "id": "3541d21a-296a-4edb-9f20-d52a3322267a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase"
        }
    },
    "inTransitions": {
        "purchase": [
            "3541d21a-296a-4edb-9f20-d52a3322267a"
        ]
    },
    "tokens": {
        "65aaf8d5-fe7a-4e5b-992e-256e0a5bdabd": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "65aaf8d5-fe7a-4e5b-992e-256e0a5bdabd",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1a531965-a360-4c62-a603-7fd046f7a8f4",
                    "transitionId": "3541d21a-296a-4edb-9f20-d52a3322267a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450721915662
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f936be2f-e815-4dbf-85b0-be62dfaf969e",
                    "transitionId": "3541d21a-296a-4edb-9f20-d52a3322267a",
                    "weight": 1,
                    "state": "waiting",
                    "time": 15450721915667,
                    "reason": ""
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9f18d55a-7c38-4ad7-844f-b26da144f0f7",
                    "transitionId": "3541d21a-296a-4edb-9f20-d52a3322267a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450721915671
                }
            ],
            "credit_card": "4111 1111 1111 1111"
        }
    }
}