| Total Complexity | 59 |
| Total Lines | 474 |
| 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 | |||
| 65 | return; |
||
| 66 | } catch (MissingRequiredAttributeException $e) { |
||
| 67 | $this->errors = collect(['fatal' => $e->getMessage()]); |
||
| 68 | |||
| 69 | return; |
||
| 70 | } catch (UnknownAttributeException $e) { |
||
| 71 | $this->errors = collect(['fatal' => $e->getMessage()]); |
||
| 72 | |||
| 73 | return; |
||
| 74 | } |
||
| 75 | $this->errors = new Collection(); |
||
| 76 | $this->addRows = $this->getAddRows(); //only gets data rows with no row_id |
||
| 77 | $this->updateRows = $this->getUpdateRows(); //gets data rows with matching map |
||
| 78 | $this->deleteRows = $this->getDeleteRows(); //gets map rows with no matching row |
||
| 79 | if ($export->vocabulary_id) { |
||
| 80 | $this->prefixes = $export->vocabulary->prefixes; |
||
| 81 | $this->statements = $this->getVocabularyStatements(); |
||
| 82 | } else { |
||
| 83 | $this->prefixes = $export->elementset->prefixes; |
||
| 84 | $this->statements = $this->getElementSetStatements(); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->stats['deleted'] = $this->deleteRows === null ? 0 : $this->deleteRows->count(); |
||
| 89 | $this->stats['updated'] = $this->updateRows === null ? 0 : $this->updateRows->count(); |
||
| 90 | $this->stats['added'] = $this->addRows === null ? 0 : $this->addRows->count(); |
||
| 91 | $this->stats['errors'] = $this->errors === null ? 0 : $this->errors->count(); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * return a collection of rows that have no reg_id. |
||
| 96 | * |
||
| 97 | * @return Collection |
||
| 98 | */ |
||
| 99 | public function getAddRows(): Collection |
||
| 100 | { |
||
| 101 | //only keep rows that have no reg_id |
||
| 102 | return collect($this->rows->filter(function ($row, $key) { |
||
|
1 ignored issue
–
show
|
|||
| 103 | return empty($row['reg_id']); |
||
| 104 | }))->values(); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return Collection |
||
| 109 | * @internal param Collection $map |
||
| 110 | * @internal param array $rowMap |
||
| 111 | */ |
||
| 112 | public function getChangeset(): Collection |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns an associative array of data based on the data supplied for import. |
||
| 199 | * |
||
| 200 | * @return Collection |
||
| 201 | */ |
||
| 202 | public function getDataForImport(): Collection |
||
| 203 | { |
||
| 204 | $h = $this->data->first(); |
||
| 205 | |||
| 206 | return $this->data->slice(1)->transform(function ($item, $key) use ($h) { |
||
|
1 ignored issue
–
show
|
|||
| 207 | return collect($item)->mapWithKeys(function ($item, $key) use ($h) { |
||
| 208 | return [$h[$key] => $item]; |
||
| 209 | }); |
||
| 210 | }); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return Collection |
||
| 215 | */ |
||
| 216 | public function getDeleteRows(): Collection |
||
| 217 | { |
||
| 218 | //only keep rows that are in the rowmap but are missing from the supplied data |
||
| 219 | $updateRows = $this->updateRows; |
||
| 220 | |||
| 221 | return collect($this->rowMap->reject(function ($row, $key) use ($updateRows) { |
||
| 222 | return isset($updateRows[$key]); |
||
| 223 | })); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Collection $map |
||
| 228 | * |
||
| 229 | * @return Collection |
||
| 230 | */ |
||
| 231 | public static function getHeaderFromMap(Collection $map): Collection |
||
| 232 | { |
||
| 233 | return collect($map->first()); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getStats(): Collection |
||
| 237 | { |
||
| 238 | $errorCount = 0; |
||
| 239 | if ($this->errors !== null) { |
||
| 240 | foreach ($this->errors as $error) { |
||
| 241 | $errorCount += \count($error); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | $this->stats['errors'] = $errorCount; |
||
| 246 | |||
| 247 | return collect($this->stats); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param Collection $map |
||
| 252 | * |
||
| 253 | * @return Collection |
||
| 254 | */ |
||
| 255 | public static function getRowMap(Collection $map): Collection |
||
| 256 | { |
||
| 257 | $p = self::getHeaderFromMap($map); |
||
| 258 | |||
| 259 | return $map->slice(1)->transform(function ($item, $key) use ($p) { |
||
|
1 ignored issue
–
show
|
|||
| 260 | return collect($item)->mapWithKeys(function ($item, $key) use ($p) { |
||
| 261 | return [$p[$key]['label'] => $item]; |
||
| 262 | }); |
||
| 263 | }); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param Export $export |
||
| 268 | * @param Collection $columnHeaders |
||
| 269 | * |
||
| 270 | * @return Collection |
||
| 271 | * @throws DuplicateAttributesException |
||
| 272 | * @throws MissingRequiredAttributeException |
||
| 273 | * @throws UnknownAttributeException |
||
| 274 | */ |
||
| 275 | public static function getColumnProfileMap(Export $export, Collection $columnHeaders): Collection |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return Collection |
||
| 340 | */ |
||
| 341 | public function getUpdateRows(): Collection |
||
| 342 | { |
||
| 343 | //only keep rows that have a non-empty reg_id |
||
| 344 | return $this->rows->reject(function ($row) { |
||
| 345 | return empty($row['reg_id']); |
||
| 346 | })->keyBy('reg_id'); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return Collection |
||
| 351 | */ |
||
| 352 | public function getErrors(): Collection |
||
| 353 | { |
||
| 354 | return $this->errors; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return Collection |
||
| 359 | */ |
||
| 360 | public function getVocabularyStatements(): Collection |
||
| 361 | { |
||
| 362 | return Concept::whereVocabularyId($this->export->vocabulary_id)->with('statements.profile_property', 'status')->get()->keyBy('id')->map(function ($concept, $key) { |
||
|
1 ignored issue
–
show
|
|||
| 363 | return $concept->statements->keyBy('id')->map(function ($property) { |
||
| 364 | return [ |
||
| 365 | 'old value' => $property->object, |
||
| 366 | 'updated_at' => $property->updated_at, |
||
| 367 | ]; |
||
| 368 | })->prepend([ |
||
| 369 | 'old value' => $concept->uri, |
||
| 370 | 'updated_at' => $concept->updated_at, |
||
| 371 | ], |
||
| 372 | '*uri')->prepend([ |
||
| 373 | 'old value' => $concept->status->display_name, |
||
| 374 | 'updated_at' => $concept->updated_at, |
||
| 375 | ], |
||
| 376 | '*status'); |
||
| 377 | }); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return Collection |
||
| 382 | */ |
||
| 383 | public function getElementSetStatements(): Collection |
||
| 384 | { |
||
| 385 | return Element::whereSchemaId($this->export->schema_id)->with('statements.profile_property', 'status')->get()->keyBy('id')->map(function ($element, $key) { |
||
|
1 ignored issue
–
show
|
|||
| 386 | $status = $element->status->display_name; |
||
| 387 | $thingy = $element->statements->keyBy('id')->map(function ($property) { |
||
| 388 | return [ |
||
| 389 | 'old value' => $property->object, |
||
| 390 | 'updated_at' => $property->updated_at, |
||
| 391 | 'profile_uri' => $property->profile_property->uri, |
||
| 392 | 'is_resource' => (bool) $property->profile_property->is_object_prop, |
||
| 393 | ]; |
||
| 394 | })->map(function ($item) use ($status) { |
||
| 395 | if ($item['profile_uri'] === 'reg:status') { |
||
| 396 | $item['old value'] = $status; |
||
| 397 | } |
||
| 398 | if ($item['is_resource']) { |
||
| 399 | $item['old value'] = self::makeCurie($this->prefixes, $item['old value']); |
||
| 400 | } |
||
| 401 | |||
| 402 | return $item; |
||
| 403 | }); |
||
| 404 | |||
| 405 | return $thingy; |
||
| 406 | }); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param $message |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected static function makeErrorMessage($message): string |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param $value |
||
| 421 | * @param $column |
||
| 422 | * @param $row |
||
| 423 | * @param $level |
||
| 424 | */ |
||
| 425 | private function logRowError($value, $column, $row, $level): void |
||
| 426 | { |
||
| 427 | //if this is the first error, initialize the row errors |
||
| 428 | if (! $this->errors->get('row')) { |
||
| 429 | $this->errors->put('row', collect()); |
||
| 430 | } |
||
| 431 | |||
| 432 | $this->errors->get('row')->push(collect([$row, $column, $value, $level])); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param array $prefixes |
||
| 437 | * @param string $uri |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | private function makeFqn($prefixes, $uri): string |
||
| 442 | { |
||
| 443 | $result = $uri; |
||
| 444 | foreach ($prefixes as $prefix => $fullUri) { |
||
| 445 | $result = preg_replace('#' . $prefix . ':#uis', $fullUri, $uri); |
||
| 446 | if ($result !== $uri) { |
||
| 447 | break; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | if ($uri === $result && strpos($uri, ':') && ! strpos($uri, '://')) { |
||
| 451 | //we have an unregistered prefix |
||
| 452 | $prefix = str_before($uri, ':'); |
||
| 453 | $this->logRowError(self::makeErrorMessage("'$prefix' is an unregistered prefix and cannot be expanded to form a full URI"), $this->currentColumnName, $this->currentRowName, 'warning'); |
||
| 454 | } |
||
| 455 | |||
| 456 | return $result; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param array $prefixes |
||
| 461 | * @param string $uri |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | private static function makeCurie($prefixes, $uri): string |
||
| 466 | { |
||
| 467 | $result = $uri; |
||
| 468 | foreach ($prefixes as $prefix => $fullUri) { |
||
| 469 | $result = preg_replace('!' . $fullUri . '!uis', $prefix . ':', $uri); |
||
| 470 | if ($result !== $uri) { |
||
| 471 | break; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | return $result; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param $value |
||
| 480 | * @param $column |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | private function validateRequired($value, $column): string |
||
| 492 | } |
||
| 493 | } |
||
| 494 |