| Total Complexity | 60 |
| Total Lines | 481 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataImporter 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 DataImporter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DataImporter |
||
| 19 | { |
||
| 20 | /** @var string $exportName */ |
||
| 21 | private $exportName; |
||
|
|
|||
| 22 | /** @var Collection $data */ |
||
| 23 | private $data; |
||
| 24 | /** @var Export */ |
||
| 25 | private $export; |
||
| 26 | /** @var Collection $rowMap */ |
||
| 27 | private $rowMap; |
||
| 28 | /** @var Collection $rows */ |
||
| 29 | private $rows; |
||
| 30 | /** @var Collection $addRows */ |
||
| 31 | private $addRows; |
||
| 32 | /** @var Collection $updateRows */ |
||
| 33 | private $updateRows; |
||
| 34 | /** @var Collection $updateRows */ |
||
| 35 | private $deleteRows; |
||
| 36 | /** @var Collection $errors */ |
||
| 37 | private $errors; |
||
| 38 | /** @var DBCollection $statements */ |
||
| 39 | private $statements; |
||
| 40 | private $prefixes = []; |
||
| 41 | private $stats = []; |
||
| 42 | /** @var Collection $columnProfileMap */ |
||
| 43 | private $columnProfileMap; |
||
| 44 | /** @var string */ |
||
| 45 | private $currentColumnName; |
||
| 46 | /** @var string */ |
||
| 47 | private $currentRowName; |
||
| 48 | |||
| 49 | public function __construct(Collection $data, Export $export = null) |
||
| 50 | { |
||
| 51 | $this->data = $data; |
||
| 52 | $columnHeaders = collect($data[0]); |
||
| 53 | $this->rows = $this->getDataForImport(); |
||
| 54 | |||
| 55 | $this->export = $export; |
||
| 56 | if ($export) { |
||
| 57 | $this->rowMap = self::getRowMap($export->map); |
||
| 58 | try { |
||
| 59 | $this->columnProfileMap = self::getColumnProfileMap($export, $columnHeaders); |
||
| 60 | } |
||
| 61 | //these are all fatal errors |
||
| 62 | catch (DuplicateAttributesException $e) { |
||
| 63 | $this->errors = collect(['fatal' => $e->getMessage()]); |
||
| 64 | $this->setStats(); |
||
| 65 | |||
| 66 | return; |
||
| 67 | } catch (MissingRequiredAttributeException $e) { |
||
| 68 | $this->errors = collect(['fatal' => $e->getMessage()]); |
||
| 69 | $this->setStats(); |
||
| 70 | |||
| 71 | return; |
||
| 72 | } catch (UnknownAttributeException $e) { |
||
| 73 | $this->errors = collect(['fatal' => $e->getMessage()]); |
||
| 74 | $this->setStats(); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | $this->errors = new Collection(); |
||
| 79 | $this->addRows = $this->getAddRows(); //only gets data rows with no row_id |
||
| 80 | $this->updateRows = $this->getUpdateRows(); //gets data rows with matching map |
||
| 81 | $this->deleteRows = $this->getDeleteRows(); //gets map rows with no matching row |
||
| 82 | if ($export->vocabulary_id) { |
||
| 83 | $this->prefixes = $export->vocabulary->prefixes; |
||
| 84 | $this->statements = $this->getVocabularyStatements(); |
||
| 85 | } else { |
||
| 86 | $this->prefixes = $export->elementset->prefixes; |
||
| 87 | $this->statements = $this->getElementSetStatements(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->setStats(); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function setStats() |
||
| 95 | { |
||
| 96 | $this->stats['deleted'] = $this->deleteRows === null ? 0 : $this->deleteRows->count(); |
||
| 97 | $this->stats['updated'] = $this->updateRows === null ? 0 : $this->updateRows->count(); |
||
| 98 | $this->stats['added'] = $this->addRows === null ? 0 : $this->addRows->count(); |
||
| 99 | $this->stats['errors'] = $this->errors === null ? 0 : $this->errors->count(); |
||
| 100 | } |
||
| 101 | /** |
||
| 102 | * return a collection of rows that have no reg_id. |
||
| 103 | * |
||
| 104 | * @return Collection |
||
| 105 | */ |
||
| 106 | public function getAddRows(): Collection |
||
| 107 | { |
||
| 108 | //only keep rows that have no reg_id |
||
| 109 | return collect($this->rows->filter(function ($row, $key) { |
||
| 110 | return empty($row['reg_id']); |
||
| 111 | }))->values(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return Collection |
||
| 116 | * @internal param Collection $map |
||
| 117 | * @internal param array $rowMap |
||
| 118 | */ |
||
| 119 | public function getChangeset(): Collection |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns an associative array of data based on the data supplied for import. |
||
| 206 | * |
||
| 207 | * @return Collection |
||
| 208 | */ |
||
| 209 | public function getDataForImport(): Collection |
||
| 210 | { |
||
| 211 | $h = $this->data->first(); |
||
| 212 | |||
| 213 | return $this->data->slice(1)->transform(function ($item, $key) use ($h) { |
||
| 214 | return collect($item)->mapWithKeys(function ($item, $key) use ($h) { |
||
| 215 | return [$h[$key] => $item]; |
||
| 216 | }); |
||
| 217 | }); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return Collection |
||
| 222 | */ |
||
| 223 | public function getDeleteRows(): Collection |
||
| 224 | { |
||
| 225 | //only keep rows that are in the rowmap but are missing from the supplied data |
||
| 226 | $updateRows = $this->updateRows; |
||
| 227 | |||
| 228 | return collect($this->rowMap->reject(function ($row, $key) use ($updateRows) { |
||
| 229 | return isset($updateRows[$key]); |
||
| 230 | })); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param Collection $map |
||
| 235 | * |
||
| 236 | * @return Collection |
||
| 237 | */ |
||
| 238 | public static function getHeaderFromMap(Collection $map): Collection |
||
| 239 | { |
||
| 240 | return collect($map->first()); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getStats(): Collection |
||
| 244 | { |
||
| 245 | $errorCount = 0; |
||
| 246 | if ($this->errors !== null) { |
||
| 247 | foreach ($this->errors as $error) { |
||
| 248 | $errorCount += \count($error); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | $this->stats['errors'] = $errorCount; |
||
| 253 | |||
| 254 | return collect($this->stats); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Collection $map |
||
| 259 | * |
||
| 260 | * @return Collection |
||
| 261 | */ |
||
| 262 | public static function getRowMap(Collection $map): Collection |
||
| 263 | { |
||
| 264 | $p = self::getHeaderFromMap($map); |
||
| 265 | |||
| 266 | return $map->slice(1)->transform(function ($item, $key) use ($p) { |
||
| 267 | return collect($item)->mapWithKeys(function ($item, $key) use ($p) { |
||
| 268 | return [$p[$key]['label'] => $item]; |
||
| 269 | }); |
||
| 270 | }); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Export $export |
||
| 275 | * @param Collection $columnHeaders |
||
| 276 | * |
||
| 277 | * @return Collection |
||
| 278 | * @throws DuplicateAttributesException |
||
| 279 | * @throws MissingRequiredAttributeException |
||
| 280 | * @throws UnknownAttributeException |
||
| 281 | */ |
||
| 282 | public static function getColumnProfileMap(Export $export, Collection $columnHeaders): Collection |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return Collection |
||
| 347 | */ |
||
| 348 | public function getUpdateRows(): Collection |
||
| 349 | { |
||
| 350 | //only keep rows that have a non-empty reg_id |
||
| 351 | return $this->rows->reject(function ($row) { |
||
| 352 | return empty($row['reg_id']); |
||
| 353 | })->keyBy('reg_id'); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return Collection |
||
| 358 | */ |
||
| 359 | public function getErrors(): Collection |
||
| 360 | { |
||
| 361 | return $this->errors; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return Collection |
||
| 366 | */ |
||
| 367 | public function getVocabularyStatements(): Collection |
||
| 368 | { |
||
| 369 | return Concept::whereVocabularyId($this->export->vocabulary_id)->with('statements.profile_property', 'status')->get()->keyBy('id')->map(function ($concept, $key) { |
||
| 370 | return $concept->statements->keyBy('id')->map(function ($property) { |
||
| 371 | return [ |
||
| 372 | 'old value' => $property->object, |
||
| 373 | 'updated_at' => $property->updated_at, |
||
| 374 | ]; |
||
| 375 | })->prepend([ |
||
| 376 | 'old value' => $concept->uri, |
||
| 377 | 'updated_at' => $concept->updated_at, |
||
| 378 | ], |
||
| 379 | '*uri')->prepend([ |
||
| 380 | 'old value' => $concept->status->display_name, |
||
| 381 | 'updated_at' => $concept->updated_at, |
||
| 382 | ], |
||
| 383 | '*status'); |
||
| 384 | }); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return Collection |
||
| 389 | */ |
||
| 390 | public function getElementSetStatements(): Collection |
||
| 391 | { |
||
| 392 | return Element::whereSchemaId($this->export->schema_id)->with('statements.profile_property', 'status')->get()->keyBy('id')->map(function ($element, $key) { |
||
| 393 | $status = $element->status->display_name; |
||
| 394 | $thingy = $element->statements->keyBy('id')->map(function ($property) { |
||
| 395 | return [ |
||
| 396 | 'old value' => $property->object, |
||
| 397 | 'updated_at' => $property->updated_at, |
||
| 398 | 'profile_uri' => $property->profile_property->uri, |
||
| 399 | 'is_resource' => (bool) $property->profile_property->is_object_prop, |
||
| 400 | ]; |
||
| 401 | })->map(function ($item) use ($status) { |
||
| 402 | if ($item['profile_uri'] === 'reg:status') { |
||
| 403 | $item['old value'] = $status; |
||
| 404 | } |
||
| 405 | if ($item['is_resource']) { |
||
| 406 | $item['old value'] = self::makeCurie($this->prefixes, $item['old value']); |
||
| 407 | } |
||
| 408 | |||
| 409 | return $item; |
||
| 410 | }); |
||
| 411 | |||
| 412 | return $thingy; |
||
| 413 | }); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param $message |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | protected static function makeErrorMessage($message): string |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param $value |
||
| 428 | * @param $column |
||
| 429 | * @param $row |
||
| 430 | * @param $level |
||
| 431 | */ |
||
| 432 | private function logRowError($value, $column, $row, $level): void |
||
| 433 | { |
||
| 434 | //if this is the first error, initialize the row errors |
||
| 435 | if (! $this->errors->get('row')) { |
||
| 436 | $this->errors->put('row', collect()); |
||
| 437 | } |
||
| 438 | |||
| 439 | $this->errors->get('row')->push(collect([$row, $column, $value, $level])); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param array $prefixes |
||
| 444 | * @param string $uri |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | private function makeFqn($prefixes, $uri): string |
||
| 449 | { |
||
| 450 | $result = $uri; |
||
| 451 | foreach ($prefixes as $prefix => $fullUri) { |
||
| 452 | $result = preg_replace('#' . $prefix . ':#uis', $fullUri, $uri); |
||
| 453 | if ($result !== $uri) { |
||
| 454 | break; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | if ($uri === $result && strpos($uri, ':') && ! strpos($uri, '://')) { |
||
| 458 | //we have an unregistered prefix |
||
| 459 | $prefix = str_before($uri, ':'); |
||
| 460 | $this->logRowError(self::makeErrorMessage("'$prefix' is an unregistered prefix and cannot be expanded to form a full URI"), $this->currentColumnName, $this->currentRowName, 'warning'); |
||
| 461 | } |
||
| 462 | |||
| 463 | return $result; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param array $prefixes |
||
| 468 | * @param string $uri |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | private static function makeCurie($prefixes, $uri): string |
||
| 473 | { |
||
| 474 | $result = $uri; |
||
| 475 | foreach ($prefixes as $prefix => $fullUri) { |
||
| 476 | $result = preg_replace('!' . $fullUri . '!uis', $prefix . ':', $uri); |
||
| 477 | if ($result !== $uri) { |
||
| 478 | break; |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | return $result; |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param $value |
||
| 487 | * @param $column |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | private function validateRequired($value, $column): string |
||
| 499 | } |
||
| 500 | } |
||
| 501 |