| Total Complexity | 154 |
| Total Lines | 938 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Mapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Mapper extends Service |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | public static $SINGLE = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | public static $MULTIPLE = true; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var |
||
| 35 | */ |
||
| 36 | private $file = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var JsonMachine |
||
| 40 | */ |
||
| 41 | private $productStream; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var JsonMachine |
||
| 45 | */ |
||
| 46 | private $assetStream; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $currentUniqueFields = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | private $importCount = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | public $skipSilently = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Mapper constructor. |
||
| 65 | * @param string $importerKey |
||
| 66 | * @param $file |
||
| 67 | * @throws \Exception |
||
| 68 | */ |
||
| 69 | public function __construct($importerKey, $file = null) |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Generates the current hash for the mapping |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | private function getMappingHash() |
||
| 89 | { |
||
| 90 | return md5(serialize($this->config()->get('mapping'))); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Updates the mapping hash for the mapper |
||
| 95 | * @param DataObject|SalsifyIDExtension $object |
||
| 96 | * @param bool $relations |
||
| 97 | */ |
||
| 98 | private function updateMappingHash($object, $relations) |
||
| 99 | { |
||
| 100 | $filter = [ |
||
| 101 | 'MapperService' => $this->importerKey, |
||
| 102 | 'ForRelations' => $relations, |
||
| 103 | ]; |
||
| 104 | /** @var MapperHash $mapperHash */ |
||
| 105 | $mapperHash = $object->MapperHashes()->filter($filter)->first(); |
||
| 106 | if ($mapperHash == null) { |
||
| 107 | $mapperHash = MapperHash::create(); |
||
| 108 | $mapperHash->MappedObjectID = $object->ID; |
||
| 109 | $mapperHash->MappedObjectClass = $object->ClassName; |
||
| 110 | $mapperHash->MapperService = $this->importerKey; |
||
| 111 | $mapperHash->ForRelations = $relations; |
||
| 112 | } |
||
| 113 | |||
| 114 | $mapperHash->MapperHash = $this->getMappingHash(); |
||
| 115 | $mapperHash->write(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | public function resetProductStream() |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * |
||
| 128 | */ |
||
| 129 | public function resetAssetStream() |
||
| 130 | { |
||
| 131 | $this->assetStream = JsonMachine::fromFile($this->file, '/3/digital_assets'); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Maps the data |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | public function map() |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string|DataObject $class |
||
| 169 | * @param array $mappings The mapping for a specific class |
||
| 170 | * @param array $data |
||
| 171 | * @param DataObject|null $object |
||
| 172 | * @param bool $salsifyRelations |
||
| 173 | * @param bool $forceUpdate |
||
| 174 | * |
||
| 175 | * @return DataObject|null |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | public function mapToObject( |
||
| 179 | $class, |
||
| 180 | $mappings, |
||
| 181 | $data, |
||
| 182 | $object = null, |
||
| 183 | $salsifyRelations = false, |
||
| 184 | $forceUpdate = false |
||
| 185 | ) { |
||
| 186 | if ($salsifyRelations) { |
||
| 187 | if (!$this->classConfigHasSalsifyRelation($mappings)) { |
||
| 188 | return null; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | // if object was not passed |
||
| 193 | if ($object === null) { |
||
| 194 | $object = $this->findObjectByUnique($class, $mappings, $data); |
||
| 195 | |||
| 196 | // if no existing object was found but a unique filter is valid (not empty) |
||
| 197 | if (!$object) { |
||
| 198 | // don't try to create related objects that don't exist |
||
| 199 | if (!$salsifyRelations) { |
||
| 200 | return null; |
||
| 201 | } |
||
| 202 | |||
| 203 | if (!$this->hasValidUniqueFilter($class, $mappings, $data)) { |
||
| 204 | return null; |
||
| 205 | } |
||
| 206 | |||
| 207 | $object = $class::create(); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $wasPublished = $object->hasExtension(Versioned::class) ? $object->isPublished() : false; |
||
| 212 | $wasWritten = $object->isInDB(); |
||
| 213 | |||
| 214 | $firstUniqueKey = array_keys($this->uniqueFields($class, $mappings))[0]; |
||
| 215 | if (array_key_exists($mappings[$firstUniqueKey]['salsifyField'], $data)) { |
||
| 216 | $firstUniqueValue = $data[$mappings[$firstUniqueKey]['salsifyField']]; |
||
| 217 | } else { |
||
| 218 | $firstUniqueValue = 'NULL'; |
||
| 219 | } |
||
| 220 | ImportTask::output("Updating $class $firstUniqueKey $firstUniqueValue"); |
||
| 221 | |||
| 222 | if ( |
||
| 223 | !$forceUpdate && |
||
| 224 | $this->objectUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue, $salsifyRelations) |
||
| 225 | ) { |
||
| 226 | return $object; |
||
| 227 | } |
||
| 228 | |||
| 229 | foreach ($this->yieldKeyVal($mappings) as $dbField => $salsifyField) { |
||
| 230 | $field = $this->getField($salsifyField, $data); |
||
| 231 | if ($field === false) { |
||
| 232 | $this->clearValue($object, $dbField, $salsifyField); |
||
| 233 | continue; |
||
| 234 | } |
||
| 235 | |||
| 236 | $type = $this->getFieldType($salsifyField); |
||
| 237 | // skip all but salsify relations types if not doing relations |
||
| 238 | if ($salsifyRelations && !$this->typeRequiresSalsifyObjects($type)) { |
||
| 239 | continue; |
||
| 240 | } |
||
| 241 | |||
| 242 | // skip salsify relations types if not doing relations |
||
| 243 | if (!$salsifyRelations && $this->typeRequiresSalsifyObjects($type)) { |
||
| 244 | continue; |
||
| 245 | } |
||
| 246 | |||
| 247 | $value = null; |
||
| 248 | $objectData = $data; |
||
| 249 | |||
| 250 | if ($this->handleShouldSkip($class, $dbField, $salsifyField, $data)) { |
||
| 251 | if (!$this->skipSilently) { |
||
| 252 | ImportTask::output("Skipping $class $firstUniqueKey $firstUniqueValue"); |
||
| 253 | $this->skipSilently = false; |
||
| 254 | } |
||
| 255 | return false; |
||
| 256 | }; |
||
| 257 | |||
| 258 | $objectData = $this->handleModification($type, $class, $dbField, $salsifyField, $data); |
||
| 259 | $sortColumn = $this->getSortColumn($salsifyField); |
||
| 260 | |||
| 261 | if ($salsifyRelations == false && !array_key_exists($field, $objectData)) { |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | |||
| 265 | $value = $this->handleType($type, $class, $objectData, $field, $salsifyField, $dbField); |
||
| 266 | $this->writeValue($object, $type, $dbField, $value, $sortColumn); |
||
| 267 | } |
||
| 268 | |||
| 269 | if (!$this->isMapperHashUpToDate($object, $salsifyRelations)) { |
||
| 270 | $this->updateMappingHash($object, $salsifyRelations); |
||
| 271 | } |
||
| 272 | |||
| 273 | $this->extend('beforeObjectWrite', $object); |
||
| 274 | if ($object->isChanged()) { |
||
| 275 | $object->write(); |
||
| 276 | $this->importCount++; |
||
| 277 | $this->extend('afterObjectWrite', $object, $wasWritten, $wasPublished); |
||
| 278 | } else { |
||
| 279 | ImportTask::output("$class $firstUniqueKey $firstUniqueValue was not changed."); |
||
| 280 | } |
||
| 281 | return $object; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param DataObject $object |
||
| 286 | * @param array $data |
||
| 287 | * @param string $firstUniqueKey |
||
| 288 | * @param string $firstUniqueValue |
||
| 289 | * @param bool $salsifyRelations |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | private function objectUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue, $salsifyRelations = false) |
||
| 293 | { |
||
| 294 | if ($this->config()->get('skipUpToDate') == false) { |
||
| 295 | return false; |
||
| 296 | } |
||
| 297 | |||
| 298 | if (!$this->isMapperHashUpToDate($object, $salsifyRelations)) { |
||
| 299 | ImportTask::output("Forcing update for $firstUniqueKey $firstUniqueValue. Mappings changed."); |
||
| 300 | return false; |
||
| 301 | } |
||
| 302 | |||
| 303 | if ($salsifyRelations == false) { |
||
| 304 | if ($this->objectDataUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue)) { |
||
| 305 | ImportTask::output("Skipping $firstUniqueKey $firstUniqueValue. It is up to Date."); |
||
| 306 | return true; |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | if ($this->objectRelationsUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue)) { |
||
| 310 | ImportTask::output("Skipping $firstUniqueKey $firstUniqueValue relations. It is up to Date."); |
||
| 311 | return true; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | return false; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param DataObject $object |
||
| 320 | * @param array $data |
||
| 321 | * @param string $firstUniqueKey |
||
| 322 | * @param string $firstUniqueValue |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function objectDataUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue) |
||
| 326 | { |
||
| 327 | // assume not up to date if field does not exist on object |
||
| 328 | if (!$object->hasField('SalsifyUpdatedAt')) { |
||
| 329 | return false; |
||
| 330 | } |
||
| 331 | |||
| 332 | if ($data['salsify:updated_at'] != $object->getField('SalsifyUpdatedAt')) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | |||
| 336 | return true; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param DataObject $object |
||
| 341 | * @param array $data |
||
| 342 | * @param string $firstUniqueKey |
||
| 343 | * @param string $firstUniqueValue |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function objectRelationsUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue) |
||
| 347 | { |
||
| 348 | // assume not up to date if field does not exist on object |
||
| 349 | if (!$object->hasField('SalsifyRelationsUpdatedAt')) { |
||
| 350 | return false; |
||
| 351 | } |
||
| 352 | |||
| 353 | // relations were never updated, so its up to date |
||
| 354 | if (!isset($data['salsify:relations_updated_at'])) { |
||
| 355 | return true; |
||
| 356 | } |
||
| 357 | |||
| 358 | if ($data['salsify:relations_updated_at'] != $object->getField('SalsifyRelationsUpdatedAt')) { |
||
| 359 | return false; |
||
| 360 | } |
||
| 361 | |||
| 362 | return true; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param array $salsifyField |
||
| 367 | * @param array $data |
||
| 368 | * |
||
| 369 | * @return string|false |
||
| 370 | */ |
||
| 371 | private function getField($salsifyField, $data) |
||
| 372 | { |
||
| 373 | if (!is_array($salsifyField)) { |
||
| 374 | return array_key_exists($salsifyField, $data) ? $salsifyField : false; |
||
| 375 | } |
||
| 376 | |||
| 377 | $hasSalsifyField = array_key_exists('salsifyField', $salsifyField); |
||
| 378 | $isLiteralField = ( |
||
| 379 | $this->getFieldType($salsifyField) === 'Literal' && |
||
| 380 | array_key_exists('value', $salsifyField) |
||
| 381 | ); |
||
| 382 | $isSalsifyRelationField = ( |
||
| 383 | $this->getFieldType($salsifyField) === 'SalsifyRelation' && |
||
| 384 | $hasSalsifyField |
||
| 385 | ); |
||
| 386 | |||
| 387 | if ($isLiteralField) { |
||
| 388 | return $salsifyField['value']; |
||
| 389 | } |
||
| 390 | |||
| 391 | if ($isSalsifyRelationField) { |
||
| 392 | return $salsifyField['salsifyField']; |
||
| 393 | } |
||
| 394 | |||
| 395 | if (!$hasSalsifyField) { |
||
| 396 | return false; |
||
| 397 | } |
||
| 398 | |||
| 399 | if (array_key_exists($salsifyField['salsifyField'], $data)) { |
||
| 400 | return $salsifyField['salsifyField']; |
||
| 401 | } elseif (array_key_exists('fallback', $salsifyField)) { |
||
| 402 | // make fallback an array |
||
| 403 | if (!is_array($salsifyField['fallback'])) { |
||
| 404 | $salsifyField['fallback'] = [$salsifyField['fallback']]; |
||
| 405 | } |
||
| 406 | |||
| 407 | foreach ($this->yieldSingle($salsifyField['fallback']) as $fallback) { |
||
| 408 | if (array_key_exists($fallback, $data)) { |
||
| 409 | return $fallback; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | } elseif (array_key_exists('modification', $salsifyField)) { |
||
| 413 | return $salsifyField['salsifyField']; |
||
| 414 | } |
||
| 415 | |||
| 416 | return false; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $class |
||
| 421 | * @param array $mappings |
||
| 422 | * @param array $data |
||
| 423 | * |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | private function hasValidUniqueFilter($class, $mappings, $data) |
||
| 427 | { |
||
| 428 | return (bool) count(array_filter($this->getUniqueFilter($class, $mappings, $data))); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $class |
||
| 433 | * @param array $mappings |
||
| 434 | * @param array $data |
||
| 435 | * |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | private function getUniqueFilter($class, $mappings, $data) |
||
| 439 | { |
||
| 440 | $uniqueFields = $this->uniqueFields($class, $mappings); |
||
| 441 | // creates a filter |
||
| 442 | $filter = []; |
||
| 443 | foreach ($this->yieldKeyVal($uniqueFields) as $dbField => $salsifyField) { |
||
| 444 | $modifiedData = $data; |
||
| 445 | $fieldMapping = $mappings[$dbField]; |
||
| 446 | $fieldType = $this->getFieldType($salsifyField); |
||
| 447 | $modifiedData = $this->handleModification($fieldType, $class, $dbField, $fieldMapping, $modifiedData); |
||
| 448 | |||
| 449 | // adds unique fields to filter |
||
| 450 | if (array_key_exists($salsifyField, $modifiedData)) { |
||
| 451 | $filter[$dbField] = $modifiedData[$salsifyField]; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | return $filter; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param string $class |
||
| 460 | * @param array $mappings |
||
| 461 | * @param array $data |
||
| 462 | * |
||
| 463 | * @return \SilverStripe\ORM\DataObject |
||
| 464 | */ |
||
| 465 | private function findObjectByUnique($class, $mappings, $data) |
||
| 466 | { |
||
| 467 | if ($obj = $this->findBySalsifyID($class, $mappings, $data)) { |
||
| 468 | return $obj; |
||
| 469 | } |
||
| 470 | |||
| 471 | if (!$this->hasValidUniqueFilter($class, $mappings, $data)) { |
||
| 472 | return null; |
||
| 473 | } |
||
| 474 | |||
| 475 | $filter = $this->getUniqueFilter($class, $mappings, $data); |
||
| 476 | return DataObject::get($class)->filter($filter)->first(); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param string $class |
||
| 481 | * @param array $mappings |
||
| 482 | * @param array $data |
||
| 483 | * |
||
| 484 | * @return \SilverStripe\ORM\DataObject|bool |
||
| 485 | */ |
||
| 486 | private function findBySalsifyID($class, $mappings, $data) |
||
| 487 | { |
||
| 488 | /** @var DataObject $genericObject */ |
||
| 489 | $genericObject = Injector::inst()->get($class); |
||
| 490 | if ( |
||
| 491 | !$genericObject->hasExtension(SalsifyIDExtension::class) && |
||
| 492 | !$genericObject->hasField('SalsifyID') |
||
| 493 | ) { |
||
| 494 | return false; |
||
| 495 | } |
||
| 496 | |||
| 497 | $obj = DataObject::get($class)->filter([ |
||
| 498 | 'SalsifyID' => $data['salsify:id'], |
||
| 499 | ])->first(); |
||
| 500 | if ($obj) { |
||
| 501 | return $obj; |
||
| 502 | } |
||
| 503 | |||
| 504 | return false; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Gets a list of all the unique field keys |
||
| 509 | * |
||
| 510 | * @param string class |
||
| 511 | * @param array $mappings |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | private function uniqueFields($class, $mappings) |
||
| 515 | { |
||
| 516 | // cached after first map |
||
| 517 | if (array_key_exists($class, $this->currentUniqueFields) && !empty($this->currentUniqueFields[$class])) { |
||
| 518 | return $this->currentUniqueFields[$class]; |
||
| 519 | } |
||
| 520 | |||
| 521 | $uniqueFields = []; |
||
| 522 | foreach ($this->yieldKeyVal($mappings) as $dbField => $salsifyField) { |
||
| 523 | if (!is_array($salsifyField)) { |
||
| 524 | continue; |
||
| 525 | } |
||
| 526 | |||
| 527 | if ( |
||
| 528 | !array_key_exists('unique', $salsifyField) || |
||
| 529 | !array_key_exists('salsifyField', $salsifyField) |
||
| 530 | ) { |
||
| 531 | continue; |
||
| 532 | } |
||
| 533 | |||
| 534 | if ($salsifyField['unique'] !== true) { |
||
| 535 | continue; |
||
| 536 | } |
||
| 537 | |||
| 538 | $uniqueFields[$dbField] = $salsifyField['salsifyField']; |
||
| 539 | } |
||
| 540 | |||
| 541 | $this->currentUniqueFields[$class] = $uniqueFields; |
||
| 542 | return $uniqueFields; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param array|string $salsifyField |
||
| 547 | * @return bool|mixed |
||
| 548 | */ |
||
| 549 | private function getSortColumn($salsifyField) |
||
| 550 | { |
||
| 551 | if (!is_array($salsifyField)) { |
||
| 552 | return false; |
||
| 553 | } |
||
| 554 | |||
| 555 | if (array_key_exists('sortColumn', $salsifyField)) { |
||
| 556 | return $salsifyField['sortColumn']; |
||
| 557 | } |
||
| 558 | |||
| 559 | return false; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | private function mappingHasSalsifyRelation() |
||
| 566 | { |
||
| 567 | foreach ($this->yieldKeyVal($this->config()->get('mapping')) as $class => $mappings) { |
||
| 568 | if ($this->classConfigHasSalsifyRelation($mappings)) { |
||
| 569 | return true; |
||
| 570 | } |
||
| 571 | } |
||
| 572 | return false; |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param array $classConfig |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | private function classConfigHasSalsifyRelation($classConfig) |
||
| 580 | { |
||
| 581 | foreach ($this->yieldKeyVal($classConfig) as $field => $config) { |
||
| 582 | if (!is_array($config)) { |
||
| 583 | continue; |
||
| 584 | } |
||
| 585 | |||
| 586 | if (!array_key_exists('salsifyField', $config)) { |
||
| 587 | continue; |
||
| 588 | } |
||
| 589 | |||
| 590 | if (!array_key_exists('type', $config)) { |
||
| 591 | continue; |
||
| 592 | } |
||
| 593 | |||
| 594 | if (in_array($config['type'], $this->getFieldsRequiringSalsifyObjects())) { |
||
| 595 | return true; |
||
| 596 | } |
||
| 597 | } |
||
| 598 | return false; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | private function getFieldsRequiringSalsifyObjects() |
||
| 605 | { |
||
| 606 | $fieldTypes = $this->config()->get('field_types'); |
||
| 607 | $types = []; |
||
| 608 | foreach ($this->yieldKeyVal($this->config()->get('field_types')) as $field => $config) { |
||
| 609 | $type = [ |
||
| 610 | 'type' => $field, |
||
| 611 | 'config' => $config, |
||
| 612 | ]; |
||
| 613 | if ($this->typeRequiresSalsifyObjects($type)) { |
||
| 614 | $types[] = $field; |
||
| 615 | } |
||
| 616 | } |
||
| 617 | |||
| 618 | return $types; |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param array $type |
||
| 623 | * @return bool |
||
| 624 | */ |
||
| 625 | private function typeRequiresWrite($type) |
||
| 626 | { |
||
| 627 | $config = $type['config']; |
||
| 628 | |||
| 629 | if (array_key_exists('requiresWrite', $config)) { |
||
| 630 | return $config['requiresWrite']; |
||
| 631 | } |
||
| 632 | |||
| 633 | return false; |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param array $type |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | private function typeRequiresSalsifyObjects($type) |
||
| 641 | { |
||
| 642 | $config = $type['config']; |
||
| 643 | |||
| 644 | if (array_key_exists('requiresSalsifyObjects', $config)) { |
||
| 645 | return $config['requiresSalsifyObjects']; |
||
| 646 | } |
||
| 647 | |||
| 648 | return false; |
||
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @param array $type |
||
| 653 | * @return string|bool |
||
| 654 | */ |
||
| 655 | private function typeFallback($type) |
||
| 656 | { |
||
| 657 | $config = $type['config']; |
||
| 658 | |||
| 659 | if (array_key_exists('fallback', $config)) { |
||
| 660 | return $config['fallback']; |
||
| 661 | } |
||
| 662 | |||
| 663 | return false; |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param array $type |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | private function canModifyType($type) |
||
| 671 | { |
||
| 672 | $config = $type['config']; |
||
| 673 | |||
| 674 | if (array_key_exists('allowsModification', $config)) { |
||
| 675 | return $config['allowsModification']; |
||
| 676 | } |
||
| 677 | |||
| 678 | return true; |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param array $type |
||
| 683 | * @param string $class |
||
| 684 | * @param string $dbField |
||
| 685 | * @param array $config |
||
| 686 | * @param array $data |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | private function handleModification($type, $class, $dbField, $config, $data) |
||
| 690 | { |
||
| 691 | if (!is_array($config)) { |
||
| 692 | return $data; |
||
| 693 | } |
||
| 694 | |||
| 695 | if (!$this->canModifyType($type)) { |
||
| 696 | return $data; |
||
| 697 | } |
||
| 698 | |||
| 699 | if (array_key_exists('modification', $config)) { |
||
| 700 | $mod = $config['modification']; |
||
| 701 | if ($this->hasMethod($mod)) { |
||
| 702 | return $this->{$mod}($class, $dbField, $config, $data); |
||
| 703 | } |
||
| 704 | ImportTask::output("{$mod} is not a valid field modifier. skipping modification for field {$dbField}."); |
||
| 705 | } |
||
| 706 | return $data; |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @param string $class |
||
| 711 | * @param string $dbField |
||
| 712 | * @param array $config |
||
| 713 | * @param array $data |
||
| 714 | * @return boolean |
||
| 715 | */ |
||
| 716 | public function handleShouldSkip($class, $dbField, $config, $data) |
||
| 717 | { |
||
| 718 | if (!is_array($config)) { |
||
| 719 | return false; |
||
| 720 | } |
||
| 721 | |||
| 722 | if (array_key_exists('shouldSkip', $config)) { |
||
| 723 | $skipMethod = $config['shouldSkip']; |
||
| 724 | if ($this->hasMethod($skipMethod)) { |
||
| 725 | return $this->{$skipMethod}($class, $dbField, $config, $data); |
||
| 726 | } |
||
| 727 | ImportTask::output( |
||
| 728 | "{$skipMethod} is not a valid skip test method. Skipping skip test for field {$dbField}." |
||
| 729 | ); |
||
| 730 | } |
||
| 731 | return false; |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param string|array $field |
||
| 736 | * @return array |
||
| 737 | */ |
||
| 738 | public function getFieldType($field) |
||
| 739 | { |
||
| 740 | $fieldTypes = $this->config()->get('field_types'); |
||
| 741 | if (is_array($field) && array_key_exists('type', $field)) { |
||
| 742 | if (array_key_exists($field['type'], $fieldTypes)) { |
||
| 743 | return [ |
||
| 744 | 'type' => $field['type'], |
||
| 745 | 'config' => $fieldTypes[$field['type']], |
||
| 746 | ]; |
||
| 747 | } |
||
| 748 | } |
||
| 749 | // default to raw |
||
| 750 | return [ |
||
| 751 | 'type' => 'Raw', |
||
| 752 | 'config' => $fieldTypes['Raw'], |
||
| 753 | ]; |
||
| 754 | } |
||
| 755 | |||
| 756 | /** |
||
| 757 | * @param array $type |
||
| 758 | * @param string|DataObject $class |
||
| 759 | * @param array $salsifyData |
||
| 760 | * @param string $salsifyField |
||
| 761 | * @param array $dbFieldConfig |
||
| 762 | * @param string $dbField |
||
| 763 | * |
||
| 764 | * @return mixed |
||
| 765 | */ |
||
| 766 | private function handleType($type, $class, $salsifyData, $salsifyField, $dbFieldConfig, $dbField) |
||
| 767 | { |
||
| 768 | $typeName = $type['type']; |
||
| 769 | $typeConfig = $type['config']; |
||
| 770 | if ($this->hasMethod("handle{$typeName}Type")) { |
||
| 771 | return $this->{"handle{$typeName}Type"}($class, $salsifyData, $salsifyField, $dbFieldConfig, $dbField); |
||
| 772 | } |
||
| 773 | |||
| 774 | if (array_key_exists('fallback', $typeConfig)) { |
||
| 775 | $fallback = $typeConfig['fallback']; |
||
| 776 | if ($this->hasMethod("handle{$fallback}Type")) { |
||
| 777 | return $this->{"handle{$fallback}Type"}($class, $salsifyData, $salsifyField, $dbFieldConfig, $dbField); |
||
| 778 | } |
||
| 779 | } |
||
| 780 | |||
| 781 | ImportTask::output("{$typeName} is not a valid type. skipping field {$dbField}."); |
||
| 782 | return ''; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @param DataObject $object |
||
| 787 | * @param array $type |
||
| 788 | * @param string $dbField |
||
| 789 | * @param mixed $value |
||
| 790 | * @param string|bool $sortColumn |
||
| 791 | * |
||
| 792 | * @throws \Exception |
||
| 793 | */ |
||
| 794 | private function writeValue($object, $type, $dbField, $value, $sortColumn) |
||
| 795 | { |
||
| 796 | $isManyRelation = array_key_exists($dbField, $object->config()->get('has_many')) || |
||
| 797 | array_key_exists($dbField, $object->config()->get('many_many')) || |
||
| 798 | array_key_exists($dbField, $object->config()->get('belongs_many_many')); |
||
| 799 | |||
| 800 | $isSingleRelation = array_key_exists(rtrim($dbField, 'ID'), $object->config()->get('has_one')); |
||
| 801 | |||
| 802 | // write the object so relations can be written |
||
| 803 | if ($this->typeRequiresWrite($type) && !$object->exists()) { |
||
| 804 | $this->extend('beforeObjectWrite', $object); |
||
| 805 | $object->write(); |
||
| 806 | } |
||
| 807 | |||
| 808 | if (!$isManyRelation) { |
||
| 809 | if (!$isSingleRelation || ($isSingleRelation && $value !== false)) { |
||
| 810 | $object->$dbField = $value; |
||
| 811 | } |
||
| 812 | return; |
||
| 813 | } |
||
| 814 | |||
| 815 | // change to an array and filter out empty values |
||
| 816 | if (!is_array($value)) { |
||
| 817 | $value = [$value]; |
||
| 818 | } |
||
| 819 | $value = array_filter($value); |
||
| 820 | |||
| 821 | // don't try to write an empty set |
||
| 822 | if (!count($value)) { |
||
| 823 | return; |
||
| 824 | } |
||
| 825 | |||
| 826 | $this->removeUnrelated($object, $dbField, $value); |
||
| 827 | $this->writeManyRelation($object, $dbField, $value, $sortColumn); |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param DataObject $object |
||
| 832 | * @param string $dbField |
||
| 833 | * @param array $value |
||
| 834 | * @param string|bool $sortColumn |
||
| 835 | * |
||
| 836 | * @throws \Exception |
||
| 837 | */ |
||
| 838 | private function writeManyRelation($object, $dbField, $value, $sortColumn) |
||
| 839 | { |
||
| 840 | /** @var DataList|HasManyList|ManyManyList $relation */ |
||
| 841 | $relation = $object->{$dbField}(); |
||
| 842 | |||
| 843 | if ($sortColumn && $relation instanceof ManyManyList) { |
||
| 844 | for ($i = 0; $i < count($value); $i++) { |
||
| 845 | $relation->add($value[$i], [$sortColumn => $i]); |
||
| 846 | } |
||
| 847 | return; |
||
| 848 | } |
||
| 849 | |||
| 850 | // HasManyList, so it exists on the value |
||
| 851 | if ($sortColumn) { |
||
| 852 | for ($i = 0; $i < count($value); $i++) { |
||
| 853 | $value[$i]->{$sortColumn} = $i; |
||
| 854 | $relation->add($value[$i]); |
||
| 855 | } |
||
| 856 | return; |
||
| 857 | } |
||
| 858 | |||
| 859 | $relation->addMany($value); |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Removes unrelated objects in the relation that were previously related |
||
| 864 | * @param DataObject $object |
||
| 865 | * @param string $dbField |
||
| 866 | * @param array $value |
||
| 867 | */ |
||
| 868 | private function removeUnrelated($object, $dbField, $value) |
||
| 869 | { |
||
| 870 | $ids = []; |
||
| 871 | foreach ($value as $v) { |
||
| 872 | $ids[] = $v->ID; |
||
| 873 | } |
||
| 874 | |||
| 875 | /** @var DataList $relation */ |
||
| 876 | $relation = $object->{$dbField}(); |
||
| 877 | // remove all unrelated - removeAll had an odd side effect (relations only got added back half the time) |
||
| 878 | if (!empty($ids)) { |
||
| 879 | $relation->removeMany( |
||
| 880 | $relation->exclude([ |
||
| 881 | 'ID' => $ids, |
||
| 882 | ])->column('ID') |
||
| 883 | ); |
||
| 884 | } |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * @param string|array $salsifyField |
||
| 889 | * @throws Exception |
||
| 890 | */ |
||
| 891 | private function clearValue($object, $dbField, $salsifyField) |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @return JsonMachine |
||
| 912 | */ |
||
| 913 | public function getProductStream() |
||
| 914 | { |
||
| 915 | return $this->productStream; |
||
| 916 | } |
||
| 917 | |||
| 918 | /** |
||
| 919 | * @return JsonMachine |
||
| 920 | */ |
||
| 921 | public function getAssetStream() |
||
| 922 | { |
||
| 923 | return $this->assetStream; |
||
| 924 | } |
||
| 925 | |||
| 926 | /** |
||
| 927 | * @return bool |
||
| 928 | */ |
||
| 929 | public function hasFile() |
||
| 932 | } |
||
| 933 | |||
| 934 | /** |
||
| 935 | * @param DataObject|SalsifyIDExtension $object |
||
| 936 | * @return bool |
||
| 937 | */ |
||
| 938 | private function isMapperHashUpToDate($object, $relations) |
||
| 958 | } |
||
| 959 | } |
||
| 960 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths