| 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 Symfony\Component\EventDispatcher\EventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Akeneo\Bundle\BatchBundle\Event\EventInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 11 |  | View Code Duplication | class CategoryWriter extends DrupalItemStep implements ItemWriterInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected $eventDispatcher; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function __construct(EventDispatcher $eventDispatcher) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function write(array $items) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         //file_put_contents('categories.json', json_encode($items)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         foreach ($items[0] as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                  $this->webservice->sendCategory($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 $event = new InvalidItemEvent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                   __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                   $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                   array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                   ['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 // Logging file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 $this->eventDispatcher->dispatch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                   EventInterface::INVALID_ITEM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                   $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 // Loggin Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $this->stepExecution->addWarning( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                   __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                   $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                   array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                   ['code' => array_key_exists('code', $item) ? $item['code'] : 'NULL'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 /** @var ClientErrorResponseException  $e */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 if ($e->getResponse()->getStatusCode() <= 404) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $e = new \Exception($e->getResponse()->getReasonPhrase()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     $this->stepExecution->addFailureException($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     $exitStatus = new ExitStatus(ExitStatus::FAILED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     $this->stepExecution->setExitStatus($exitStatus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 // Handle next element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->stepExecution->incrementSummaryInfo('write'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->stepExecution->incrementWriteCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                        
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.