1 | <?php |
||
37 | class DataOperations |
||
38 | { |
||
39 | private $wrapper; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var DriverAdapter |
||
44 | */ |
||
45 | private $adapter; |
||
46 | private $data; |
||
47 | private $invalidFields; |
||
48 | private $validator; |
||
49 | private $hasMultipleData; |
||
50 | |||
51 | const MODE_SAVE = 0; |
||
52 | const MODE_UPDATE = 1; |
||
53 | |||
54 | 34 | public function __construct($wrapper, $adapter) |
|
55 | { |
||
56 | 34 | $this->wrapper = $wrapper; |
|
57 | 34 | $this->adapter = $adapter; |
|
58 | 34 | } |
|
59 | |||
60 | 10 | public function doSave($hasMultipleData) |
|
61 | { |
||
62 | 10 | $this->hasMultipleData = $hasMultipleData; |
|
63 | 10 | $invalidFields = []; |
|
64 | 10 | $data = $this->wrapper->getData(); |
|
65 | 10 | $this->adapter->setModel($this->wrapper); |
|
66 | 10 | $primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); |
|
67 | 10 | $singlePrimaryKey = null; |
|
|
|||
68 | 10 | $succesful = true; |
|
69 | |||
70 | 10 | if (count($primaryKey) == 1) { |
|
71 | 10 | $singlePrimaryKey = $primaryKey[0]; |
|
72 | } |
||
73 | |||
74 | // Assign an empty array to force a validation error for empty models |
||
75 | 10 | if(empty($data)) { |
|
76 | 2 | $data = [[]]; |
|
77 | } |
||
78 | |||
79 | 10 | Db::getDriver()->beginTransaction(); |
|
80 | |||
81 | 10 | foreach($data as $i => $datum) { |
|
82 | 10 | $status = $this->saveRecord($datum, $primaryKey); |
|
83 | 10 | $data[$i] = $datum; |
|
84 | |||
85 | 10 | if(!$status['success']) { |
|
86 | 4 | $succesful = false; |
|
87 | 4 | $invalidFields[$i] = $status['invalid_fields']; |
|
88 | 4 | Db::getDriver()->rollback(); |
|
89 | 10 | break; |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 10 | if($succesful) { |
|
94 | 6 | Db::getDriver()->commit(); |
|
95 | } else { |
||
96 | 4 | $this->assignValue($this->invalidFields, $invalidFields); |
|
97 | } |
||
98 | |||
99 | 10 | $this->wrapper->setData($hasMultipleData ? $data : $data[0]); |
|
100 | |||
101 | 10 | return $succesful; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Save an individual record. |
||
106 | * |
||
107 | * @param array $record The record to be saved |
||
108 | * @param type $primaryKey The primary keys of the record |
||
109 | * @return boolean |
||
110 | */ |
||
111 | 10 | private function saveRecord(&$record, $primaryKey) |
|
112 | { |
||
113 | $status = [ |
||
114 | 10 | 'success' => true, |
|
115 | 'pk_assigned' => null, |
||
116 | 'invalid_fields' => [] |
||
117 | ]; |
||
118 | |||
119 | // Determine if the primary key of the record is set. |
||
120 | 10 | $pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
121 | |||
122 | // Reset the data in the model to contain only the data to be saved |
||
123 | 10 | $this->wrapper->setData($record); |
|
124 | |||
125 | // Run preUpdate or preSave callbacks on models and behaviours |
||
126 | 10 | if($pkSet) { |
|
127 | 2 | $this->wrapper->preUpdateCallback(); |
|
128 | 2 | $record = $this->wrapper->getData(); |
|
129 | 2 | $record = reset($record) === false ? [] : reset($record); |
|
130 | 2 | $record = $this->runBehaviours('preUpdateCallback', [$record]); |
|
131 | } else { |
||
132 | 8 | $this->wrapper->preSaveCallback(); |
|
133 | 8 | $record = $this->wrapper->getData(); |
|
134 | 8 | $record = reset($record) === false ? [] : reset($record); |
|
135 | 8 | $record = $this->runBehaviours('preSaveCallback', [$record]); |
|
136 | } |
||
137 | |||
138 | // Validat the data |
||
139 | 10 | $validity = $this->validate( |
|
140 | $record, |
||
141 | 10 | $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
|
142 | ); |
||
143 | |||
144 | // Exit if data is invalid |
||
145 | 10 | if($validity !== true) { |
|
146 | 4 | $status['invalid_fields'] = $validity; |
|
147 | 4 | $status['success'] = false; |
|
148 | 4 | return $status; |
|
149 | } |
||
150 | |||
151 | // Assign the data to the wrapper again |
||
152 | 6 | $this->wrapper->setData($record); |
|
153 | |||
154 | // Update or save the data and run post callbacks |
||
155 | 6 | if($pkSet) { |
|
156 | 2 | $this->adapter->update($record); |
|
157 | 2 | $this->wrapper->postUpdateCallback(); |
|
158 | 2 | $this->runBehaviours('postUpdateCallback', [$record]); |
|
159 | } else { |
||
160 | 4 | $this->adapter->insert($record); |
|
161 | 4 | $keyValue = Db::getDriver()->getLastInsertId(); |
|
162 | 4 | $this->wrapper->{$primaryKey[0]} = $keyValue; |
|
163 | 4 | $this->wrapper->postSaveCallback($keyValue); |
|
164 | 4 | $this->runBehaviours('postSaveCallback', [$record, $keyValue]); |
|
165 | } |
||
166 | |||
167 | // Reset the data so it contains any modifications made by callbacks |
||
168 | 6 | $record = $this->wrapper->getData()[0]; |
|
169 | 6 | return $status; |
|
170 | } |
||
171 | |||
172 | 10 | private function validate($data, $mode) |
|
173 | { |
||
174 | 10 | $valid = true; |
|
175 | 10 | $validator = Utils::factory($this->validator, |
|
176 | 10 | function() use($mode) { |
|
177 | 10 | return new ModelValidator($this->wrapper, $mode); |
|
178 | 10 | } |
|
179 | ); |
||
180 | |||
181 | 10 | if(!$validator->validate($data)) { |
|
182 | 4 | $valid = false; |
|
183 | } |
||
184 | |||
185 | 10 | if($valid === false) { |
|
186 | 4 | $valid = $validator->getInvalidFields(); |
|
187 | } |
||
188 | |||
189 | 10 | return $valid; |
|
190 | } |
||
191 | |||
192 | |||
193 | 10 | private function isPrimaryKeySet($primaryKey, $data) |
|
194 | { |
||
195 | 10 | if(is_string($primaryKey)) { |
|
196 | if(isset($data[$primaryKey])) |
||
197 | { |
||
198 | return true; |
||
199 | } |
||
200 | } |
||
201 | 10 | foreach($primaryKey as $keyField) { |
|
202 | 10 | if(!isset($data[$keyField])) { |
|
203 | 8 | break; |
|
204 | } |
||
205 | 2 | if($data[$keyField] !== '' && $data[$keyField] !== null) { |
|
206 | 2 | return true; |
|
207 | } |
||
208 | } |
||
209 | 8 | return false; |
|
210 | } |
||
211 | |||
212 | 4 | private function assignValue(&$property, $value) |
|
213 | { |
||
214 | 4 | if($this->hasMultipleData) { |
|
215 | $property = $value; |
||
216 | } else { |
||
217 | 4 | $property = $value[0]; |
|
218 | } |
||
219 | 4 | } |
|
220 | |||
221 | public function getData() |
||
225 | |||
226 | 10 | public function getInvalidFields() |
|
230 | |||
231 | public function isItemDeletable($primaryKey, $data) |
||
239 | |||
240 | 10 | private function runBehaviours($event, $args) |
|
241 | { |
||
242 | 10 | foreach($this->wrapper->getBehaviours() as $behaviour) { |
|
243 | $args[0] = call_user_func_array([$behaviour, $event], $args); |
||
244 | } |
||
247 | } |
||
248 |
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.