Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractSubject 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractSubject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | abstract class AbstractSubject implements SubjectInterface |
||
| 49 | { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The trait that provides basic filesystem handling functionality. |
||
| 53 | * |
||
| 54 | * @var TechDivision\Import\Subjects\FilesystemTrait |
||
| 55 | */ |
||
| 56 | use FilesystemTrait; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The trait that provides basic filesystem handling functionality. |
||
| 60 | * |
||
| 61 | * @var TechDivision\Import\SystemLoggerTrait |
||
| 62 | */ |
||
| 63 | use SystemLoggerTrait; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The trait that provides header handling functionality. |
||
| 67 | * |
||
| 68 | * @var TechDivision\Import\HeaderTrait |
||
| 69 | */ |
||
| 70 | use HeaderTrait; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The trait that provides row handling functionality. |
||
| 74 | * |
||
| 75 | * @var TechDivision\Import\RowTrait |
||
| 76 | */ |
||
| 77 | use RowTrait; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The name of the file to be imported. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $filename; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The actual line number. |
||
| 88 | * |
||
| 89 | * @var integer |
||
| 90 | */ |
||
| 91 | protected $lineNumber = 0; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The actual operation name. |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $operationName ; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The flag that stop's overserver execution on the actual row. |
||
| 102 | * |
||
| 103 | * @var boolean |
||
| 104 | */ |
||
| 105 | protected $skipRow = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The import adapter instance. |
||
| 109 | * |
||
| 110 | * @var \TechDivision\Import\Adapter\ImportAdapterInterface |
||
| 111 | */ |
||
| 112 | protected $importAdapter; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The system configuration. |
||
| 116 | * |
||
| 117 | * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
||
| 118 | */ |
||
| 119 | protected $configuration; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The RegistryProcessor instance to handle running threads. |
||
| 123 | * |
||
| 124 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 125 | */ |
||
| 126 | protected $registryProcessor; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The actions unique serial. |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $serial; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Array with the subject's observers. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $observers = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Array with the subject's callbacks. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | protected $callbacks = array(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The subject's callback mappings. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $callbackMappings = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The available root categories. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | protected $rootCategories = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The Magento configuration. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected $coreConfigData = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The available stores. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | protected $stores = array(); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The available websites. |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | protected $storeWebsites = array(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The default store. |
||
| 186 | * |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | protected $defaultStore; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * The store view code the create the product/attributes for. |
||
| 193 | * |
||
| 194 | * @var string |
||
| 195 | */ |
||
| 196 | protected $storeViewCode; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * The UID generator for the core config data. |
||
| 200 | * |
||
| 201 | * @var \TechDivision\Import\Utils\Generators\GeneratorInterface |
||
| 202 | */ |
||
| 203 | protected $coreConfigDataUidGenerator; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Initialize the subject instance. |
||
| 207 | * |
||
| 208 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 209 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
| 210 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances |
||
| 211 | */ |
||
| 212 | 77 | public function __construct( |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Set's the name of the file to import |
||
| 224 | * |
||
| 225 | * @param string $filename The filename |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | 10 | public function setFilename($filename) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Return's the name of the file to import. |
||
| 236 | * |
||
| 237 | * @return string The filename |
||
| 238 | */ |
||
| 239 | 12 | public function getFilename() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Set's the actual operation name. |
||
| 246 | * |
||
| 247 | * @param string $operationName The actual operation name |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | 1 | public function setOperationName($operationName) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Return's the actual operation name. |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | 1 | public function getOperationName() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Set's the actual line number. |
||
| 268 | * |
||
| 269 | * @param integer $lineNumber The line number |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | 1 | public function setLineNumber($lineNumber) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return's the actual line number. |
||
| 280 | * |
||
| 281 | * @return integer The line number |
||
| 282 | */ |
||
| 283 | 13 | public function getLineNumber() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Stop's observer execution on the actual row. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 1 | public function skipRow() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return's the default callback mappings. |
||
| 300 | * |
||
| 301 | * @return array The default callback mappings |
||
| 302 | */ |
||
| 303 | 1 | public function getDefaultCallbackMappings() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
| 310 | * If the passed value is NOT a valid date, NULL will be returned. |
||
| 311 | * |
||
| 312 | * @param string $value The value to format |
||
| 313 | * |
||
| 314 | * @return string|null The formatted date or NULL if the date is not valid |
||
| 315 | */ |
||
| 316 | 2 | public function formatDate($value) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Extracts the elements of the passed value by exploding them |
||
| 330 | * with the also passed delimiter. |
||
| 331 | * |
||
| 332 | * @param string $value The value to extract |
||
| 333 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
| 334 | * |
||
| 335 | * @return array The exploded values |
||
| 336 | */ |
||
| 337 | 2 | public function explode($value, $delimiter = null) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
| 353 | * |
||
| 354 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
| 355 | */ |
||
| 356 | 1 | public function isDebugMode() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Set's the subject configuration. |
||
| 363 | * |
||
| 364 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 77 | public function setConfiguration(SubjectConfigurationInterface $configuration) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Return's the subject configuration. |
||
| 375 | * |
||
| 376 | * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
||
| 377 | */ |
||
| 378 | 77 | public function getConfiguration() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Set's the import adapter instance. |
||
| 385 | * |
||
| 386 | * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | 3 | public function setImportAdapter(ImportAdapterInterface $importAdapter) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Return's the import adapter instance. |
||
| 397 | * |
||
| 398 | * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance |
||
| 399 | */ |
||
| 400 | 3 | public function getImportAdapter() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 407 | * |
||
| 408 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
| 409 | */ |
||
| 410 | 77 | public function getRegistryProcessor() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Set's the unique serial for this import process. |
||
| 417 | * |
||
| 418 | * @param string $serial The unique serial |
||
| 419 | * |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | 9 | public function setSerial($serial) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Return's the unique serial for this import process. |
||
| 429 | * |
||
| 430 | * @return string The unique serial |
||
| 431 | */ |
||
| 432 | 4 | public function getSerial() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Return's the source date format to use. |
||
| 439 | * |
||
| 440 | * @return string The source date format |
||
| 441 | */ |
||
| 442 | 4 | public function getSourceDateFormat() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
| 449 | * |
||
| 450 | * @return string The multiple field delimiter character |
||
| 451 | */ |
||
| 452 | 1 | public function getMultipleFieldDelimiter() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Return's the multiple value delimiter character to use, default value is comma (|). |
||
| 459 | * |
||
| 460 | * @return string The multiple value delimiter character |
||
| 461 | */ |
||
| 462 | 1 | public function getMultipleValueDelimiter() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 469 | * |
||
| 470 | * @param string $serial The serial of the actual import |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | 77 | public function setUp($serial) |
|
| 475 | { |
||
| 476 | |||
| 477 | // load the status of the actual import |
||
| 478 | 77 | $status = $this->getRegistryProcessor()->getAttribute($serial); |
|
| 479 | |||
| 480 | // load the global data we've prepared initially |
||
| 481 | 77 | $this->stores = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORES]; |
|
| 482 | 77 | $this->defaultStore = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::DEFAULT_STORE]; |
|
| 483 | 77 | $this->storeWebsites = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES]; |
|
| 484 | 77 | $this->rootCategories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ROOT_CATEGORIES]; |
|
| 485 | 77 | $this->coreConfigData = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CORE_CONFIG_DATA]; |
|
| 486 | |||
| 487 | // initialize the operation name |
||
| 488 | 77 | $this->operationName = $this->getConfiguration()->getConfiguration()->getOperationName(); |
|
| 489 | |||
| 490 | // merge the callback mappings with the mappings from the child instance |
||
| 491 | 77 | $this->callbackMappings = array_merge($this->callbackMappings, $this->getDefaultCallbackMappings()); |
|
| 492 | |||
| 493 | // merge the header mappings with the values found in the configuration |
||
| 494 | 77 | $this->headerMappings = array_merge($this->headerMappings, $this->getConfiguration()->getHeaderMappings()); |
|
| 495 | |||
| 496 | // merge the callback mappings the the one from the configuration file |
||
| 497 | 77 | foreach ($this->getConfiguration()->getCallbacks() as $callbackMappings) { |
|
| 498 | 77 | foreach ($callbackMappings as $attributeCode => $mappings) { |
|
| 499 | // write a log message, that default callback configuration will |
||
| 500 | // be overwritten with the one from the configuration file |
||
| 501 | 77 | if (isset($this->callbackMappings[$attributeCode])) { |
|
| 502 | 72 | $this->getSystemLogger()->notice( |
|
| 503 | 72 | sprintf('Now override callback mappings for attribute %s with values found in configuration file', $attributeCode) |
|
| 504 | 72 | ); |
|
| 505 | 72 | } |
|
| 506 | |||
| 507 | // override the attributes callbacks |
||
| 508 | 77 | $this->callbackMappings[$attributeCode] = $mappings; |
|
| 509 | 77 | } |
|
| 510 | 77 | } |
|
| 511 | 77 | } |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Clean up the global data after importing the variants. |
||
| 515 | * |
||
| 516 | * @param string $serial The serial of the actual import |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | 1 | public function tearDown($serial) |
|
| 521 | { |
||
| 522 | |||
| 523 | // load the registry processor |
||
| 524 | 1 | $registryProcessor = $this->getRegistryProcessor(); |
|
| 525 | |||
| 526 | // update the source directory for the next subject |
||
| 527 | 1 | $registryProcessor->mergeAttributesRecursive( |
|
| 528 | 1 | $serial, |
|
| 529 | 1 | array(RegistryKeys::SOURCE_DIRECTORY => $newSourceDir = $this->getNewSourceDir($serial)) |
|
| 530 | 1 | ); |
|
| 531 | |||
| 532 | // log a debug message with the new source directory |
||
| 533 | 1 | $this->getSystemLogger()->debug( |
|
| 534 | 1 | sprintf('Subject %s successfully updated source directory to %s', get_class($this), $newSourceDir) |
|
| 535 | 1 | ); |
|
| 536 | 1 | } |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Return's the target directory for the artefact export. |
||
| 540 | * |
||
| 541 | * @return string The target directory for the artefact export |
||
| 542 | */ |
||
| 543 | 1 | public function getTargetDir() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Return's the next source directory, which will be the target directory |
||
| 550 | * of this subject, in most cases. |
||
| 551 | * |
||
| 552 | * @param string $serial The serial of the actual import |
||
| 553 | * |
||
| 554 | * @return string The new source directory |
||
| 555 | */ |
||
| 556 | 4 | public function getNewSourceDir($serial) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Register the passed observer with the specific type. |
||
| 563 | * |
||
| 564 | * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to register |
||
| 565 | * @param string $type The type to register the observer with |
||
| 566 | * |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | 6 | public function registerObserver(ObserverInterface $observer, $type) |
|
| 570 | { |
||
| 571 | |||
| 572 | // query whether or not the array with the callbacks for the |
||
| 573 | // passed type has already been initialized, or not |
||
| 574 | 6 | if (!isset($this->observers[$type])) { |
|
| 575 | 6 | $this->observers[$type] = array(); |
|
| 576 | 6 | } |
|
| 577 | |||
| 578 | // append the callback with the instance of the passed type |
||
| 579 | 6 | $this->observers[$type][] = $observer; |
|
| 580 | 6 | } |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Register the passed callback with the specific type. |
||
| 584 | * |
||
| 585 | * @param \TechDivision\Import\Callbacks\CallbackInterface $callback The subject to register the callbacks for |
||
| 586 | * @param string $type The type to register the callback with |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | 2 | public function registerCallback(CallbackInterface $callback, $type) |
|
| 591 | { |
||
| 592 | |||
| 593 | // query whether or not the array with the callbacks for the |
||
| 594 | // passed type has already been initialized, or not |
||
| 595 | 2 | if (!isset($this->callbacks[$type])) { |
|
| 596 | 2 | $this->callbacks[$type] = array(); |
|
| 597 | 2 | } |
|
| 598 | |||
| 599 | // append the callback with the instance of the passed type |
||
| 600 | 2 | $this->callbacks[$type][] = $callback; |
|
| 601 | 2 | } |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Return's the array with callbacks for the passed type. |
||
| 605 | * |
||
| 606 | * @param string $type The type of the callbacks to return |
||
| 607 | * |
||
| 608 | * @return array The callbacks |
||
| 609 | */ |
||
| 610 | 1 | public function getCallbacksByType($type) |
|
| 611 | { |
||
| 612 | |||
| 613 | // initialize the array for the callbacks |
||
| 614 | 1 | $callbacks = array(); |
|
| 615 | |||
| 616 | // query whether or not callbacks for the type are available |
||
| 617 | 1 | if (isset($this->callbacks[$type])) { |
|
| 618 | 1 | $callbacks = $this->callbacks[$type]; |
|
| 619 | 1 | } |
|
| 620 | |||
| 621 | // return the array with the type's callbacks |
||
| 622 | 1 | return $callbacks; |
|
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Return's the array with the available observers. |
||
| 627 | * |
||
| 628 | * @return array The observers |
||
| 629 | */ |
||
| 630 | 6 | public function getObservers() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Return's the array with the available callbacks. |
||
| 637 | * |
||
| 638 | * @return array The callbacks |
||
| 639 | */ |
||
| 640 | 1 | public function getCallbacks() |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Return's the callback mappings for this subject. |
||
| 647 | * |
||
| 648 | * @return array The array with the subject's callback mappings |
||
| 649 | */ |
||
| 650 | 2 | public function getCallbackMappings() |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Imports the content of the file with the passed filename. |
||
| 657 | * |
||
| 658 | * |
||
| 659 | * @param string $serial The serial of the actual import |
||
| 660 | * @param string $filename The filename to process |
||
| 661 | * |
||
| 662 | * @return void |
||
| 663 | * @throws \Exception Is thrown, if the import can't be processed |
||
| 664 | */ |
||
| 665 | 5 | public function import($serial, $filename) |
|
| 731 | |||
| 732 | /** |
||
| 733 | * This method queries whether or not the passed filename matches |
||
| 734 | * the pattern, based on the subjects configured prefix. |
||
| 735 | * |
||
| 736 | * @param string $filename The filename to match |
||
| 737 | * |
||
| 738 | * @return boolean TRUE if the filename matches, else FALSE |
||
| 739 | */ |
||
| 740 | 5 | protected function match($filename) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Imports the passed row into the database. If the import failed, the exception |
||
| 756 | * will be catched and logged, but the import process will be continued. |
||
| 757 | * |
||
| 758 | * @param array $row The row with the data to be imported |
||
| 759 | * |
||
| 760 | * @return void |
||
| 761 | */ |
||
| 762 | 7 | public function importRow(array $row) |
|
| 763 | { |
||
| 764 | |||
| 765 | // initialize the row |
||
| 766 | 7 | $this->row = $row; |
|
| 767 | |||
| 768 | // raise the line number and reset the skip row flag |
||
| 769 | 7 | $this->lineNumber++; |
|
| 770 | 7 | $this->skipRow = false; |
|
| 771 | |||
| 772 | // initialize the headers with the columns from the first line |
||
| 773 | 7 | if (sizeof($this->headers) === 0) { |
|
| 774 | 1 | foreach ($this->row as $value => $key) { |
|
| 775 | 1 | $this->headers[$this->mapAttributeCodeByHeaderMapping($key)] = $value; |
|
| 776 | 1 | } |
|
| 777 | 1 | return; |
|
| 778 | } |
||
| 779 | |||
| 780 | // process the observers |
||
| 781 | 6 | foreach ($this->getObservers() as $observers) { |
|
| 782 | // invoke the pre-import/import and post-import observers |
||
| 783 | 6 | foreach ($observers as $observer) { |
|
| 784 | // query whether or not we have to skip the row |
||
| 785 | 6 | if ($this->skipRow) { |
|
| 786 | // log a debug message with the actual line nr/file information |
||
| 787 | 1 | $this->getSystemLogger()->warning( |
|
| 788 | 1 | $this->appendExceptionSuffix( |
|
| 789 | 1 | sprintf( |
|
| 790 | 1 | 'Skip processing operation "%s" after observer "%s"', |
|
| 791 | 1 | $this->operationName, |
|
| 792 | 1 | $this->getConfiguration()->getId() |
|
| 793 | 1 | ) |
|
| 794 | 1 | ) |
|
| 795 | 1 | ); |
|
| 796 | |||
| 797 | // skip the row |
||
| 798 | 1 | break 2; |
|
| 799 | } |
||
| 800 | |||
| 801 | // if not, set the subject and process the observer |
||
| 802 | 6 | if ($observer instanceof ObserverInterface) { |
|
| 803 | 6 | $this->row = $observer->handle($this); |
|
| 804 | 6 | } |
|
| 805 | 6 | } |
|
| 806 | 6 | } |
|
| 807 | |||
| 808 | // log a debug message with the actual line nr/file information |
||
| 809 | 6 | $this->getSystemLogger()->debug( |
|
| 810 | 6 | $this->appendExceptionSuffix( |
|
| 811 | 6 | sprintf( |
|
| 812 | 6 | 'Successfully processed operation "%s"', |
|
| 813 | 6 | $this->operationName |
|
| 814 | 6 | ) |
|
| 815 | 6 | ) |
|
| 816 | 6 | ); |
|
| 817 | 6 | } |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Queries whether or not that the subject needs an OK file to be processed. |
||
| 821 | * |
||
| 822 | * @return boolean TRUE if the subject needs an OK file, else FALSE |
||
| 823 | */ |
||
| 824 | 1 | public function isOkFileNeeded() |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Return's the default store. |
||
| 831 | * |
||
| 832 | * @return array The default store |
||
| 833 | */ |
||
| 834 | public function getDefaultStore() |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Return's the default store view code. |
||
| 841 | * |
||
| 842 | * @return array The default store view code |
||
| 843 | */ |
||
| 844 | 5 | public function getDefaultStoreViewCode() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Set's the store view code the create the product/attributes for. |
||
| 851 | * |
||
| 852 | * @param string $storeViewCode The store view code |
||
| 853 | * |
||
| 854 | * @return void |
||
| 855 | */ |
||
| 856 | 4 | public function setStoreViewCode($storeViewCode) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Return's the store view code the create the product/attributes for. |
||
| 863 | * |
||
| 864 | * @param string|null $default The default value to return, if the store view code has not been set |
||
| 865 | * |
||
| 866 | * @return string The store view code |
||
| 867 | */ |
||
| 868 | 8 | public function getStoreViewCode($default = null) |
|
| 885 | |||
| 886 | /** |
||
| 887 | * Prepare's the store view code in the subject. If the store_view_code row doesn't contain |
||
| 888 | * any value, the default code of the default store view will be set. |
||
| 889 | * |
||
| 890 | * @return void |
||
| 891 | */ |
||
| 892 | 2 | public function prepareStoreViewCode() |
|
| 893 | { |
||
| 894 | |||
| 895 | // re-set the store view code |
||
| 896 | 2 | $this->setStoreViewCode(null); |
|
| 897 | |||
| 898 | // initialize the store view code |
||
| 899 | 2 | if ($storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE)) { |
|
| 900 | 2 | $this->setStoreViewCode($storeViewCode); |
|
| 901 | 2 | } |
|
| 902 | 2 | } |
|
| 903 | |||
| 904 | /** |
||
| 905 | * Return's the store ID of the store with the passed store view code |
||
| 906 | * |
||
| 907 | * @param string $storeViewCode The store view code to return the store ID for |
||
| 908 | * |
||
| 909 | * @return integer The ID of the store with the passed ID |
||
| 910 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 911 | */ |
||
| 912 | 4 | public function getStoreId($storeViewCode) |
|
| 913 | { |
||
| 914 | |||
| 915 | // query whether or not, the requested store is available |
||
| 916 | 4 | if (isset($this->stores[$storeViewCode])) { |
|
| 917 | 3 | return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID]; |
|
| 918 | } |
||
| 919 | |||
| 920 | // throw an exception, if not |
||
| 921 | 1 | throw new \Exception( |
|
| 922 | 1 | sprintf( |
|
| 923 | 1 | 'Found invalid store view code %s in file %s on line %d', |
|
| 924 | 1 | $storeViewCode, |
|
| 925 | 1 | $this->getFilename(), |
|
| 926 | 1 | $this->getLineNumber() |
|
| 927 | 1 | ) |
|
| 928 | 1 | ); |
|
| 929 | } |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Return's the store ID of the actual row, or of the default store |
||
| 933 | * if no store view code is set in the CSV file. |
||
| 934 | * |
||
| 935 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
| 936 | * |
||
| 937 | * @return integer The ID of the actual store |
||
| 938 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
| 939 | */ |
||
| 940 | 2 | public function getRowStoreId($default = null) |
|
| 941 | { |
||
| 942 | |||
| 943 | // initialize the default store view code, if not passed |
||
| 944 | 2 | if ($default === null) { |
|
| 945 | 2 | $default = $this->getDefaultStoreViewCode(); |
|
| 946 | 2 | } |
|
| 947 | |||
| 948 | // load the store view code the create the product/attributes for |
||
| 949 | 2 | return $this->getStoreId($this->getStoreViewCode($default)); |
|
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Return's the root category for the actual view store. |
||
| 954 | * |
||
| 955 | * @return array The store's root category |
||
| 956 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
| 957 | */ |
||
| 958 | 2 | public function getRootCategory() |
|
| 972 | |||
| 973 | /** |
||
| 974 | * Return's the Magento configuration value. |
||
| 975 | * |
||
| 976 | * @param string $path The Magento path of the requested configuration value |
||
| 977 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
| 978 | * @param string $scope The scope the configuration value has been set |
||
| 979 | * @param integer $scopeId The scope ID the configuration value has been set |
||
| 980 | * |
||
| 981 | * @return mixed The configuration value |
||
| 982 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
| 983 | */ |
||
| 984 | 5 | public function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
|
| 985 | { |
||
| 986 | |||
| 987 | // initialize the core config data |
||
| 988 | $coreConfigData = array( |
||
| 989 | 5 | MemberNames::PATH => $path, |
|
| 990 | 5 | MemberNames::SCOPE => $scope, |
|
| 991 | 5 | MemberNames::SCOPE_ID => $scopeId |
|
| 992 | 5 | ); |
|
| 993 | |||
| 994 | // generate the UID from the passed data |
||
| 995 | 5 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
| 996 | |||
| 997 | // iterate over the core config data and try to find the requested configuration value |
||
| 998 | 5 | if (isset($this->coreConfigData[$uniqueIdentifier])) { |
|
| 999 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
| 1000 | } |
||
| 1001 | |||
| 1002 | // query whether or not we've to query for the configuration value on fallback level 'websites' also |
||
| 1003 | 4 | if ($scope === ScopeKeys::SCOPE_STORES) { |
|
| 1004 | // query whether or not the website with the passed ID is available |
||
| 1005 | 2 | foreach ($this->storeWebsites as $storeWebsite) { |
|
| 1006 | 2 | if ($storeWebsite[MemberNames::WEBSITE_ID] === $scopeId) { |
|
| 1007 | // replace scope with 'websites' and website ID |
||
| 1008 | 2 | $coreConfigData = array_merge( |
|
| 1009 | 2 | $coreConfigData, |
|
| 1010 | array( |
||
| 1011 | 2 | MemberNames::SCOPE => ScopeKeys::SCOPE_WEBSITES, |
|
| 1012 | 2 | MemberNames::SCOPE_ID => $storeWebsite[MemberNames::WEBSITE_ID] |
|
| 1013 | 2 | ) |
|
| 1014 | 2 | ); |
|
| 1015 | |||
| 1016 | // generate the UID from the passed data, merged with the 'websites' scope and ID |
||
| 1017 | 2 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
| 1018 | |||
| 1019 | // query whether or not, the configuration value on 'websites' level |
||
| 1020 | 2 | View Code Duplication | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
| 1021 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
| 1022 | } |
||
| 1023 | 1 | } |
|
| 1024 | 2 | } |
|
| 1025 | 1 | } |
|
| 1026 | |||
| 1027 | // replace scope with 'default' and scope ID '0' |
||
| 1028 | 3 | $coreConfigData = array_merge( |
|
| 1029 | 3 | $coreConfigData, |
|
| 1030 | array( |
||
| 1031 | 3 | MemberNames::SCOPE => ScopeKeys::SCOPE_DEFAULT, |
|
| 1032 | 3 | MemberNames::SCOPE_ID => 0 |
|
| 1033 | 3 | ) |
|
| 1034 | 3 | ); |
|
| 1035 | |||
| 1036 | // generate the UID from the passed data, merged with the 'default' scope and ID 0 |
||
| 1037 | 3 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
| 1038 | |||
| 1039 | // query whether or not, the configuration value on 'default' level |
||
| 1040 | 3 | View Code Duplication | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
| 1041 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
| 1042 | } |
||
| 1043 | |||
| 1044 | // if not, return the passed default value |
||
| 1045 | 2 | if ($default !== null) { |
|
| 1046 | 1 | return $default; |
|
| 1047 | } |
||
| 1048 | |||
| 1049 | // throw an exception if no value can be found |
||
| 1050 | // in the Magento configuration |
||
| 1051 | 1 | throw new \Exception( |
|
| 1052 | 1 | sprintf( |
|
| 1053 | 1 | 'Can\'t find a value for configuration "%s-%s-%d" in "core_config_data"', |
|
| 1054 | 1 | $path, |
|
| 1055 | 1 | $scope, |
|
| 1056 | $scopeId |
||
| 1057 | 1 | ) |
|
| 1058 | 1 | ); |
|
| 1059 | } |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Resolve the original column name for the passed one. |
||
| 1063 | * |
||
| 1064 | * @param string $columnName The column name that has to be resolved |
||
| 1065 | * |
||
| 1066 | * @return string|null The original column name |
||
| 1067 | */ |
||
| 1068 | 2 | public function resolveOriginalColumnName($columnName) |
|
| 1069 | { |
||
| 1070 | |||
| 1071 | // try to load the original data |
||
| 1072 | 2 | $originalData = $this->getOriginalData(); |
|
| 1073 | |||
| 1074 | // query whether or not original data is available |
||
| 1075 | 2 | if (isset($originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES])) { |
|
| 1076 | // query whether or not the original column name is available |
||
| 1077 | 1 | if (isset($originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES][$columnName])) { |
|
| 1078 | 1 | return $originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES][$columnName]; |
|
| 1079 | } |
||
| 1080 | |||
| 1081 | // query whether or a wildcard column name is available |
||
| 1082 | 1 | if (isset($originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES]['*'])) { |
|
| 1083 | 1 | return $originalData[ColumnKeys::ORIGINAL_COLUMN_NAMES]['*']; |
|
| 1084 | } |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | // return the original column name |
||
| 1088 | 1 | return $columnName; |
|
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Return's the original data if available, or an empty array. |
||
| 1093 | * |
||
| 1094 | * @return array The original data |
||
| 1095 | */ |
||
| 1096 | 2 | public function getOriginalData() |
|
| 1097 | { |
||
| 1098 | |||
| 1099 | // initialize the array for the original data |
||
| 1100 | 2 | $originalData = array(); |
|
| 1101 | |||
| 1102 | // query whether or not the column contains original data |
||
| 1103 | 2 | if ($this->hasOriginalData()) { |
|
| 1104 | // unerialize the original data from the column |
||
| 1105 | 1 | $originalData = unserialize($this->row[$this->headers[ColumnKeys::ORIGINAL_DATA]]); |
|
| 1106 | 1 | } |
|
| 1107 | |||
| 1108 | // return an empty array, if not |
||
| 1109 | 2 | return $originalData; |
|
| 1110 | } |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Query's whether or not the actual column contains original data like |
||
| 1114 | * filename, line number and column names. |
||
| 1115 | * |
||
| 1116 | * @return boolean TRUE if the actual column contains origin data, else FALSE |
||
| 1117 | */ |
||
| 1118 | 3 | public function hasOriginalData() |
|
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Wraps the passed exeception into a new one by trying to resolve the original filname, |
||
| 1125 | * line number and column names and use it for a detailed exception message. |
||
| 1126 | * |
||
| 1127 | * @param array $columnNames The column names that should be resolved and wrapped |
||
| 1128 | * @param \Exception $parent The exception we want to wrap |
||
| 1129 | * @param string $className The class name of the exception type we want to wrap the parent one |
||
| 1130 | * |
||
| 1131 | * @return \Exception the wrapped exception |
||
| 1132 | */ |
||
| 1133 | 2 | public function wrapException( |
|
| 1134 | array $columnNames = array(), |
||
| 1135 | \Exception $parent = null, |
||
| 1136 | $className = '\TechDivision\Import\Exceptions\WrappedColumnException' |
||
| 1137 | ) { |
||
| 1138 | |||
| 1139 | // initialize the message |
||
| 1140 | 2 | $message = $parent->getMessage(); |
|
| 1141 | |||
| 1142 | // query whether or not has been a result of invalid data of a previous column of a CSV file |
||
| 1143 | 2 | if ($this->hasOriginalData()) { |
|
| 1144 | // load the original data |
||
| 1145 | 1 | $originalData = $this->getOriginalData(); |
|
| 1146 | |||
| 1147 | // replace old filename and line number of the original message |
||
| 1148 | 1 | $message = $this->appendExceptionSuffix( |
|
| 1149 | 1 | $this->stripExceptionSuffix($message), |
|
| 1150 | 1 | $originalData[ColumnKeys::ORIGINAL_FILENAME], |
|
| 1151 | 1 | $originalData[ColumnKeys::ORIGINAL_LINE_NUMBER] |
|
| 1152 | 1 | ); |
|
| 1153 | |||
| 1154 | 1 | } else { |
|
| 1155 | // append filename and line number to the original message |
||
| 1156 | 1 | $message = $this->appendExceptionSuffix( |
|
| 1157 | 1 | $this->stripExceptionSuffix($message), |
|
| 1158 | 1 | $this->filename, |
|
| 1159 | 1 | $this->lineNumber |
|
| 1160 | 1 | ); |
|
| 1161 | } |
||
| 1162 | |||
| 1163 | // query whether or not, column names has been passed |
||
| 1164 | 2 | if (sizeof($columnNames) > 0) { |
|
| 1165 | // prepare the original column names |
||
| 1166 | 1 | $originalColumnNames = array(); |
|
| 1167 | 1 | foreach ($columnNames as $columnName) { |
|
| 1168 | 1 | $originalColumnNames[] = $this->resolveOriginalColumnName($columnName); |
|
| 1169 | 1 | } |
|
| 1170 | |||
| 1171 | // append the column information |
||
| 1172 | 1 | $message = sprintf('%s in column(s) %s', $message, implode(', ', $originalColumnNames)); |
|
| 1173 | 1 | } |
|
| 1174 | |||
| 1175 | // create a new exception and wrap the parent one |
||
| 1176 | 2 | return new $className($message, null, $parent); |
|
| 1177 | } |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Strip's the exception suffix containing filename and line number from the |
||
| 1181 | * passed message. |
||
| 1182 | * |
||
| 1183 | * @param string $message The message to strip the exception suffix from |
||
| 1184 | * |
||
| 1185 | * @return mixed The message without the exception suffix |
||
| 1186 | */ |
||
| 1187 | 2 | public function stripExceptionSuffix($message) |
|
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Append's the exception suffix containing filename and line number to the |
||
| 1194 | * passed message. If no message has been passed, only the suffix will be |
||
| 1195 | * returned |
||
| 1196 | * |
||
| 1197 | * @param string|null $message The message to append the exception suffix to |
||
| 1198 | * @param string|null $filename The filename used to create the suffix |
||
| 1199 | * @param string|null $lineNumber The line number used to create the suffx |
||
| 1200 | * |
||
| 1201 | * @return string The message with the appended exception suffix |
||
| 1202 | */ |
||
| 1203 | 12 | public function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null) |
|
| 1204 | { |
||
| 1205 | |||
| 1206 | // query whether or not a filename has been passed |
||
| 1207 | 12 | if ($filename === null) { |
|
| 1208 | 12 | $filename = $this->getFilename(); |
|
| 1209 | 12 | } |
|
| 1210 | |||
| 1211 | // query whether or not a line number has been passed |
||
| 1212 | 12 | if ($lineNumber === null) { |
|
| 1213 | 12 | $lineNumber = $this->getLineNumber(); |
|
| 1214 | 12 | } |
|
| 1215 | |||
| 1216 | // if no message has been passed, only return the suffix |
||
| 1217 | 12 | if ($message === null) { |
|
| 1218 | 2 | return sprintf(' in file %s on line %d', $filename, $lineNumber); |
|
| 1219 | } |
||
| 1220 | |||
| 1221 | // concatenate the message with the suffix and return it |
||
| 1222 | 12 | return sprintf('%s in file %s on line %d', $message, $filename, $lineNumber); |
|
| 1223 | } |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Raises the value for the counter with the passed key by one. |
||
| 1227 | * |
||
| 1228 | * @param mixed $counterName The name of the counter to raise |
||
| 1229 | * |
||
| 1230 | * @return integer The counter's new value |
||
| 1231 | */ |
||
| 1232 | 1 | public function raiseCounter($counterName) |
|
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Merge the passed array into the status of the actual import. |
||
| 1244 | * |
||
| 1245 | * @param array $status The status information to be merged |
||
| 1246 | * |
||
| 1247 | * @return void |
||
| 1248 | */ |
||
| 1249 | 1 | public function mergeAttributesRecursive(array $status) |
|
| 1258 | } |
||
| 1259 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..