1 | <?php |
||
55 | class EntityBuilder { |
||
56 | |||
57 | /** |
||
58 | * a cached instance of the built entity object |
||
59 | * |
||
60 | * @var Object |
||
61 | */ |
||
62 | protected $entity; |
||
63 | |||
64 | /** |
||
65 | * a cached instance of the used Identifier. |
||
66 | * |
||
67 | * @var Identifier |
||
68 | */ |
||
69 | protected $identifier; |
||
70 | |||
71 | /** |
||
72 | * Gets an application domain specific model of choice, as implemented by a |
||
73 | * subclass. |
||
74 | * |
||
75 | * @param Identifier $identifier |
||
76 | * @param boolean $create_fresh_entity |
||
77 | * optional. if true, then a new instance is always created, |
||
78 | * else it might be cached if used for the same Identifier |
||
79 | * (performance). |
||
80 | * Since a statemachine might run multiple transitions in memory |
||
81 | * and alter the data in the persistence layer, |
||
82 | * there might be a need to refresh the entity (create it again) |
||
83 | * if the in memory object and the data |
||
84 | * in the persistence layer are not synchronized. this might |
||
85 | * cause rules or commands to act on |
||
86 | * in-memory data while it should use the persisted data. |
||
87 | * (an ORM should handle this automatically) |
||
88 | * |
||
89 | * @return Object an object of any type, depending on the statemachine. |
||
90 | * This object will be used by the Rule and Command that go with a |
||
91 | * certain statemachine. It will be injected in the constructor of |
||
92 | * the |
||
93 | * Rule and Command. |
||
94 | * It implements caching so we always get the same entity instance |
||
95 | * on |
||
96 | * each call of 'getEntity' with the same Context |
||
97 | * @see Context::getEntity() |
||
98 | * @throws Exception |
||
99 | */ |
||
100 | 46 | final public function getEntity(Identifier $identifier, $create_fresh_entity = false) |
|
123 | |||
124 | /** |
||
125 | * the actual building function. |
||
126 | * Override this method to return an application specific domain model. |
||
127 | * |
||
128 | * In an overriden function it is possible to use the state of the concrete |
||
129 | * builder itself, which can be passed in via dependency injection at |
||
130 | * construction time, so we have additional information on how to build the |
||
131 | * domain object. |
||
132 | * |
||
133 | * @param Identifier $identifier |
||
134 | * @return Object an object of any type defined by the subclass |
||
135 | */ |
||
136 | 40 | protected function build(Identifier $identifier) |
|
145 | |||
146 | /** |
||
147 | * returns the string representation |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 2 | public function toString() |
|
155 | |||
156 | /** |
||
157 | * returns the string representation |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 1 | public function __toString() |
|
165 | } |
||
166 |