| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Pim\Bundle\CatalogBundle\Entity\Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class PimCatalogPriceCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 13 |  | View Code Duplication | class PimCatalogPriceCollectionNormalizer implements NormalizerInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param  object                                                $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param  null                                                  $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param  array                                                 $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return array|\Symfony\Component\Serializer\Normalizer\scalar | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public function normalize($attribute, $format = null, array $context = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         /**@var Attribute $attribute * */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $normalizedAttribute       = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |           'code'       => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |           'type'       => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |           'required'   => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |           'labels'     => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |           'parameters' => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $availableLocales          = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $attributeAvailableLocales = $attribute->getAvailableLocales(); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         if (!is_null($attributeAvailableLocales)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             foreach ($attribute->getAvailableLocales() as $availableLocale) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 $availableLocales [] = $availableLocale; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $normalizedAttribute['required']   = $attribute->isRequired(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $normalizedAttribute['type']       = $attribute->getAttributeType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $normalizedAttribute['code']       = $attribute->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $normalizedAttribute['parameters'] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |           'scope'             => $attribute->isScopable(), | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |           'localizable'       => $attribute->isLocalizable(), | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |           'available_locales' => $availableLocales, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ($attribute->isLocalizable()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             foreach ($attribute->getTranslations() as $trans) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $normalizedAttribute['labels'][$trans->getLocale( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 )] = $trans->getLabel(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel( | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return $normalizedAttribute; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Checks whether the given class is supported for normalization by this normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param mixed  $data   Data to normalize. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $format The format being (de-)serialized from or into. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function supportsNormalization($data, $format = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $data instanceof Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                        
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.