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 |
||
53 | abstract class AbstractSubject implements SubjectInterface, FilesystemSubjectInterface, DateConverterSubjectInterface |
||
54 | { |
||
55 | |||
56 | /** |
||
57 | * The trait that provides basic filesystem handling functionality. |
||
58 | * |
||
59 | * @var \TechDivision\Import\Subjects\FilesystemTrait |
||
60 | */ |
||
61 | use FilesystemTrait; |
||
62 | |||
63 | /** |
||
64 | * The trait that provides basic filesystem handling functionality. |
||
65 | * |
||
66 | * @var \TechDivision\Import\SystemLoggerTrait |
||
67 | */ |
||
68 | use SystemLoggerTrait; |
||
69 | |||
70 | /** |
||
71 | * The trait that provides date converting functionality. |
||
72 | * |
||
73 | * @var \TechDivision\Import\DateConverterTrait |
||
74 | */ |
||
75 | use DateConverterTrait; |
||
76 | |||
77 | /** |
||
78 | * The trait that provides header handling functionality. |
||
79 | * |
||
80 | * @var \TechDivision\Import\HeaderTrait |
||
81 | */ |
||
82 | use HeaderTrait; |
||
83 | |||
84 | /** |
||
85 | * The trait that provides row handling functionality. |
||
86 | * |
||
87 | * @var \TechDivision\Import\RowTrait |
||
88 | */ |
||
89 | use RowTrait; |
||
90 | |||
91 | /** |
||
92 | * The unique identifier for the actual invocation. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $uniqueId; |
||
97 | |||
98 | /** |
||
99 | * The name of the file to be imported. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $filename; |
||
104 | |||
105 | /** |
||
106 | * The actual line number. |
||
107 | * |
||
108 | * @var integer |
||
109 | */ |
||
110 | protected $lineNumber = 0; |
||
111 | |||
112 | /** |
||
113 | * The import adapter instance. |
||
114 | * |
||
115 | * @var \TechDivision\Import\Adapter\ImportAdapterInterface |
||
116 | */ |
||
117 | protected $importAdapter; |
||
118 | |||
119 | /** |
||
120 | * The subject configuration. |
||
121 | * |
||
122 | * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
||
123 | */ |
||
124 | protected $configuration; |
||
125 | |||
126 | /** |
||
127 | * The plugin configuration. |
||
128 | * |
||
129 | * @var \TechDivision\Import\Configuration\PluginConfigurationInterface |
||
130 | */ |
||
131 | protected $pluginConfiguration; |
||
132 | |||
133 | /** |
||
134 | * The RegistryProcessor instance to handle running threads. |
||
135 | * |
||
136 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
137 | */ |
||
138 | protected $registryProcessor; |
||
139 | |||
140 | /** |
||
141 | * The actions unique serial. |
||
142 | * |
||
143 | * @var string |
||
144 | */ |
||
145 | protected $serial; |
||
146 | |||
147 | /** |
||
148 | * Array with the subject's observers. |
||
149 | * |
||
150 | * @var array |
||
151 | */ |
||
152 | protected $observers = array(); |
||
153 | |||
154 | /** |
||
155 | * Array with the subject's callbacks. |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $callbacks = array(); |
||
160 | |||
161 | /** |
||
162 | * The subject's callback mappings. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $callbackMappings = array(); |
||
167 | |||
168 | /** |
||
169 | * The available root categories. |
||
170 | * |
||
171 | * @var array |
||
172 | */ |
||
173 | protected $rootCategories = array(); |
||
174 | |||
175 | /** |
||
176 | * The Magento configuration. |
||
177 | * |
||
178 | * @var array |
||
179 | */ |
||
180 | protected $coreConfigData = array(); |
||
181 | |||
182 | /** |
||
183 | * The available stores. |
||
184 | * |
||
185 | * @var array |
||
186 | */ |
||
187 | protected $stores = array(); |
||
188 | |||
189 | /** |
||
190 | * The available websites. |
||
191 | * |
||
192 | * @var array |
||
193 | */ |
||
194 | protected $storeWebsites = array(); |
||
195 | |||
196 | /** |
||
197 | * The default store. |
||
198 | * |
||
199 | * @var array |
||
200 | */ |
||
201 | protected $defaultStore; |
||
202 | |||
203 | /** |
||
204 | * The store view code the create the product/attributes for. |
||
205 | * |
||
206 | * @var string |
||
207 | */ |
||
208 | protected $storeViewCode; |
||
209 | |||
210 | /** |
||
211 | * The UID generator for the core config data. |
||
212 | * |
||
213 | * @var \TechDivision\Import\Utils\Generators\GeneratorInterface |
||
214 | */ |
||
215 | protected $coreConfigDataUidGenerator; |
||
216 | |||
217 | /** |
||
218 | * UNIX timestamp with the date the last row counter has been logged. |
||
219 | * |
||
220 | * @var integer |
||
221 | */ |
||
222 | protected $lastLog = 0; |
||
223 | |||
224 | /** |
||
225 | * The number of the last line that has been logged with the row counter |
||
226 | * @var integer |
||
227 | */ |
||
228 | protected $lastLineNumber = 0; |
||
229 | |||
230 | /** |
||
231 | * The event emitter instance. |
||
232 | * |
||
233 | * @var \League\Event\EmitterInterface |
||
234 | */ |
||
235 | protected $emitter; |
||
236 | |||
237 | /** |
||
238 | * The status of the file (0 = not processed, 1 = successfully processed, 2 = processed with failure) |
||
239 | * |
||
240 | * @var array |
||
241 | */ |
||
242 | protected $status = array(); |
||
243 | |||
244 | /** |
||
245 | * The default values for the columns from the configuration. |
||
246 | * |
||
247 | * @var array |
||
248 | */ |
||
249 | protected $defaultColumnValues = array(); |
||
250 | |||
251 | /** |
||
252 | * The values of the actual column, pre-initialized with the default values. |
||
253 | * |
||
254 | * @var array |
||
255 | */ |
||
256 | protected $columnValues = array(); |
||
257 | |||
258 | /** |
||
259 | * Mapping for the virtual entity type code to the real Magento 2 EAV entity type code. |
||
260 | * |
||
261 | * @var array |
||
262 | */ |
||
263 | protected $entityTypeCodeMappings = array( |
||
264 | EntityTypeCodes::EAV_ATTRIBUTE => EntityTypeCodes::CATALOG_PRODUCT, |
||
265 | EntityTypeCodes::EAV_ATTRIBUTE_SET => EntityTypeCodes::CATALOG_PRODUCT, |
||
266 | EntityTypeCodes::CATALOG_PRODUCT_URL => EntityTypeCodes::CATALOG_PRODUCT, |
||
267 | EntityTypeCodes::CATALOG_PRODUCT_PRICE => EntityTypeCodes::CATALOG_PRODUCT, |
||
268 | EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => EntityTypeCodes::CATALOG_PRODUCT, |
||
269 | EntityTypeCodes::CATALOG_PRODUCT_INVENTORY_MSI => EntityTypeCodes::CATALOG_PRODUCT, |
||
270 | EntityTypeCodes::CATALOG_PRODUCT_TIER_PRICE => EntityTypeCodes::CATALOG_PRODUCT, |
||
271 | EntityTypeCodes::CUSTOMER => EntityTypeCodes::CUSTOMER |
||
272 | ); |
||
273 | |||
274 | /** |
||
275 | * Initialize the subject instance. |
||
276 | * |
||
277 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
278 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
279 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances |
||
280 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
281 | */ |
||
282 | 81 | public function __construct( |
|
293 | |||
294 | /** |
||
295 | * Return's the event emitter instance. |
||
296 | * |
||
297 | * @return \League\Event\EmitterInterface The event emitter instance |
||
298 | */ |
||
299 | 9 | public function getEmitter() |
|
303 | |||
304 | /** |
||
305 | * Set's the name of the file to import |
||
306 | * |
||
307 | * @param string $filename The filename |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | 13 | public function setFilename($filename) |
|
315 | |||
316 | /** |
||
317 | * Return's the name of the file to import. |
||
318 | * |
||
319 | * @return string The filename |
||
320 | */ |
||
321 | 12 | public function getFilename() |
|
325 | |||
326 | /** |
||
327 | * Set's the actual line number. |
||
328 | * |
||
329 | * @param integer $lineNumber The line number |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | 1 | public function setLineNumber($lineNumber) |
|
337 | |||
338 | /** |
||
339 | * Return's the actual line number. |
||
340 | * |
||
341 | * @return integer The line number |
||
342 | */ |
||
343 | 17 | public function getLineNumber() |
|
347 | |||
348 | /** |
||
349 | * Return's the default callback mappings. |
||
350 | * |
||
351 | * @return array The default callback mappings |
||
352 | */ |
||
353 | 1 | public function getDefaultCallbackMappings() |
|
357 | |||
358 | /** |
||
359 | * Load the default column values from the configuration. |
||
360 | * |
||
361 | * @return array The array with the default column values |
||
362 | */ |
||
363 | 81 | View Code Duplication | public function getDefaultColumnValues() |
383 | |||
384 | /** |
||
385 | * Load the default header mappings from the configuration. |
||
386 | * |
||
387 | * @return array The array with the default header mappings |
||
388 | */ |
||
389 | 81 | View Code Duplication | public function getDefaultHeaderMappings() |
409 | |||
410 | /** |
||
411 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
412 | * If the passed value is NOT a valid date, NULL will be returned. |
||
413 | * |
||
414 | * @param string $value The value to format |
||
415 | * |
||
416 | * @return string|null The formatted date or NULL if the date is not valid |
||
417 | * @throws \InvalidArgumentException Is thrown, if the passed can not be formatted according to the configured date format |
||
418 | */ |
||
419 | public function formatDate($value) |
||
435 | |||
436 | /** |
||
437 | * Extracts the elements of the passed value by exploding them |
||
438 | * with the also passed delimiter. |
||
439 | * |
||
440 | * @param string $value The value to extract |
||
441 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
442 | * |
||
443 | * @return array The exploded values |
||
444 | */ |
||
445 | public function explode($value, $delimiter = null) |
||
449 | |||
450 | /** |
||
451 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
452 | * |
||
453 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
454 | */ |
||
455 | 1 | public function isDebugMode() |
|
459 | |||
460 | /** |
||
461 | * Return's the subject's execution context configuration. |
||
462 | * |
||
463 | * @return \TechDivision\Import\ExecutionContextInterface The execution context configuration to use |
||
464 | */ |
||
465 | public function getExecutionContext() |
||
469 | |||
470 | /** |
||
471 | * Set's the subject configuration. |
||
472 | * |
||
473 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration |
||
474 | * |
||
475 | * @return void |
||
476 | */ |
||
477 | 81 | public function setConfiguration(SubjectConfigurationInterface $configuration) |
|
481 | |||
482 | /** |
||
483 | * Return's the subject configuration. |
||
484 | * |
||
485 | * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
||
486 | */ |
||
487 | 81 | public function getConfiguration() |
|
491 | |||
492 | /** |
||
493 | * Set's the import adapter instance. |
||
494 | * |
||
495 | * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance |
||
496 | * |
||
497 | * @return void |
||
498 | */ |
||
499 | 1 | public function setImportAdapter(ImportAdapterInterface $importAdapter) |
|
503 | |||
504 | /** |
||
505 | * Return's the import adapter instance. |
||
506 | * |
||
507 | * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance |
||
508 | */ |
||
509 | 1 | public function getImportAdapter() |
|
513 | |||
514 | /** |
||
515 | * Return's the RegistryProcessor instance to handle the running threads. |
||
516 | * |
||
517 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
518 | */ |
||
519 | 81 | public function getRegistryProcessor() |
|
523 | |||
524 | /** |
||
525 | * Set's the unique serial for this import process. |
||
526 | * |
||
527 | * @param string $serial The unique serial |
||
528 | * |
||
529 | * @return void |
||
530 | */ |
||
531 | 8 | public function setSerial($serial) |
|
535 | |||
536 | /** |
||
537 | * Return's the unique serial for this import process. |
||
538 | * |
||
539 | * @return string The unique serial |
||
540 | */ |
||
541 | 1 | public function getSerial() |
|
545 | |||
546 | /** |
||
547 | * Merge's the passed status into the actual one. |
||
548 | * |
||
549 | * @param array $status The status to MergeBuilder |
||
550 | * |
||
551 | * @return void |
||
552 | */ |
||
553 | 4 | public function mergeStatus(array $status) |
|
557 | |||
558 | /** |
||
559 | * Retur's the actual status. |
||
560 | * |
||
561 | * @return array The actual status |
||
562 | */ |
||
563 | public function getStatus() |
||
567 | |||
568 | /** |
||
569 | * Return's the unique identifier for the actual invocation. |
||
570 | * |
||
571 | * @return string The unique identifier |
||
572 | */ |
||
573 | 4 | public function getUniqueId() |
|
577 | |||
578 | /** |
||
579 | * Return's the source date format to use. |
||
580 | * |
||
581 | * @return string The source date format |
||
582 | */ |
||
583 | public function getSourceDateFormat() |
||
587 | |||
588 | /** |
||
589 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
590 | * |
||
591 | * @return string The multiple field delimiter character |
||
592 | */ |
||
593 | 1 | public function getMultipleFieldDelimiter() |
|
597 | |||
598 | /** |
||
599 | * Return's the multiple value delimiter character to use, default value is comma (|). |
||
600 | * |
||
601 | * @return string The multiple value delimiter character |
||
602 | */ |
||
603 | 1 | public function getMultipleValueDelimiter() |
|
607 | |||
608 | /** |
||
609 | * Intializes the previously loaded global data for exactly one bunch. |
||
610 | * |
||
611 | * @param string $serial The serial of the actual import |
||
612 | * |
||
613 | * @return void |
||
614 | */ |
||
615 | 81 | public function setUp($serial) |
|
675 | |||
676 | /** |
||
677 | * Clean up the global data after importing the variants. |
||
678 | * |
||
679 | * @param string $serial The serial of the actual import |
||
680 | * |
||
681 | * @return void |
||
682 | */ |
||
683 | 1 | public function tearDown($serial) |
|
713 | |||
714 | /** |
||
715 | * Return's the target directory for the artefact export. |
||
716 | * |
||
717 | * @return string The target directory for the artefact export |
||
718 | */ |
||
719 | 1 | View Code Duplication | public function getTargetDir() |
733 | |||
734 | /** |
||
735 | * Register the passed observer with the specific type. |
||
736 | * |
||
737 | * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to register |
||
738 | * @param string $type The type to register the observer with |
||
739 | * |
||
740 | * @return void |
||
741 | */ |
||
742 | 6 | public function registerObserver(ObserverInterface $observer, $type) |
|
754 | |||
755 | /** |
||
756 | * Register the passed callback with the specific type. |
||
757 | * |
||
758 | * @param \TechDivision\Import\Callbacks\CallbackInterface $callback The subject to register the callbacks for |
||
759 | * @param string $type The type to register the callback with |
||
760 | * |
||
761 | * @return void |
||
762 | */ |
||
763 | 2 | public function registerCallback(CallbackInterface $callback, $type) |
|
775 | |||
776 | /** |
||
777 | * Return's the array with callbacks for the passed type. |
||
778 | * |
||
779 | * @param string $type The type of the callbacks to return |
||
780 | * |
||
781 | * @return array The callbacks |
||
782 | */ |
||
783 | 1 | public function getCallbacksByType($type) |
|
797 | |||
798 | /** |
||
799 | * Return's the array with the available observers. |
||
800 | * |
||
801 | * @return array The observers |
||
802 | */ |
||
803 | 81 | public function getObservers() |
|
807 | |||
808 | /** |
||
809 | * Return's the array with the available callbacks. |
||
810 | * |
||
811 | * @return array The callbacks |
||
812 | */ |
||
813 | 1 | public function getCallbacks() |
|
817 | |||
818 | /** |
||
819 | * Return's the callback mappings for this subject. |
||
820 | * |
||
821 | * @return array The array with the subject's callback mappings |
||
822 | */ |
||
823 | 2 | public function getCallbackMappings() |
|
827 | |||
828 | /** |
||
829 | * Imports the content of the file with the passed filename. |
||
830 | * |
||
831 | * |
||
832 | * @param string $serial The serial of the actual import |
||
833 | * @param string $filename The filename to process |
||
834 | * |
||
835 | * @return void |
||
836 | * @throws \Exception Is thrown, if the import can't be processed |
||
837 | */ |
||
838 | 2 | public function import($serial, $filename) |
|
839 | { |
||
840 | |||
841 | try { |
||
842 | // initialize the serial/filename |
||
843 | 2 | $this->setSerial($serial); |
|
844 | 2 | $this->setFilename($filename); |
|
845 | |||
846 | // invoke the events that has to be fired before the artfact will be processed |
||
847 | 2 | $this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_START, $this); |
|
848 | 2 | $this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_START), $this); |
|
849 | |||
850 | // load the system logger instance |
||
851 | 2 | $systemLogger = $this->getSystemLogger(); |
|
852 | |||
853 | // prepare the flag filenames |
||
854 | 2 | $inProgressFilename = sprintf('%s.inProgress', $filename); |
|
855 | 2 | $importedFilename = sprintf('%s.imported', $filename); |
|
856 | 2 | $failedFilename = sprintf('%s.failed', $filename); |
|
857 | |||
858 | // query whether or not the file has already been imported |
||
859 | 2 | if ($this->isFile($failedFilename) || |
|
860 | 1 | $this->isFile($importedFilename) || |
|
861 | 2 | $this->isFile($inProgressFilename) |
|
862 | ) { |
||
863 | // log a debug message and exit |
||
864 | 1 | $systemLogger->debug(sprintf('Import running, found inProgress file "%s"', $inProgressFilename)); |
|
865 | 1 | return; |
|
866 | } |
||
867 | |||
868 | // flag file as in progress |
||
869 | 1 | $this->touch($inProgressFilename); |
|
870 | |||
871 | // track the start time |
||
872 | 1 | $startTime = microtime(true); |
|
873 | |||
874 | // initialize the last time we've logged the counter with the processed rows per minute |
||
875 | 1 | $this->lastLog = time(); |
|
876 | |||
877 | // log a message that the file has to be imported |
||
878 | 1 | $systemLogger->info( |
|
879 | 1 | sprintf('Now start processing file "%s"', basename($filename)), |
|
880 | 1 | array('operation-name' => $operationName = $this->getFullOperationName()) |
|
881 | ); |
||
882 | |||
883 | // let the adapter process the file |
||
884 | 1 | $this->getImportAdapter()->import(array($this, 'importRow'), $filename); |
|
885 | |||
886 | // track the time needed for the import in seconds |
||
887 | 1 | $endTime = microtime(true) - $startTime; |
|
888 | |||
889 | // log a notice that the file has successfully been imported, |
||
890 | // but also render the number of lines that have been skipped |
||
891 | 1 | $systemLogger->notice( |
|
892 | 1 | sprintf( |
|
893 | 1 | 'Successfully processed file "%s" with "%d" lines (skipped "%d") in "%f" s', |
|
894 | 1 | basename($filename), |
|
895 | 1 | $this->lineNumber - 1, |
|
896 | 1 | $this->skippedRows, |
|
897 | $endTime |
||
898 | ), |
||
899 | 1 | array('operation-name' => $operationName) |
|
900 | ); |
||
901 | |||
902 | // rename flag file, because import has been successfull |
||
903 | 1 | if ($this->getConfiguration()->isCreatingImportedFile()) { |
|
904 | 1 | $this->rename($inProgressFilename, $importedFilename); |
|
905 | } else { |
||
906 | $this->getFilesystemAdapter()->delete($inProgressFilename); |
||
907 | } |
||
908 | |||
909 | // update the status |
||
910 | 1 | $this->mergeStatus( |
|
911 | array( |
||
912 | RegistryKeys::STATUS => array( |
||
913 | RegistryKeys::FILES => array( |
||
914 | $filename => array( |
||
915 | 1 | $this->getUniqueId() => array( |
|
916 | 1 | RegistryKeys::STATUS => 1, |
|
917 | 1 | RegistryKeys::PROCESSED_ROWS => $this->getLineNumber() |
|
918 | ) |
||
919 | ) |
||
920 | ) |
||
921 | ) |
||
922 | ) |
||
923 | ); |
||
924 | |||
925 | // invoke the events that has to be fired when the artfact has been successfully processed |
||
926 | 1 | $this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_SUCCESS, $this); |
|
927 | 1 | $this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_SUCCESS), $this); |
|
928 | } catch (\Exception $e) { |
||
929 | // rename the flag file, because import failed and write the stack trace |
||
930 | $this->rename($inProgressFilename, $failedFilename); |
||
931 | $this->write($failedFilename, $e->__toString()); |
||
932 | |||
933 | // update the status with the error message |
||
934 | $this->mergeStatus( |
||
935 | array( |
||
936 | RegistryKeys::STATUS => array( |
||
937 | RegistryKeys::FILES => array( |
||
938 | $filename => array( |
||
939 | $this->getUniqueId() => array( |
||
940 | RegistryKeys::STATUS => 2, |
||
941 | RegistryKeys::ERROR_MESSAGE => $e->getMessage(), |
||
942 | RegistryKeys::PROCESSED_ROWS => $this->getLineNumber() |
||
943 | ) |
||
944 | ) |
||
945 | ) |
||
946 | ) |
||
947 | ) |
||
948 | ); |
||
949 | |||
950 | // invoke the events that has to be fired when the artfact can't be processed |
||
951 | $this->getEmitter()->emit(EventNames::SUBJECT_ARTEFACT_PROCESS_FAILURE, $this, $e); |
||
952 | $this->getEmitter()->emit($this->getEventName(EventNames::SUBJECT_ARTEFACT_PROCESS_FAILURE), $this, $e); |
||
953 | |||
954 | // do not wrap the exception if not already done |
||
955 | if ($e instanceof WrappedColumnException) { |
||
956 | throw $e; |
||
957 | } |
||
958 | |||
959 | // else wrap and throw the exception |
||
960 | throw $this->wrapException(array(), $e); |
||
961 | } |
||
962 | 1 | } |
|
963 | |||
964 | /** |
||
965 | * Imports the passed row into the database. If the import failed, the exception |
||
966 | * will be catched and logged, but the import process will be continued. |
||
967 | * |
||
968 | * @param array $row The row with the data to be imported |
||
969 | * |
||
970 | * @return void |
||
971 | */ |
||
972 | 7 | public function importRow(array $row) |
|
1080 | |||
1081 | /** |
||
1082 | * Queries whether or not that the subject needs an OK file to be processed. |
||
1083 | * |
||
1084 | * @return boolean TRUE if the subject needs an OK file, else FALSE |
||
1085 | */ |
||
1086 | 1 | public function isOkFileNeeded() |
|
1090 | |||
1091 | /** |
||
1092 | * Return's the default store. |
||
1093 | * |
||
1094 | * @return array The default store |
||
1095 | */ |
||
1096 | public function getDefaultStore() |
||
1100 | |||
1101 | /** |
||
1102 | * Return's the default store view code. |
||
1103 | * |
||
1104 | * @return array The default store view code |
||
1105 | */ |
||
1106 | 5 | public function getDefaultStoreViewCode() |
|
1110 | |||
1111 | /** |
||
1112 | * Set's the store view code the create the product/attributes for. |
||
1113 | * |
||
1114 | * @param string $storeViewCode The store view code |
||
1115 | * |
||
1116 | * @return void |
||
1117 | */ |
||
1118 | 4 | public function setStoreViewCode($storeViewCode) |
|
1122 | |||
1123 | /** |
||
1124 | * Return's the store view code the create the product/attributes for. |
||
1125 | * |
||
1126 | * @param string|null $default The default value to return, if the store view code has not been set |
||
1127 | * |
||
1128 | * @return string The store view code |
||
1129 | */ |
||
1130 | 8 | public function getStoreViewCode($default = null) |
|
1147 | |||
1148 | /** |
||
1149 | * Prepare's the store view code in the subject. If the store_view_code row doesn't contain |
||
1150 | * any value, the default code of the default store view will be set. |
||
1151 | * |
||
1152 | * @return void |
||
1153 | */ |
||
1154 | 2 | public function prepareStoreViewCode() |
|
1165 | |||
1166 | /** |
||
1167 | * Return's the store ID of the store with the passed store view code |
||
1168 | * |
||
1169 | * @param string $storeViewCode The store view code to return the store ID for |
||
1170 | * |
||
1171 | * @return integer The ID of the store with the passed ID |
||
1172 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
1173 | */ |
||
1174 | 4 | public function getStoreId($storeViewCode) |
|
1175 | { |
||
1176 | |||
1177 | // query whether or not, the requested store is available |
||
1178 | 4 | if (isset($this->stores[$storeViewCode])) { |
|
1179 | 3 | return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID]; |
|
1180 | } |
||
1181 | |||
1182 | // throw an exception, if not |
||
1183 | 1 | throw new \Exception( |
|
1184 | 1 | sprintf( |
|
1185 | 1 | 'Found invalid store view code %s in file %s on line %d', |
|
1186 | $storeViewCode, |
||
1187 | 1 | $this->getFilename(), |
|
1188 | 1 | $this->getLineNumber() |
|
1189 | ) |
||
1190 | ); |
||
1191 | } |
||
1192 | |||
1193 | /** |
||
1194 | * Return's the store ID of the actual row, or of the default store |
||
1195 | * if no store view code is set in the CSV file. |
||
1196 | * |
||
1197 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
1198 | * |
||
1199 | * @return integer The ID of the actual store |
||
1200 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
1201 | */ |
||
1202 | 2 | public function getRowStoreId($default = null) |
|
1213 | |||
1214 | /** |
||
1215 | * Return's the root category for the actual view store. |
||
1216 | * |
||
1217 | * @return array The store's root category |
||
1218 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
1219 | */ |
||
1220 | 2 | public function getRootCategory() |
|
1234 | |||
1235 | /** |
||
1236 | * Return's the Magento configuration value. |
||
1237 | * |
||
1238 | * @param string $path The Magento path of the requested configuration value |
||
1239 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
1240 | * @param string $scope The scope the configuration value has been set |
||
1241 | * @param integer $scopeId The scope ID the configuration value has been set |
||
1242 | * |
||
1243 | * @return mixed The configuration value |
||
1244 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
1245 | */ |
||
1246 | 5 | public function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
|
1247 | { |
||
1248 | |||
1249 | // initialize the core config data |
||
1250 | $coreConfigData = array( |
||
1251 | 5 | MemberNames::PATH => $path, |
|
1252 | 5 | MemberNames::SCOPE => $scope, |
|
1253 | 5 | MemberNames::SCOPE_ID => $scopeId |
|
1254 | ); |
||
1255 | |||
1256 | // generate the UID from the passed data |
||
1257 | 5 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
1258 | |||
1259 | // iterate over the core config data and try to find the requested configuration value |
||
1260 | 5 | if (isset($this->coreConfigData[$uniqueIdentifier])) { |
|
1261 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
1262 | } |
||
1263 | |||
1264 | // query whether or not we've to query for the configuration value on fallback level 'websites' also |
||
1265 | 4 | if ($scope === ScopeKeys::SCOPE_STORES) { |
|
1266 | // query whether or not the website with the passed ID is available |
||
1267 | 2 | foreach ($this->storeWebsites as $storeWebsite) { |
|
1268 | 2 | if ($storeWebsite[MemberNames::WEBSITE_ID] === $scopeId) { |
|
1269 | // replace scope with 'websites' and website ID |
||
1270 | 2 | $coreConfigData = array_merge( |
|
1271 | 2 | $coreConfigData, |
|
1272 | array( |
||
1273 | 2 | MemberNames::SCOPE => ScopeKeys::SCOPE_WEBSITES, |
|
1274 | MemberNames::SCOPE_ID => $storeWebsite[MemberNames::WEBSITE_ID] |
||
1275 | ) |
||
1276 | ); |
||
1277 | |||
1278 | // generate the UID from the passed data, merged with the 'websites' scope and ID |
||
1279 | 2 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
1280 | |||
1281 | // query whether or not, the configuration value on 'websites' level |
||
1282 | 2 | View Code Duplication | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
1283 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
1284 | } |
||
1285 | } |
||
1286 | } |
||
1287 | } |
||
1288 | |||
1289 | // replace scope with 'default' and scope ID '0' |
||
1290 | 3 | $coreConfigData = array_merge( |
|
1291 | 3 | $coreConfigData, |
|
1292 | array( |
||
1293 | 3 | MemberNames::SCOPE => ScopeKeys::SCOPE_DEFAULT, |
|
1294 | MemberNames::SCOPE_ID => 0 |
||
1295 | ) |
||
1296 | ); |
||
1297 | |||
1298 | // generate the UID from the passed data, merged with the 'default' scope and ID 0 |
||
1299 | 3 | $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); |
|
1300 | |||
1301 | // query whether or not, the configuration value on 'default' level |
||
1302 | 3 | View Code Duplication | if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { |
1303 | 1 | return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; |
|
1304 | } |
||
1305 | |||
1306 | // if not, return the passed default value |
||
1307 | 2 | if ($default !== null) { |
|
1308 | 1 | return $default; |
|
1309 | } |
||
1310 | |||
1311 | // throw an exception if no value can be found |
||
1312 | // in the Magento configuration |
||
1313 | 1 | throw new \Exception( |
|
1314 | 1 | sprintf( |
|
1315 | 1 | 'Can\'t find a value for configuration "%s-%s-%d" in "core_config_data"', |
|
1316 | $path, |
||
1317 | $scope, |
||
1318 | $scopeId |
||
1319 | ) |
||
1320 | ); |
||
1321 | } |
||
1322 | |||
1323 | /** |
||
1324 | * Resolve the original column name for the passed one. |
||
1325 | * |
||
1326 | * @param string $columnName The column name that has to be resolved |
||
1327 | * |
||
1328 | * @return string|null The original column name |
||
1329 | */ |
||
1330 | 2 | public function resolveOriginalColumnName($columnName) |
|
1352 | |||
1353 | /** |
||
1354 | * Return's the original data if available, or an empty array. |
||
1355 | * |
||
1356 | * @return array The original data |
||
1357 | */ |
||
1358 | 2 | public function getOriginalData() |
|
1373 | |||
1374 | /** |
||
1375 | * Query's whether or not the actual column contains original data like |
||
1376 | * filename, line number and column names. |
||
1377 | * |
||
1378 | * @return boolean TRUE if the actual column contains origin data, else FALSE |
||
1379 | */ |
||
1380 | 2 | public function hasOriginalData() |
|
1384 | |||
1385 | /** |
||
1386 | * Wraps the passed exeception into a new one by trying to resolve the original filname, |
||
1387 | * line number and column names and use it for a detailed exception message. |
||
1388 | * |
||
1389 | * @param array $columnNames The column names that should be resolved and wrapped |
||
1390 | * @param \Exception $parent The exception we want to wrap |
||
1391 | * @param string $className The class name of the exception type we want to wrap the parent one |
||
1392 | * |
||
1393 | * @return \Exception the wrapped exception |
||
1394 | */ |
||
1395 | 1 | public function wrapException( |
|
1439 | |||
1440 | /** |
||
1441 | * Strip's the exception suffix containing filename and line number from the |
||
1442 | * passed message. |
||
1443 | * |
||
1444 | * @param string $message The message to strip the exception suffix from |
||
1445 | * |
||
1446 | * @return mixed The message without the exception suffix |
||
1447 | */ |
||
1448 | 1 | public function stripExceptionSuffix($message) |
|
1452 | |||
1453 | /** |
||
1454 | * Append's the exception suffix containing filename and line number to the |
||
1455 | * passed message. If no message has been passed, only the suffix will be |
||
1456 | * returned |
||
1457 | * |
||
1458 | * @param string|null $message The message to append the exception suffix to |
||
1459 | * @param string|null $filename The filename used to create the suffix |
||
1460 | * @param string|null $lineNumber The line number used to create the suffx |
||
1461 | * |
||
1462 | * @return string The message with the appended exception suffix |
||
1463 | */ |
||
1464 | 12 | public function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null) |
|
1485 | |||
1486 | /** |
||
1487 | * Raises the value for the counter with the passed key by one. |
||
1488 | * |
||
1489 | * @param mixed $counterName The name of the counter to raise |
||
1490 | * |
||
1491 | * @return integer The counter's new value |
||
1492 | */ |
||
1493 | 1 | public function raiseCounter($counterName) |
|
1494 | { |
||
1495 | |||
1496 | // raise the counter with the passed name |
||
1497 | 1 | return $this->getRegistryProcessor()->raiseCounter( |
|
1498 | 1 | RegistryKeys::COUNTERS, |
|
1499 | $counterName |
||
1500 | ); |
||
1501 | } |
||
1502 | |||
1503 | /** |
||
1504 | * Merge the passed array into the status of the actual import. |
||
1505 | * |
||
1506 | * @param array $status The status information to be merged |
||
1507 | * |
||
1508 | * @return void |
||
1509 | */ |
||
1510 | 1 | public function mergeAttributesRecursive(array $status) |
|
1511 | { |
||
1512 | |||
1513 | // merge the passed status |
||
1514 | 1 | return $this->getRegistryProcessor()->mergeAttributesRecursive( |
|
1515 | 1 | RegistryKeys::STATUS, |
|
1516 | $status |
||
1517 | ); |
||
1518 | } |
||
1519 | |||
1520 | /** |
||
1521 | * Return's the entity type code to be used. |
||
1522 | * |
||
1523 | * @return string The entity type code to be used |
||
1524 | */ |
||
1525 | public function getEntityTypeCode() |
||
1539 | |||
1540 | /** |
||
1541 | * Concatenates and returns the event name for the actual plugin and subject context. |
||
1542 | * |
||
1543 | * @param string $eventName The event name to concatenate |
||
1544 | * |
||
1545 | * @return string The concatenated event name |
||
1546 | */ |
||
1547 | 9 | protected function getEventName($eventName) |
|
1548 | { |
||
1549 | 9 | return sprintf( |
|
1550 | 9 | '%s.%s.%s', |
|
1551 | 9 | $this->getConfiguration()->getPluginConfiguration()->getId(), |
|
1552 | 9 | $this->getConfiguration()->getId(), |
|
1553 | $eventName |
||
1554 | ); |
||
1555 | } |
||
1556 | |||
1557 | /** |
||
1558 | * Return's the full opration name, which consists of the Magento edition, the entity type code and the operation name. |
||
1559 | * |
||
1560 | * @param string $separator The separator used to seperate the elements |
||
1561 | * |
||
1562 | * @return string The full operation name |
||
1563 | */ |
||
1564 | public function getFullOperationName($separator = '/') |
||
1568 | } |
||
1569 |
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..