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
Afficher le fichier Open project: rolfvreijdenberger/izzum-statemachine Class Usage Examples

Protected Properties

Свойство 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.

Méthodes publiques

Méthode 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 méthode

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 méthode

public __toString ( )

add() public méthode

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.
Résultat boolean true if it was added, false if it was already there

getBuilder() public méthode

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

getEntity() public méthode

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
Résultat mixed

getEntityId() public méthode

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

getId() public méthode

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
Résultat string

getIdentifier() public méthode

get the Identifier
public getIdentifier ( ) : Identifier
Résultat Identifier

getMachine() public méthode

gets the statemachine name that handles the entity
public getMachine ( ) : string
Résultat string

getPersistenceAdapter() public méthode

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

getState() public méthode

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

getStateMachine() public méthode

gets the associated statemachine (if a statemachine is associated)
public getStateMachine ( ) : StateMachine
Résultat StateMachine

setFailedTransition() public méthode

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 méthode

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.
Résultat boolan true if there was never any state persisted for this machine before (just added for the first time), false otherwise

setStateMachine() public méthode

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

toString() public méthode

get the toString representation
public toString ( ) : string
Résultat string

Property Details

$entity_builder protected_oe property

the builder to get the reference to the entity.
protected EntityBuilder,izzum\statemachine $entity_builder
Résultat EntityBuilder

$identifier protected_oe property

the Identifier that uniquely identifies the statemachine
protected Identifier,izzum\statemachine $identifier
Résultat Identifier

$persistence_adapter protected_oe property

the instance for getting to the persistence layer
protected Adapter,izzum\statemachine\persistence $persistence_adapter
Résultat izzum\statemachine\persistence\Adapter

$statemachine protected_oe property

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