PHP Classes

File: example/ChopSuey.php

Recommend this page to a friend!
  Classes of Jorge Castro  >  PHP State Machine One  >  example/ChopSuey.php  >  Download  
File: example/ChopSuey.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP State Machine One
Process transitions in a state machine
Author: By
Last change: 2.4 2019-12-26
* 2.2 2019-10-22
* Updated eftec/MiniLang (dependency) from 2.9 => 2.12
* Updated eftec/pdoone (dependency) from 1.11 => 1.12
* Added dependency/documentstoreone (dependency) to 1.11 (it allows to use the filesystem as database)
* New methods setDocOne(), getDocOne()
* Now the library allows to use pdo (mysql database) or a file system (documentOne) for the persistence.
* Fixed a problem with the UI (it only executed the last job)
* Added changes to the UI. Now, it is possible to view and change the current job.
* Fixed a problem with the creation of table. Now the column TEXT_JOB is always created.
Date: 1 year ago
Size: 3,812 bytes


Class file image Download
 * @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
 * @link

use eftec\statemachineone\Job;


// it is specific for this project

define("EVENT_ABORT",'ABORT'); // it could be a number too
define("EVENT_FLIPABORT",'FLIP ABORT'); // it could be a number too
define("EVENT_CUSTOMER_PRESENT",'CUSTOMER PRESENT'); // it could be a number too
define("EVENT_CUSTOMER_ABSENT",'CUSTOMER ABSENT'); // it could be a number too
define("EVENT_ADDRESS_FOUND",'ADDRESS FOUND'); // it could be a number too
define("EVENT_ADDRESS_NOT_FOUND",'ADDRESS NOT FOUND'); // it could be a number too
define("EVENT_CUSTOM_METHOD",'CUSTOM METHOD'); // it could be a number too

class ServiceClass {
    public function
customMethod($instock) {
$obj=new ServiceClass();

$smachine=new StateMachineOne($obj);


$smachine->setAutoGarbage(false); // we don't want to delete automatically a stopped job.
$smachine->setStates([STATE_PICK=>'Pick order'
,STATE_CANCEL=>'Cancel order'
,STATE_TRANSPORT=>'Transport order'
,STATE_ABORTTRANSPORT=>'Abort the delivery'
,STATE_TODELIVER=>'Pending to deliver'
,STATE_HELP=>'Request assistance'

if (!
$smachine->getPdoOne()->tableExist($smachine->tableJobs)) {
$smachine->createDbTable(true); // you don't need to create this table every time.

$smachine->loadDBAllJob(); // we load all jobs, including finished ones.
//$smachine->loadDBActiveJobs(); // use this in production!.

// business rules
,'when instock = 0 set abort = 1','stop');
,'when instock = 1','change');
,'when abort = 1','stop');
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1 timeout 3600','stop'); // 1 hour max.
,'when addressnotfound = 1 or customerpresent = 0 timeout 3600','change'); // 1 hour max
,'when wait timeout 900 fulltimeout 2000','change'); // it waits 15 minutes max.
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1','change');

$smachine->addEvent(EVENT_ABORT,'set abort = 1');
$smachine->addEvent(EVENT_FLIPABORT,'set abort=flip()');
$smachine->addEvent(EVENT_CUSTOMER_PRESENT,'set customerpresent = 1');
$smachine->addEvent(EVENT_CUSTOMER_ABSENT,'set customerpresent = 0');
$smachine->addEvent(EVENT_ADDRESS_FOUND,'set addressnotfound = 0');
$smachine->addEvent(EVENT_ADDRESS_NOT_FOUND,'set addressnotfound =1');
$smachine->addEvent(EVENT_CUSTOM_METHOD,'set instock=customMethod(instock)');
// $smachine->callEvent(EVENT_ABORT);


$smachine->viewUI(null,$msg); // null means it takes the current job

For more information send a message to info at phpclasses dot org.