| Conditions | 10 | 
| Paths | 12 | 
| Total Lines | 85 | 
| Code Lines | 52 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 87 | public function saveProductImages($images) | ||
| 88 |     { | ||
| 89 |         $this->logger->debug('Save images process:'); | ||
| 90 | $newImagesCount = 0; | ||
| 91 | // get product model: | ||
| 92 | $productModel = $this->product; | ||
| 93 | // get ean attribute: | ||
| 94 | $eanField = $this->config->getEanFieldName(); | ||
| 95 | // loop images: | ||
| 96 |         foreach ($images as $image) { | ||
| 97 |             $this->logger->debug('Image URL: '.$image->{'Url'}.' - EAN: '.$image->EAN); | ||
| 98 | // load product by ean: | ||
| 99 | $product = $productModel->loadByAttribute($eanField, $image->EAN); | ||
| 100 | // continue looping if we do not have product: | ||
| 101 |             if (!$product) { | ||
| 102 |                 $this->logger->debug('Product not found for EAN: '.$image->EAN); | ||
| 103 | continue; | ||
| 104 | } | ||
| 105 |             $this->logger->debug('Loaded product: '.$product->getId()); | ||
| 106 |             if(!$product->getData('stockbase_product')) { | ||
| 107 |                 $this->logger->debug('The Product is not mark as Stockbase product: '.$product->getId()); | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | // stockbase image: | ||
| 111 |             $stockbaseImage = (string)$image->{'Url'}; | ||
| 112 | // image name: | ||
| 113 |             $imageName = baseName($image->{'Url'}); | ||
| 114 | // check if the image exists: | ||
| 115 | $imageCollection = $this->productImageResource->imageExists($imageName, $product->getId(), $image->EAN); | ||
| 116 |             if(count($imageCollection)>0) { | ||
| 117 |                 $this->logger->debug('The image '.$imageName.' is already synchronized for product '.$product->getId()); | ||
| 118 | continue; | ||
| 119 | } | ||
| 120 | // create temporal folder if it is not exists: | ||
| 121 | $tmpDir = $this->getMediaDirTmpDir(); | ||
| 122 | $this->file->checkAndCreateFolder($tmpDir); | ||
| 123 | // get new file path: | ||
| 124 | $newFileName = $tmpDir . $imageName; | ||
| 125 | // if the image file is not in our system: | ||
| 126 |             if(!file_exists($newFileName)) { | ||
| 127 | // read file from URL and copy it to the new destination: | ||
| 128 | $this->file->read($stockbaseImage, $newFileName); | ||
| 129 |                 $this->logger->debug('New image saved: '. $newFileName); | ||
| 130 | } | ||
| 131 | // if the process worked then the file should be there now: | ||
| 132 |             if (file_exists($newFileName)) { | ||
| 133 | // if product gallery is empty the set the default values: | ||
| 134 | $mediaGallery = $product->getMediaGallery(); | ||
| 135 |                 if (!$mediaGallery) { | ||
| 136 |                     $product->setMediaGallery(array('images' => array(), 'values' => array())); | ||
| 137 |                 } else { | ||
| 138 | // if the product has a gallery and the image is already there then continue with the next one: | ||
| 139 |                     if(is_array($mediaGallery['images']) && in_array($imageName, $mediaGallery['images'])) { | ||
| 140 | continue; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | // add saved file into the product gallery: | ||
| 144 | $product->addImageToMediaGallery( | ||
| 145 | $newFileName, | ||
| 146 |                     array('image', 'small_image', 'thumbnail'), | ||
| 147 | false, | ||
| 148 | false | ||
| 149 | ); | ||
| 150 | // save product: | ||
| 151 | $product->save(); | ||
| 152 | // save image in relation table: | ||
| 153 | $imageModel = clone($this->productImage); | ||
| 154 | // image data: | ||
| 155 | $imageData = [ | ||
| 156 | 'ean' => $image->EAN, | ||
| 157 | 'product_id' => $product->getId(), | ||
| 158 | 'image' => $imageName, | ||
| 159 |                     'timestamp' => date('Y-m-d H:i:s') | ||
| 160 | ]; | ||
| 161 | // save: | ||
| 162 | $imageModel->setData($imageData)->save(); | ||
| 163 | // end process. | ||
| 164 |                 $this->logger->debug('Product saved.'); | ||
| 165 | $newImagesCount++; | ||
| 166 |             } else { | ||
| 167 |                 $this->logger->debug('There is an issue with the image: '.$newFileName); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | return $newImagesCount; | ||
| 171 | } | ||
| 172 | |||
| 185 |