1 | <?php |
||
15 | class Store extends Command |
||
16 | { |
||
17 | /** |
||
18 | * Persist the entity in the database |
||
19 | * |
||
20 | * @throws \InvalidArgumentException |
||
21 | * @return false|mixed |
||
22 | */ |
||
23 | public function execute() |
||
24 | { |
||
25 | $entity = $this->aggregate->getEntityObject(); |
||
26 | |||
27 | $mapper = $this->aggregate->getMapper(); |
||
28 | |||
29 | if ($mapper->fireEvent('storing', $entity) === false) { |
||
30 | return false; |
||
31 | } |
||
32 | |||
33 | $this->preStoreProcess(); |
||
34 | |||
35 | /** |
||
36 | * We will test the entity for existence |
||
37 | * and run a creation if it doesn't exists |
||
38 | */ |
||
39 | if (!$this->aggregate->exists()) { |
||
40 | if ($mapper->fireEvent('creating', $entity) === false) { |
||
41 | return false; |
||
42 | } |
||
43 | |||
44 | $this->insert(); |
||
45 | |||
46 | $mapper->fireEvent('created', $entity, false); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * We'll only run an update if the entity |
||
51 | * is actually dirty |
||
52 | */ |
||
53 | if ($this->aggregate->isDirty()) { |
||
54 | if ($mapper->fireEvent('updating', $entity) === false) { |
||
55 | return false; |
||
56 | } |
||
57 | $this->update(); |
||
58 | |||
59 | $mapper->fireEvent('updated', $entity, false); |
||
60 | } |
||
61 | |||
62 | $this->postStoreProcess(); |
||
63 | |||
64 | $mapper->fireEvent('stored', $entity, false); |
||
65 | |||
66 | return $entity; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Run all operations that have to occur before actually |
||
71 | * storing the entity |
||
72 | * |
||
73 | * @throws \InvalidArgumentException |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function preStoreProcess() |
||
77 | { |
||
78 | // Create any related object that doesn't exist in the database. |
||
79 | $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
||
80 | |||
81 | $this->createRelatedEntities($localRelationships); |
||
82 | |||
83 | // Now we can sync the related collections |
||
84 | $this->aggregate->syncRelationships($localRelationships); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Check for existence and create non-existing related entities |
||
89 | * |
||
90 | * @param array |
||
91 | * @throws \InvalidArgumentException |
||
92 | * @return void |
||
93 | */ |
||
94 | protected function createRelatedEntities($relations) |
||
102 | |||
103 | /** |
||
104 | * Create a new store command |
||
105 | * |
||
106 | * @param Aggregate $aggregate |
||
107 | * @return Store |
||
108 | */ |
||
109 | protected function createStoreCommand(Aggregate $aggregate) |
||
116 | |||
117 | /** |
||
118 | * Run all operations that have to occur after the entity |
||
119 | * is stored. |
||
120 | * |
||
121 | * @throws \InvalidArgumentException |
||
122 | * @return void |
||
123 | */ |
||
124 | protected function postStoreProcess() |
||
155 | |||
156 | /** |
||
157 | * Update Related Entities which attributes have |
||
158 | * been modified. |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | protected function updateDirtyRelated() |
||
206 | |||
207 | /** |
||
208 | * Execute an insert statement on the database |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | protected function insert() |
||
213 | { |
||
214 | $aggregate = $this->aggregate; |
||
215 | |||
216 | $attributes = $aggregate->getRawAttributes(); |
||
217 | |||
218 | $keyName = $aggregate->getEntityMap()->getKeyName(); |
||
219 | |||
220 | // Check if the primary key is defined in the attributes |
||
221 | if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
||
222 | $this->query->insert($attributes); |
||
223 | } else { |
||
224 | $sequence = $aggregate->getEntityMap()->getSequence(); |
||
225 | |||
226 | $id = $this->query->insertGetId($attributes, $sequence); |
||
227 | |||
228 | $aggregate->setEntityAttribute($keyName, $id); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Run an update statement on the entity |
||
234 | * |
||
235 | * @throws \InvalidArgumentException |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | protected function update() |
||
253 | } |
||
254 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: