| Total Complexity | 59 |
| Total Lines | 369 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 14 | class Mapper extends Service |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | private $file = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var JsonMachine |
||
| 24 | */ |
||
| 25 | private $productStream; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var JsonMachine |
||
| 29 | */ |
||
| 30 | private $assetStream; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $currentUniqueFields; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $importCount = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Mapper constructor. |
||
| 44 | * @param string $importerKey |
||
| 45 | * @param $file |
||
| 46 | * @throws \Exception |
||
| 47 | */ |
||
| 48 | public function __construct($importerKey, $file = null) |
||
| 49 | { |
||
| 50 | parent::__construct($importerKey); |
||
| 51 | if (!$this->config()->get('mapping')) { |
||
| 52 | throw new Exception('A Mapper needs a mapping'); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($file !== null) { |
||
| 56 | $this->file = $file; |
||
| 57 | $this->productStream = JsonMachine::fromFile($file, '/4/products'); |
||
| 58 | $this->resetAssetStream(); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * |
||
| 64 | */ |
||
| 65 | public function resetAssetStream() |
||
| 66 | { |
||
| 67 | $this->assetStream = JsonMachine::fromFile($this->file, '/3/digital_assets'); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Maps the data |
||
| 72 | * @throws \Exception |
||
| 73 | */ |
||
| 74 | public function map() |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string|DataObject $class |
||
| 87 | * @param array $mappings The mapping for a specific class |
||
| 88 | * @param array $data |
||
| 89 | * |
||
| 90 | * @return DataObject |
||
| 91 | * @throws \Exception |
||
| 92 | */ |
||
| 93 | public function mapToObject($class, $mappings, $data) |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param DataObject $object |
||
| 142 | * @param array $data |
||
| 143 | * @param string $firstUniqueKey |
||
| 144 | * @param string $firstUniqueValue |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | private function objectUpToDate($object, $data, $firstUniqueKey, $firstUniqueValue) |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $salsifyField |
||
| 162 | * @param array $data |
||
| 163 | * |
||
| 164 | * @return string|false |
||
| 165 | */ |
||
| 166 | private function getField($salsifyField, $data) |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $class |
||
| 206 | * @param array $mappings |
||
| 207 | * @param array $data |
||
| 208 | * |
||
| 209 | * @return \SilverStripe\ORM\DataObject |
||
| 210 | */ |
||
| 211 | private function findObjectByUnique($class, $mappings, $data) |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Gets a list of all the unique field keys |
||
| 231 | * |
||
| 232 | * @param array $mappings |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | private function uniqueFields($mappings) |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $dbField |
||
| 268 | * @param array $config |
||
| 269 | * @param array $data |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | private function handleModification($dbField, $config, $data) |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $dbField |
||
| 286 | * @param array $config |
||
| 287 | * @param array $data |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | private function handleShouldSkip($dbField, $config, $data) |
||
| 291 | { |
||
| 292 | if (array_key_exists('shouldSkip', $config)) { |
||
| 293 | $skipMethod = $config['shouldSkip']; |
||
| 294 | if ($this->hasMethod($skipMethod)) { |
||
| 295 | return $this->{$skipMethod}($dbField, $config, $data); |
||
| 296 | } |
||
| 297 | ImportTask::output( |
||
| 298 | "{$skipMethod} is not a valid skip test method. Skipping skip test for field {$dbField}." |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string|array $field |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getFieldType($field) |
||
| 309 | { |
||
| 310 | $fieldTypes = $this->config()->get('field_types'); |
||
| 311 | if (is_array($field) && array_key_exists('type', $field)) { |
||
| 312 | if (in_array($field['type'], $fieldTypes)) { |
||
| 313 | return $field['type']; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | // default to raw |
||
| 317 | return 'Raw'; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param int $type |
||
| 322 | * @param array $salsifyData |
||
| 323 | * @param string $salsifyField |
||
| 324 | * @param array $dbFieldConfig |
||
| 325 | * @param string $dbField |
||
| 326 | * @param string $class |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | private function handleType($type, $salsifyData, $salsifyField, $dbFieldConfig, $dbField, $class) |
||
| 331 | { |
||
| 332 | if ($this->hasMethod("handle{$type}Type")) { |
||
| 333 | return $this->{"handle{$type}Type"}($salsifyData, $salsifyField, $dbFieldConfig, $dbField, $class); |
||
| 334 | } |
||
| 335 | ImportTask::output("{$type} is not a valid type. skipping field {$dbField}."); |
||
| 336 | return ''; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param DataObject $object |
||
| 341 | * @param string $dbField |
||
| 342 | * @param mixed $value |
||
| 343 | * |
||
| 344 | * @throws \Exception |
||
| 345 | */ |
||
| 346 | private function writeValue($object, $dbField, $value) |
||
| 347 | { |
||
| 348 | $isManyRelation = array_key_exists($dbField, $object->config()->get('has_many')) || |
||
| 349 | array_key_exists($dbField, $object->config()->get('many_many')) || |
||
| 350 | array_key_exists($dbField, $object->config()->get('belongs_many_many')); |
||
| 351 | |||
| 352 | if (!$isManyRelation) { |
||
| 353 | $object->$dbField = $value; |
||
| 354 | return; |
||
| 355 | } |
||
| 356 | |||
| 357 | if (!$object->exists()) { |
||
| 358 | $object->write(); |
||
| 359 | } |
||
| 360 | |||
| 361 | if (is_array($value)) { |
||
| 362 | $object->{$dbField}()->addMany($value); |
||
| 363 | return; |
||
| 364 | } |
||
| 365 | |||
| 366 | $object->{$dbField}()->add($value); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return \JsonMachine\JsonMachine |
||
| 371 | */ |
||
| 372 | public function getAssetStream() |
||
| 373 | { |
||
| 374 | return $this->assetStream; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function hasFile() |
||
| 383 | } |
||
| 384 | } |
||
| 385 |