1 | <?php |
||
36 | class DataOperations |
||
37 | { |
||
38 | |||
39 | private $wrapper; |
||
40 | |||
41 | /** |
||
42 | * Private instance of driver adapter |
||
43 | * |
||
44 | * @var DriverAdapter |
||
45 | */ |
||
46 | private $adapter; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $data; |
||
53 | private $invalidFields; |
||
54 | private $hasMultipleData; |
||
55 | private $validator; |
||
56 | private $driver; |
||
57 | |||
58 | const MODE_SAVE = 0; |
||
59 | const MODE_UPDATE = 1; |
||
60 | |||
61 | 34 | public function __construct(RecordWrapper $wrapper, Driver $driver) |
|
67 | |||
68 | 10 | public function doSave($hasMultipleData) |
|
69 | { |
||
70 | 10 | $this->hasMultipleData = $hasMultipleData; |
|
71 | 10 | $invalidFields = []; |
|
72 | 10 | $data = $this->wrapper->getData(); |
|
73 | |||
74 | 10 | $primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
|
75 | 10 | $singlePrimaryKey = null; |
|
|
|||
76 | 10 | $succesful = true; |
|
77 | |||
78 | 10 | if (count($primaryKey) == 1) { |
|
79 | 10 | $singlePrimaryKey = $primaryKey[0]; |
|
80 | } |
||
81 | |||
82 | // Assign an empty array to force a validation error for empty models |
||
83 | 10 | if (empty($data)) { |
|
84 | 2 | $data = [[]]; |
|
85 | } |
||
86 | |||
87 | 10 | $this->driver->beginTransaction(); |
|
88 | |||
89 | 10 | foreach ($data as $i => $datum) { |
|
90 | 10 | $status = $this->saveRecord($datum, $primaryKey); |
|
91 | 10 | $data[$i] = $datum; |
|
92 | |||
93 | 10 | if (!$status['success']) { |
|
94 | 4 | $succesful = false; |
|
95 | 4 | $invalidFields[$i] = $status['invalid_fields']; |
|
96 | 4 | $this->driver->rollback(); |
|
97 | 10 | break; |
|
98 | } |
||
99 | } |
||
100 | |||
101 | 10 | if ($succesful) { |
|
102 | 6 | $this->driver->commit(); |
|
103 | } else { |
||
104 | 4 | $this->assignValue($this->invalidFields, $invalidFields); |
|
105 | } |
||
106 | |||
107 | 10 | $this->wrapper->setData($hasMultipleData ? $data : $data[0]); |
|
108 | |||
109 | 10 | return $succesful; |
|
110 | } |
||
111 | |||
112 | public function doValidate() |
||
113 | { |
||
114 | $record = $this->wrapper->getData()[0]; |
||
115 | $primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
||
116 | $pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
||
117 | return $this->validate( |
||
118 | $record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Save an individual record. |
||
124 | * |
||
125 | * @param array $record The record to be saved |
||
126 | * @param type $primaryKey The primary keys of the record |
||
127 | * @return boolean |
||
128 | */ |
||
129 | 10 | private function saveRecord(&$record, $primaryKey) |
|
130 | { |
||
131 | $status = [ |
||
132 | 10 | 'success' => true, |
|
133 | 'pk_assigned' => null, |
||
134 | 'invalid_fields' => [] |
||
135 | ]; |
||
136 | |||
137 | // Determine if the primary key of the record is set. |
||
138 | 10 | $pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
139 | |||
140 | // Reset the data in the model to contain only the data to be saved |
||
141 | 10 | $this->wrapper->setData($record); |
|
142 | |||
143 | // Run preUpdate or preSave callbacks on models and behaviours |
||
144 | 10 | if ($pkSet) { |
|
145 | 2 | $this->wrapper->preUpdateCallback(); |
|
146 | 2 | $record = $this->wrapper->getData(); |
|
147 | 2 | $record = reset($record) === false ? [] : reset($record); |
|
148 | } else { |
||
149 | 8 | $this->wrapper->preSaveCallback(); |
|
150 | 8 | $record = $this->wrapper->getData(); |
|
151 | 8 | $record = reset($record) === false ? [] : reset($record); |
|
152 | } |
||
153 | |||
154 | // Validate the data |
||
155 | 10 | $validity = $this->validate( |
|
156 | 10 | $record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
|
157 | ); |
||
158 | |||
159 | // Exit if data is invalid |
||
160 | 10 | if ($validity !== true) { |
|
161 | 4 | $status['invalid_fields'] = $validity; |
|
162 | 4 | $status['success'] = false; |
|
163 | 4 | return $status; |
|
164 | } |
||
165 | |||
166 | // Save any relationships that are attached to the data |
||
167 | 6 | $relationships = $this->wrapper->getDescription()->getRelationships(); |
|
168 | 6 | $presentRelationships = []; |
|
169 | |||
170 | 6 | foreach ($relationships ?? [] as $model => $relationship) { |
|
171 | 6 | if (isset($record[$model])) { |
|
172 | $relationship->preSave($record, $record[$model]); |
||
173 | 6 | $presentRelationships[$model] = $relationship; |
|
174 | } |
||
175 | } |
||
176 | |||
177 | // Assign the data to the wrapper again |
||
178 | 6 | $this->wrapper->setData($record); |
|
179 | |||
180 | // Update or save the data and run post callbacks |
||
181 | 6 | if ($pkSet) { |
|
182 | 2 | $this->adapter->update($record); |
|
183 | 2 | $this->wrapper->postUpdateCallback(); |
|
184 | } else { |
||
185 | 4 | $this->adapter->insert($record); |
|
186 | 4 | $keyValue = $this->driver->getLastInsertId(); |
|
187 | 4 | $this->wrapper->{$primaryKey[0]} = $keyValue; |
|
188 | 4 | $this->wrapper->postSaveCallback($keyValue); |
|
189 | } |
||
190 | |||
191 | // Reset the data so it contains any modifications made by callbacks |
||
192 | 6 | $record = $this->wrapper->getData()[0]; |
|
193 | 6 | foreach ($presentRelationships as $model => $relationship) { |
|
194 | $relationship->postSave($record); |
||
195 | } |
||
196 | |||
197 | 6 | return $status; |
|
198 | } |
||
199 | |||
200 | 10 | private function validate($data, $mode) |
|
201 | { |
||
202 | /*$validator = $this->container->resolve( |
||
203 | ModelValidator::class, ['model' => $this->wrapper, 'mode' => $mode] |
||
204 | );*/ |
||
205 | 10 | $validator = ORMContext::getInstance()->getModelValidatorFactory()->createModelValidator($this->wrapper, $mode); |
|
206 | 10 | $errors = []; |
|
207 | |||
208 | 10 | if (!$validator->validate($data)) { |
|
209 | 4 | $errors = $validator->getInvalidFields(); |
|
210 | } |
||
211 | 10 | $errors = $this->wrapper->onValidate($errors); |
|
212 | 10 | return empty($errors) ? true : $errors; |
|
213 | } |
||
214 | |||
215 | 10 | private function isPrimaryKeySet($primaryKey, $data) |
|
216 | { |
||
217 | 10 | if (is_string($primaryKey) && ($data[$primaryKey] !== null || $data[$primaryKey] !== '')) { |
|
218 | return true; |
||
219 | } |
||
220 | 10 | foreach ($primaryKey as $keyField) { |
|
221 | 10 | if (!isset($data[$keyField]) || $data[$keyField] === null || $data[$keyField] === '') { |
|
222 | 10 | return false; |
|
223 | } |
||
224 | } |
||
225 | 2 | return true; |
|
226 | } |
||
227 | |||
228 | 4 | private function assignValue(&$property, $value) |
|
229 | { |
||
230 | 4 | if ($this->hasMultipleData) { |
|
231 | $property = $value; |
||
232 | } else { |
||
233 | 4 | $property = $value[0]; |
|
234 | } |
||
235 | 4 | } |
|
236 | |||
237 | public function getData() |
||
241 | |||
242 | 10 | public function getInvalidFields() |
|
243 | { |
||
244 | 10 | return $this->invalidFields; |
|
245 | } |
||
246 | |||
247 | public function isItemDeletable($primaryKey, $data) |
||
248 | { |
||
255 | |||
256 | } |
||
257 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.