| Conditions | 22 |
| Paths | 400 |
| Total Lines | 108 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 230 | protected function processRecord($record, $columnMap, &$results, $preview = false) |
||
| 231 | { |
||
| 232 | $class = $this->objectClass; |
||
| 233 | |||
| 234 | // find existing object, or create new one |
||
| 235 | $existingObj = $this->findExistingObject($record, $columnMap); |
||
| 236 | /** @var DataObject $obj */ |
||
| 237 | $obj = ($existingObj) ? $existingObj : new $class(); |
||
| 238 | $schema = DataObject::getSchema(); |
||
| 239 | |||
| 240 | // first run: find/create any relations and store them on the object |
||
| 241 | // we can't combine runs, as other columns might rely on the relation being present |
||
| 242 | foreach ($record as $fieldName => $val) { |
||
| 243 | // don't bother querying of value is not set |
||
| 244 | if ($this->isNullValue($val)) { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | |||
| 248 | // checking for existing relations |
||
| 249 | if (isset($this->relationCallbacks[$fieldName])) { |
||
| 250 | // trigger custom search method for finding a relation based on the given value |
||
| 251 | // and write it back to the relation (or create a new object) |
||
| 252 | $relationName = $this->relationCallbacks[$fieldName]['relationname']; |
||
| 253 | /** @var DataObject $relationObj */ |
||
| 254 | $relationObj = null; |
||
| 255 | if ($this->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 256 | $relationObj = $this->{$this->relationCallbacks[$fieldName]['callback']}($obj, $val, $record); |
||
| 257 | } elseif ($obj->hasMethod($this->relationCallbacks[$fieldName]['callback'])) { |
||
| 258 | $relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record); |
||
| 259 | } |
||
| 260 | if (!$relationObj || !$relationObj->exists()) { |
||
| 261 | $relationClass = $schema->hasOneComponent(get_class($obj), $relationName); |
||
| 262 | $relationObj = new $relationClass(); |
||
| 263 | //write if we aren't previewing |
||
| 264 | if (!$preview) { |
||
| 265 | $relationObj->write(); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 269 | //write if we are not previewing |
||
| 270 | if (!$preview) { |
||
| 271 | $obj->write(); |
||
| 272 | $obj->flushCache(); // avoid relation caching confusion |
||
| 273 | } |
||
| 274 | } elseif (strpos($fieldName, '.') !== false) { |
||
| 275 | // we have a relation column with dot notation |
||
| 276 | list($relationName, $columnName) = explode('.', $fieldName); |
||
| 277 | // always gives us an component (either empty or existing) |
||
| 278 | $relationObj = $obj->getComponent($relationName); |
||
| 279 | if (!$preview) { |
||
| 280 | $relationObj->write(); |
||
| 281 | } |
||
| 282 | $obj->{"{$relationName}ID"} = $relationObj->ID; |
||
| 283 | |||
| 284 | //write if we are not previewing |
||
| 285 | if (!$preview) { |
||
| 286 | $obj->write(); |
||
| 287 | $obj->flushCache(); // avoid relation caching confusion |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | // second run: save data |
||
| 293 | |||
| 294 | foreach ($record as $fieldName => $val) { |
||
| 295 | // break out of the loop if we are previewing |
||
| 296 | if ($preview) { |
||
| 297 | break; |
||
| 298 | } |
||
| 299 | |||
| 300 | // look up the mapping to see if this needs to map to callback |
||
| 301 | $mapped = $this->columnMap && isset($this->columnMap[$fieldName]); |
||
| 302 | |||
| 303 | if ($mapped && strpos($this->columnMap[$fieldName], '->') === 0) { |
||
| 304 | $funcName = substr($this->columnMap[$fieldName], 2); |
||
| 305 | |||
| 306 | $this->$funcName($obj, $val, $record); |
||
| 307 | } elseif ($obj->hasMethod("import{$fieldName}")) { |
||
| 308 | $obj->{"import{$fieldName}"}($val, $record); |
||
| 309 | } else { |
||
| 310 | $obj->update(array($fieldName => $val)); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | // write record |
||
| 315 | if (!$preview) { |
||
| 316 | $obj->write(); |
||
| 317 | } |
||
| 318 | |||
| 319 | // @todo better message support |
||
| 320 | $message = ''; |
||
| 321 | |||
| 322 | // save to results |
||
| 323 | if ($existingObj) { |
||
| 324 | $results->addUpdated($obj, $message); |
||
| 325 | } else { |
||
| 326 | $results->addCreated($obj, $message); |
||
| 327 | } |
||
| 328 | |||
| 329 | $objID = $obj->ID; |
||
| 330 | |||
| 331 | $obj->destroy(); |
||
| 332 | |||
| 333 | // memory usage |
||
| 334 | unset($existingObj); |
||
| 335 | unset($obj); |
||
| 336 | |||
| 337 | return $objID; |
||
| 338 | } |
||
| 404 |