| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * TechDivision\Import\Product\Observers\ProductObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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\Observers\AbstractProductImportObserver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Observer that create's the product itself. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @copyright 2016 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @link      https://github.com/techdivision/import-product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class ProductObserver extends AbstractProductImportObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Process the observer's business logic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return array The processed row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     protected function process() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // query whether or not, we've found a new SKU => means we've found a new product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         if ($this->isLastSku($this->getValue(ColumnKeys::SKU))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return $this->getRow(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // prepare the static entity values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         $product = $this->initializeProduct($this->prepareAttributes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // insert the entity and set the entity ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $this->setLastEntityId($this->persistProduct($product)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Prepare the attributes of the entity that has to be persisted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array The prepared attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     protected function prepareAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // prepare the date format for the created at/updated at dates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         // load the product's attribute set ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         $attributeSet = $this->getAttributeSetByAttributeSetName($this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         $attributeSetId = $attributeSet[MemberNames::ATTRIBUTE_SET_ID]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $this->setAttributeSet($attributeSet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // initialize the product values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $sku = $this->getValue(ColumnKeys::SKU); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $productType = $this->getValue(ColumnKeys::PRODUCT_TYPE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // return the prepared product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         return $this->initializeEntity( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                 MemberNames::SKU              => $sku, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |                 MemberNames::CREATED_AT       => $createdAt, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |                 MemberNames::UPDATED_AT       => $updatedAt, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |                 MemberNames::HAS_OPTIONS      => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                 MemberNames::REQUIRED_OPTIONS => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                 MemberNames::TYPE_ID          => $productType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |                 MemberNames::ATTRIBUTE_SET_ID => $attributeSetId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Initialize the product with the passed attributes and returns an instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param array $attr The product attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return array The initialized product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     protected function initializeProduct(array $attr) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $attr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Persist's the passed product data and return's the ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param array $product The product data to persist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return string The ID of the persisted entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |     protected function persistProduct($product) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         return $this->getSubject()->persistProduct($product); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Set's the attribute set of the product that has to be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array $attributeSet The attribute set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |     protected function setAttributeSet(array $attributeSet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         $this->getSubject()->setAttributeSet($attributeSet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Return's the attribute set of the product that has to be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return array The attribute set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     protected function getAttributeSet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->getSubject()->getAttributeSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * Return's the attribute set with the passed attribute set name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param string $attributeSetName The name of the requested attribute set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @return array The attribute set data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |     protected function getAttributeSetByAttributeSetName($attributeSetName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |         return $this->getSubject()->getAttributeSetByAttributeSetName($attributeSetName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * Set's the ID of the product that has been created recently. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @param string $lastEntityId The entity ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 159 | 1 |  |     protected function setLastEntityId($lastEntityId) | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 161 | 1 |  |         $this->getSubject()->setLastEntityId($lastEntityId); | 
            
                                                        
            
                                    
            
            
                | 162 | 1 |  |     } | 
            
                                                        
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  |