| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Stockbase\Integration\Model\Observer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Magento\Framework\Event\ObserverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Magento\Framework\Event\Observer as EventObserver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Stockbase\Integration\Model\Config\StockbaseConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Stockbase\Integration\Helper\Images as ImagesHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class ProductSaveAfterObserver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ProductSaveAfterObserver implements ObserverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var StockbaseConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var ImagesHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $imagesHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var StockbaseClientFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $stockbaseClientFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * ProductSaveAfterObserver constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param StockbaseConfiguration $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param ImagesHelper $imagesHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param StockbaseClientFactory $stockbaseClientFactory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         LoggerInterface $logger, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         StockbaseConfiguration $config, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ImagesHelper $imagesHelper, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         StockbaseClientFactory $stockbaseClientFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->logger = $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->config = $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->imagesHelper = $imagesHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->stockbaseClientFactory = $stockbaseClientFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param EventObserver $observer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function execute(EventObserver $observer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         /** @var \Magento\Catalog\Model\Product */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $product = $observer->getEvent()->getProduct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // validate configuration: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // get ean attribute: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $attribute = $this->config->getEanFieldName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // validate attribute: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if($attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             // get ean: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $ean = $product->getData($attribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             // if the ean is not empty: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if ($ean) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $this->logger->debug('Image Sync in Product Save process. EAN found: '.$ean); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $client = $this->stockbaseClientFactory->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $images = $client->getImages([$ean]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 // validate returned images: | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 77 |  | View Code Duplication |                 if(is_array($images->{'Items'}) && count($images->{'Items'}) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     // download and save the images locally: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     $this->imagesHelper->saveProductImages($images->{'Items'}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     $this->logger->debug('New images synchronized.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                        
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.