PHP Class izzum\statemachine\Context

A Context is created by your application to provide the right dependencies ('context') for the statemachine to work with. It seperates the concerns for the statemachine of how you are reading/writing state data and of how you access your domain models. Important are: - the entity id, which references an application domain specific object like 'Order' or 'Customer' that goes through some finite states in it's lifecycle. - the machine name, which is the type identifier for the machine and related to the entity (eg: 'order-machine') - persistence adapter, which reads/writes to/from a storage facility - entity_builder, which constructs the stateful entity. The entity is the object that will be acted upon by the Statemachine. This stateful object will be uniquely identified by it's id, which will mostly be some sort of primary key for that object that is defined by the application specific implementation. A reference to the stateful object can be obtained via the factory method getEntity(). This class delegates reading and writing states to specific implementations of the Adapter classes. this is useful for testing and creating specific behaviour for statemachines that need extra functionality to get and set the correct states.
Author: Rolf Vreijdenberger
Show file Open project: rolfvreijdenberger/izzum-statemachine Class Usage Examples

Protected Properties

Property Type Description
$entity_builder EntityBuilder the builder to get the reference to the entity.
$identifier Identifier the Identifier that uniquely identifies the statemachine
$persistence_adapter izzum\statemachine\persistence\Adapter the instance for getting to the persistence layer
$statemachine StateMachine Only a statemachine that uses this Context should set itself on the Context, providing a bidirectional association.

Public Methods

Method Description
__construct ( Identifier $identifier, EntityBuilder $entity_builder = null, Adapter $persistence_adapter = null ) Constructor
__toString ( )
add ( string $state, string $message = null ) : boolean adds the state data to the persistence layer if it is not there.
getBuilder ( ) : EntityBuilder returns the builder used to get the application domain specific model.
getEntity ( boolean $create_fresh_entity = false ) : mixed Gets a (cached) reference to the application domain specific model, for example an 'Order' or 'Customer' that transitions through states in it's lifecycle.
getEntityId ( ) : string gets the entity id that represents the unique identifier for the application domain specific model.
getId ( boolean $readable = false, boolean $with_state = false ) : string get the unique identifier for an Context, which consists of the machine name and the entity_id in parseable form, with an optional state
getIdentifier ( ) : Identifier get the Identifier
getMachine ( ) : string gets the statemachine name that handles the entity
getPersistenceAdapter ( ) : Adapter gets the Context state reader/writer.
getState ( ) : string gets the state.
getStateMachine ( ) : StateMachine gets the associated statemachine (if a statemachine is associated)
setFailedTransition ( Transition $transition, Exception $e ) stores a failed transition, called by the statemachine This is a transition that has failed since it: - was not allowed - where an exception was thrown from a rule or command - etc. any general transition failure
setState ( string $state, string $message = null ) : boolan Sets the state
setStateMachine ( StateMachine $statemachine ) Provides a bidirectional association with the statemachine.
toString ( ) : string get the toString representation

Method Details

__construct() public method

Constructor
public __construct ( Identifier $identifier, EntityBuilder $entity_builder = null, Adapter $persistence_adapter = null )
$identifier Identifier the identifier for the statemachine
$entity_builder EntityBuilder optional: A specific builder class to create a reference to the entity we wish to manipulate/have access to.
$persistence_adapter izzum\statemachine\persistence\Adapter optional: A specific reader/writer class can be used to generate different 'read/write' behaviour

__toString() public method

public __toString ( )

add() public method

Used to mark the initial construction of a statemachine at a certain point in time. subsequent calls to 'add' will not have any effect if it has already been persisted.
public add ( string $state, string $message = null ) : boolean
$state string
$message string optional message. this can be used by the persistence adapter to be part of the transition history to provide extra information about the transition.
return boolean true if it was added, false if it was already there

getBuilder() public method

returns the builder used to get the application domain specific model.
public getBuilder ( ) : EntityBuilder
return EntityBuilder

getEntity() public method

Gets a (cached) reference to the application domain specific model, for example an 'Order' or 'Customer' that transitions through states in it's lifecycle.
public getEntity ( boolean $create_fresh_entity = false ) : mixed
$create_fresh_entity boolean optional
return mixed

getEntityId() public method

gets the entity id that represents the unique identifier for the application domain specific model.
public getEntityId ( ) : string
return string

getId() public method

get the unique identifier for an Context, which consists of the machine name and the entity_id in parseable form, with an optional state
public getId ( boolean $readable = false, boolean $with_state = false ) : string
$readable boolean human readable or not. defaults to false
$with_state boolean append current state. defaults to false
return string

getIdentifier() public method

get the Identifier
public getIdentifier ( ) : Identifier
return Identifier

getMachine() public method

gets the statemachine name that handles the entity
public getMachine ( ) : string
return string

getPersistenceAdapter() public method

gets the Context state reader/writer.
public getPersistenceAdapter ( ) : Adapter
return izzum\statemachine\persistence\Adapter a concrete persistence adapter

getState() public method

first try the backend storage facility. If not found, then try the configured statemachine itself for the initial state.
public getState ( ) : string
return string

getStateMachine() public method

gets the associated statemachine (if a statemachine is associated)
public getStateMachine ( ) : StateMachine
return StateMachine

setFailedTransition() public method

stores a failed transition, called by the statemachine This is a transition that has failed since it: - was not allowed - where an exception was thrown from a rule or command - etc. any general transition failure
public setFailedTransition ( Transition $transition, Exception $e )
$transition Transition
$e Exception

setState() public method

Sets the state
public setState ( string $state, string $message = null ) : boolan
$state string
$message string optional message. this can be used by the persistence adapter to be part of the transition history to provide extra information about the transition.
return boolan true if there was never any state persisted for this machine before (just added for the first time), false otherwise

setStateMachine() public method

This method should be called only by the StateMachine itself.
public setStateMachine ( StateMachine $statemachine )
$statemachine StateMachine

toString() public method

get the toString representation
public toString ( ) : string
return string

Property Details

$entity_builder protected property

the builder to get the reference to the entity.
protected EntityBuilder,izzum\statemachine $entity_builder
return EntityBuilder

$identifier protected property

the Identifier that uniquely identifies the statemachine
protected Identifier,izzum\statemachine $identifier
return Identifier

$persistence_adapter protected property

the instance for getting to the persistence layer
protected Adapter,izzum\statemachine\persistence $persistence_adapter
return izzum\statemachine\persistence\Adapter

$statemachine protected property

Only a statemachine that uses this Context should set itself on the Context, providing a bidirectional association.
protected StateMachine,izzum\statemachine $statemachine
return StateMachine