| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * TechDivision\Import\Product\Media\Observers\ProductMediaObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 2016 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-media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | namespace TechDivision\Import\Product\Media\Observers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TechDivision\Import\Product\Media\Utils\ColumnKeys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use TechDivision\Import\Product\Observers\AbstractProductImportObserver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Observer that extracts theproduct's media data from a CSV file to be added to media specifi CSV file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @copyright 2016 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @link      https://github.com/techdivision/import-product-media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | class ProductMediaObserver extends AbstractProductImportObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * The artefact type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     const ARTEFACT_TYPE = 'media'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * The default image label. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     const DEFAULT_IMAGE_LABEL = 'Image'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Process the observer's business logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return array The processed row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function process() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // initialize the array for the product media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $artefacts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         // load the store view code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // load the parent SKU from the row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $parentSku = $this->getValue(ColumnKeys::SKU); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // query whether or not, we've a base image | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 70 |  | View Code Duplication |         if ($baseImage = $this->getValue(ColumnKeys::BASE_IMAGE)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             // prepare and append the base image to the artefacts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $artefacts[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ColumnKeys::STORE_VIEW_CODE  => $storeViewCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 ColumnKeys::IMAGE_PARENT_SKU => $parentSku, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ColumnKeys::IMAGE_PATH       => $baseImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ColumnKeys::IMAGE_PATH_NEW   => $baseImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ColumnKeys::IMAGE_LABEL      => $this->hasValue(ColumnKeys::BASE_IMAGE_LABEL) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                                 $this->getValue(ColumnKeys::BASE_IMAGE_LABEL) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                                                 $this->getDefaultImageLabel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // query whether or not, we've a small image | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 84 |  | View Code Duplication |         if ($smallImage = $this->getValue(ColumnKeys::SMALL_IMAGE)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // prepare and append the small image to the artefacts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $artefacts[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 ColumnKeys::STORE_VIEW_CODE  => $storeViewCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 ColumnKeys::IMAGE_PARENT_SKU => $parentSku, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 ColumnKeys::IMAGE_PATH       => $smallImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 ColumnKeys::IMAGE_PATH_NEW   => $smallImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ColumnKeys::IMAGE_LABEL      => $this->hasValue(ColumnKeys::SMALL_IMAGE_LABEL) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                                                 $this->getValue(ColumnKeys::SMALL_IMAGE_LABEL) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                                                 $this->getDefaultImageLabel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // query whether or not, we've a small thumbnail | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 98 |  | View Code Duplication |         if ($thumbnailImage = $this->getValue(ColumnKeys::THUMBNAIL_IMAGE)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             // prepare and append the thumbnail image to the artefacts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $artefacts[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ColumnKeys::STORE_VIEW_CODE  => $storeViewCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 ColumnKeys::IMAGE_PARENT_SKU => $parentSku, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ColumnKeys::IMAGE_PATH       => $thumbnailImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 ColumnKeys::IMAGE_PATH_NEW   => $thumbnailImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 ColumnKeys::IMAGE_LABEL      => $this->hasValue(ColumnKeys::THUMBNAIL_IMAGE_LABEL) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                                                 $this->getValue(ColumnKeys::THUMBNAIL_IMAGE_LABEL) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                                                 $this->getDefaultImageLabel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         // query whether or not, we've additional images | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($additionalImages = $this->getValue(ColumnKeys::ADDITIONAL_IMAGES, null, array($this, 'explode'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             // expand the additional image labels, if available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $additionalImageLabels = $this->getValue(ColumnKeys::ADDITIONAL_IMAGE_LABELS, array(), array($this, 'explode')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             // initialize the images with the found values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             foreach ($additionalImages as $key => $additionalImage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 // prepare and append the additional image to the artefacts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $artefacts[] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     ColumnKeys::STORE_VIEW_CODE  => $storeViewCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     ColumnKeys::IMAGE_PARENT_SKU => $parentSku, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     ColumnKeys::IMAGE_PATH       => $additionalImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     ColumnKeys::IMAGE_PATH_NEW   => $additionalImage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     ColumnKeys::IMAGE_LABEL      => isset($additionalImageLabels[$key]) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                                                     $additionalImageLabels[$key] : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                                                     $this->getDefaultImageLabel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         // append the images to the subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->addArtefacts($artefacts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Return's the default image label. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return string The default image label | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     protected function getDefaultImageLabel() | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         return ProductMediaObserver::DEFAULT_IMAGE_LABEL; | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Add the passed product type artefacts to the product with the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * last entity ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param array $artefacts The product type artefacts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     protected function addArtefacts(array $artefacts) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->getSubject()->addArtefacts(ProductMediaObserver::ARTEFACT_TYPE, $artefacts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  | 
            
                        
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.