| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Actualys\Bundle\DrupalCommerceConnectorBundle\Item\DrupalItemStep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Akeneo\Bundle\BatchBundle\Event\InvalidItemEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Akeneo\Bundle\BatchBundle\Job\ExitStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Guzzle\Http\Exception\ClientErrorResponseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\EventDispatcher\EventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Akeneo\Bundle\BatchBundle\Event\EventInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class ProductWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 18 |  | View Code Duplication | class ProductWriter extends DrupalItemStep implements ItemWriterInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct(EventDispatcher $eventDispatcher) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->eventDispatcher = $eventDispatcher; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $this->productandler   = $eventDispatcher; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /**x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getConfigurationFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return parent::getConfigurationFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function write(array $items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         foreach ($items as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |           //file_put_contents('product.json', json_encode($item)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |               $this->webservice->sendProduct($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $event = new InvalidItemEvent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                   __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                   $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                   array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                   ['sku' => array_key_exists('sku', $item) ? $item['sku'] : 'NULL'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 // Logging file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $this->eventDispatcher->dispatch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                   EventInterface::INVALID_ITEM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                   $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 // Loggin Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $this->stepExecution->addWarning( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                   __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                   $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                   array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                   ['sku' => array_key_exists('sku', $item) ? $item['sku'] : 'NULL'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 /** @var ClientErrorResponseException  $e */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 if ($e->getResponse()->getStatusCode() <= 404) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $e = new \Exception($e->getResponse()->getReasonPhrase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $this->stepExecution->addFailureException($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $exitStatus = new ExitStatus(ExitStatus::FAILED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     $this->stepExecution->setExitStatus($exitStatus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 // Handle next element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->stepExecution->incrementSummaryInfo('write'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->stepExecution->incrementWriteCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
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.