| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * TechDivision\Import\Product\Observers\GenericSkuEntityIdMappingObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * NOTICE OF LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This source file is subject to the Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * that is available through the world-wide-web at this URL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://opensource.org/licenses/osl-3.0.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * PHP version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @copyright 2019 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @link      https://github.com/techdivision/import-product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | namespace TechDivision\Import\Product\Observers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TechDivision\Import\Product\Utils\ColumnKeys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use TechDivision\Import\Product\Utils\MemberNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use TechDivision\Import\Product\Services\ProductBunchProcessorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * A generic oberserver implementation that provides functionality to add the SKU => entity ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * mapping for products that has not yet been processed or are NOT part of the actual CSV file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @copyright 2019 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @link      https://github.com/techdivision/import-product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | class GenericSkuEntityIdMappingObserver extends AbstractProductImportObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * The product bunch processor instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected $productBunchProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * The column name with the SKU to map the entity ID for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected $skuColumnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Initialize the observer with the passed product bunch processor instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string                                                               $skuColumnName         The column name with the SKU to map the entity ID for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ProductBunchProcessorInterface $productBunchProcessor, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $skuColumnName = ColumnKeys::SKU | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->productBunchProcessor = $productBunchProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->skuColumnName = $skuColumnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Return's the product bunch processor instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     protected function getProductBunchProcessor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->productBunchProcessor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Returns the column name with the SKU to map the entity ID for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return string The column name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function getSkuColumnName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->skuColumnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Process the observer's business logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws \Exception Is thrown, if the product with the SKU can not be loaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 94 |  | View Code Duplication |     protected function process() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // load the SKU from the column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $sku = $this->getValue($this->getSkuColumnName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // query whether or not the product has already been processed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($this->hasBeenProcessed($sku)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // try to load it and map the entity ID of the product with the passed SKU otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($product = $this->loadProduct($sku)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $this->addSkuPkMapping($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             // initialize the error message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $message = sprintf('Can\'t load product with SKU "%s"', $sku); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             // load the subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $subject = $this->getSubject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             // query whether or not debug mode has been enabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             if ($subject->isDebugMode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 throw new \Exception($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Map the PK for the product with the passed SKU. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param array $product The product to add the SKU => entity ID mapping for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     protected function addSkuPkMapping(array $product) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->addSkuEntityIdMapping($product[MemberNames::SKU], $product[MemberNames::ENTITY_ID]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Load's and return's the product with the passed SKU. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param string $sku The SKU of the product to load | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return array The product | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |     protected function loadProduct($sku) | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |         return $this->getProductBunchProcessor()->loadProduct($sku); | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.