1 | <?php |
||
42 | class Context { |
||
43 | |||
44 | /** |
||
45 | * the Identifier that uniquely identifies the statemachine |
||
46 | * |
||
47 | * @var Identifier |
||
48 | */ |
||
49 | protected $identifier; |
||
50 | |||
51 | /** |
||
52 | * the builder to get the reference to the entity. |
||
53 | * |
||
54 | * @var EntityBuilder |
||
55 | */ |
||
56 | protected $entity_builder; |
||
57 | |||
58 | /** |
||
59 | * the instance for getting to the persistence layer |
||
60 | * |
||
61 | * @var Adapter |
||
62 | */ |
||
63 | protected $persistence_adapter; |
||
64 | |||
65 | /** |
||
66 | * an associated statemachine, if one is set. |
||
67 | * Only a statemachine that uses this Context should set itself on the |
||
68 | * Context, providing a bidirectional association. |
||
69 | * |
||
70 | * @var StateMachine |
||
71 | */ |
||
72 | protected $statemachine; |
||
73 | |||
74 | /** |
||
75 | * Constructor |
||
76 | * |
||
77 | * @param Identifier $identifier |
||
78 | * the identifier for the statemachine |
||
79 | * @param EntityBuilder $entity_builder |
||
80 | * optional: A specific builder class to create a reference to |
||
81 | * the entity we wish to manipulate/have access to. |
||
82 | * @param Adapter $persistence_adapter |
||
83 | * optional: A specific reader/writer class can be used to |
||
84 | * generate different 'read/write' behaviour |
||
85 | */ |
||
86 | 88 | public function __construct(Identifier $identifier, $entity_builder = null, $persistence_adapter = null) |
|
92 | |||
93 | /** |
||
94 | * Provides a bidirectional association with the statemachine. |
||
95 | * This method should be called only by the StateMachine itself. |
||
96 | * |
||
97 | * @param StateMachine $statemachine |
||
98 | */ |
||
99 | 58 | public function setStateMachine(StateMachine $statemachine) |
|
103 | |||
104 | /** |
||
105 | * gets the associated statemachine (if a statemachine is associated) |
||
106 | * |
||
107 | * @return StateMachine |
||
108 | */ |
||
109 | 18 | public function getStateMachine() |
|
113 | |||
114 | /** |
||
115 | * Gets a (cached) reference to the application domain specific model, |
||
116 | * for example an 'Order' or 'Customer' that transitions through states in |
||
117 | * it's lifecycle. |
||
118 | * |
||
119 | * |
||
120 | * @param boolean $create_fresh_entity |
||
121 | * optional |
||
122 | * @return mixed |
||
123 | */ |
||
124 | 42 | public function getEntity($create_fresh_entity = false) |
|
129 | |||
130 | /** |
||
131 | * gets the state. |
||
132 | * first try the backend storage facility. If not found, then try the |
||
133 | * configured statemachine itself for the initial state. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 21 | public function getState() |
|
159 | |||
160 | /** |
||
161 | * Sets the state |
||
162 | * |
||
163 | * @param string $state |
||
164 | * @param string $message optional message. this can be used by the persistence adapter |
||
165 | * to be part of the transition history to provide extra information about the transition. |
||
166 | * @return boolan true if there was never any state persisted for this |
||
167 | * machine before (just added for the |
||
168 | * first time), false otherwise |
||
169 | */ |
||
170 | 18 | public function setState($state, $message = null) |
|
175 | |||
176 | /** |
||
177 | * adds the state data to the persistence layer if it is not there. |
||
178 | * Used to mark the initial construction of a statemachine at a certain |
||
179 | * point in time. subsequent calls to 'add' will not have any effect if it |
||
180 | * has already been persisted. |
||
181 | * |
||
182 | * @param string $state |
||
183 | * @param string $message optional message. this can be used by the persistence adapter |
||
184 | * to be part of the transition history to provide extra information about the transition. |
||
185 | * @return boolean true if it was added, false if it was already there |
||
186 | */ |
||
187 | 7 | public function add($state, $message = null) |
|
191 | |||
192 | /** |
||
193 | * returns the builder used to get the application domain specific model. |
||
194 | * |
||
195 | * @return EntityBuilder |
||
196 | */ |
||
197 | 43 | public function getBuilder() |
|
205 | |||
206 | /** |
||
207 | * gets the Context state reader/writer. |
||
208 | * |
||
209 | * @return Adapter a concrete persistence adapter |
||
210 | */ |
||
211 | 22 | public function getPersistenceAdapter() |
|
219 | |||
220 | /** |
||
221 | * gets the entity id that represents the unique identifier for the |
||
222 | * application domain specific model. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 9 | public function getEntityId() |
|
230 | |||
231 | /** |
||
232 | * get the Identifier |
||
233 | * |
||
234 | * @return Identifier |
||
235 | */ |
||
236 | 64 | public function getIdentifier() |
|
240 | |||
241 | /** |
||
242 | * gets the statemachine name that handles the entity |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | 26 | public function getMachine() |
|
250 | |||
251 | /** |
||
252 | * get the toString representation |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 10 | public function toString() |
|
260 | |||
261 | /** |
||
262 | * get the unique identifier for an Context, which consists of the machine |
||
263 | * name and the entity_id in parseable form, with an optional state |
||
264 | * |
||
265 | * @param boolean $readable |
||
266 | * human readable or not. defaults to false |
||
267 | * @param boolean $with_state |
||
268 | * append current state. defaults to false |
||
269 | * @return string |
||
270 | */ |
||
271 | 15 | public function getId($readable = false, $with_state = false) |
|
286 | |||
287 | 3 | public function __toString() |
|
291 | |||
292 | /** |
||
293 | * stores a failed transition, called by the statemachine |
||
294 | * This is a transition that has failed since it: |
||
295 | * - was not allowed |
||
296 | * - where an exception was thrown from a rule or command |
||
297 | * - etc. any general transition failure |
||
298 | * |
||
299 | * @param Transition $transition |
||
300 | * @param Exception $e |
||
301 | */ |
||
302 | 2 | public function setFailedTransition(Transition $transition, Exception $e) |
|
306 | } |