1 | <?php |
||
49 | abstract class Adapter { |
||
50 | |||
51 | /** |
||
52 | * Get all the entity id's for a specific statemachine that have been persisted |
||
53 | * All entity id's in all states are returned unless a specific |
||
54 | * state is given via the optional parameter. |
||
55 | * |
||
56 | * This method will be highly useful when you want to select a batch of |
||
57 | * entities to feed to the statemachine, for example in a cron job or a |
||
58 | * message queue. |
||
59 | * |
||
60 | * @param string $machine |
||
61 | * the name of the machine |
||
62 | * @param string $state |
||
63 | * optional: if provided, only those entities in the specific state |
||
64 | * @return string[] an array of entity_id's |
||
65 | */ |
||
66 | abstract public function getEntityIds($machine, $state = null); |
||
67 | |||
68 | /** |
||
69 | * A template method to be able to process the setting of the current state. |
||
70 | * saves an object to a storage facility (either insert or update). |
||
71 | * Implement this method for specifying how you want to set a state in the |
||
72 | * storage facility. |
||
73 | * |
||
74 | * A storage facility could store a timestamp and the state the transition |
||
75 | * was made to, for extra statistical information. |
||
76 | * |
||
77 | * this method is public to be able to call it via the ReaderWriterDelegator |
||
78 | * |
||
79 | * @param Identifier $identifier |
||
80 | * @param string $state |
||
81 | * @return boolean true if just added to storage, false if stored before |
||
82 | */ |
||
83 | 19 | public function processSetState(Identifier $identifier, $state, $message = null) |
|
95 | |||
96 | /** |
||
97 | * Adds a history record for a transition |
||
98 | * |
||
99 | * @param Identifier $identifier |
||
100 | * @param string $state |
||
101 | * @param mixed $message string or array/object with relevant fields. |
||
102 | * an optional message (which might be exception data or not). |
||
103 | * @param boolean $is_exception |
||
104 | * an optional value, specifying if there was something |
||
105 | * exceptional or not. |
||
106 | * this can be used to signify an exception for storage in the |
||
107 | * backend so we can analyze the history |
||
108 | * for regular transitions and failed transitions |
||
109 | * @throws Exception |
||
110 | */ |
||
111 | protected function addHistory(Identifier $identifier, $state, $message = null, $is_exception = false) |
||
115 | |||
116 | /** |
||
117 | * insert state for Identifier into persistance layer. |
||
118 | * |
||
119 | * @param Identifier $identifier |
||
120 | * @param string $state |
||
121 | */ |
||
122 | protected function insertState(Identifier $identifier, $state, $message = null) |
||
126 | |||
127 | /** |
||
128 | * update state for statemachine/entity into persistance layer |
||
129 | * @param Identifier $identifier |
||
130 | * @param string $state |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | protected function updateState(Identifier $identifier, $state, $message = null) |
||
137 | |||
138 | /** |
||
139 | * A hook to be able to process the getting of the current state. |
||
140 | * Implement this method for specifying how you want to get a state from a |
||
141 | * storage facility. |
||
142 | * |
||
143 | * this method is public to be able to call it via the ReaderWriterDelegator |
||
144 | * |
||
145 | * @param Identifier $identifier |
||
146 | * @return string the current state of the entity represented in the context |
||
147 | */ |
||
148 | abstract public function processGetState(Identifier $identifier); |
||
149 | |||
150 | /** |
||
151 | * is the state information already persisted? |
||
152 | * |
||
153 | * @param Identifier $identifier |
||
154 | * @return boolean |
||
155 | * @throws Exception |
||
156 | */ |
||
157 | abstract public function isPersisted(Identifier $identifier); |
||
158 | |||
159 | /** |
||
160 | * Adds state information to the persistence layer so |
||
161 | * it can be manipulated by the statemachine package. |
||
162 | * It adds a record to the underlying implementation about when the stateful |
||
163 | * object was first generated/manipulated. |
||
164 | * |
||
165 | * This method can safely be called multiple times. It will only add data |
||
166 | * when there is no state information already present. |
||
167 | * |
||
168 | * This template method creates a record in the underlying persistence layer where |
||
169 | * it's initial state is set. |
||
170 | * It can then be manipulated via other methods via this Adapter or via |
||
171 | * the statemachine itself eg: via 'getEntityIds' etc. |
||
172 | * |
||
173 | * @param Identifier $identifier |
||
174 | * @param string $state |
||
175 | * the initial state to set, which should be known to |
||
176 | * the client of the statemachine the first time a machine is |
||
177 | * created. |
||
178 | * this can also be retrieved via a loaded statemachine: |
||
179 | * $machine->getInitialState()->getName() |
||
180 | * @param string $message optional message. this can be used by the persistence adapter |
||
181 | * to be part of the transition history to provide extra information about the transition. |
||
182 | * @return boolean true if it was added, false if it was already there. |
||
183 | * @throws Exception |
||
184 | */ |
||
185 | 8 | public function add(Identifier $identifier, $state, $message = null) |
|
186 | { |
||
187 | 8 | if ($this->isPersisted($identifier)) { |
|
188 | 5 | return false; |
|
189 | } |
||
190 | 8 | $this->addHistory($identifier, $state, $message); |
|
191 | 8 | $this->insertState($identifier, $state, $message); |
|
192 | 8 | return true; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get the current state for an Identifier (machine/id) |
||
197 | * |
||
198 | * A template method. |
||
199 | * |
||
200 | * @param Identifier $identifier |
||
201 | * @return string the state |
||
202 | * @throw Exception |
||
203 | */ |
||
204 | 23 | public function getState(Identifier $identifier) |
|
218 | |||
219 | /** |
||
220 | * Sets the new state for an Identifier in the storage facility. |
||
221 | * Will only be called by the statemachine. |
||
222 | * A template method. |
||
223 | * |
||
224 | * @param Identifier $identifier |
||
225 | * (old state can be retrieved via the identifier and this class) |
||
226 | * @param string $state |
||
227 | * this is the new state |
||
228 | * @param string $message optional message. this can be used by the persistence adapter |
||
229 | * to be part of the transition history to provide extra information about the transition. |
||
230 | * @return boolan false if already stored before, true if just added |
||
231 | * @throws Exception |
||
232 | */ |
||
233 | 21 | public function setState(Identifier $identifier, $state, $message = null) |
|
246 | |||
247 | /** |
||
248 | * A template method that Stores a failed transition in the storage facility for |
||
249 | * historical/analytical purposes. |
||
250 | * |
||
251 | * @param Identifier $identifier |
||
252 | * @param Transition $transition |
||
253 | * @param \Exception $e |
||
254 | */ |
||
255 | 3 | public function setFailedTransition(Identifier $identifier, Transition $transition, \Exception $e) |
|
286 | |||
287 | 3 | public function toString() |
|
291 | |||
292 | 1 | public function __toString() |
|
296 | } |
||
297 |