| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the "elao/enum" package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) Elao | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Elao <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Elao\Enum\Bridge\Symfony\Form\DataTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Elao\Enum\EnumInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Elao\Enum\Exception\InvalidValueException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Form\DataTransformerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Form\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Form\Exception\TransformationFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ScalarToEnumTransformer implements DataTransformerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var string|EnumInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $enumClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 24 |  | View Code Duplication |     public function __construct(string $enumClass) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (!is_a($enumClass, EnumInterface::class, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             throw new InvalidArgumentException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 '"%s" is not an instance of "%s"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 $enumClass, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 EnumInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->enumClass = $enumClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Transforms EnumInterface object to a scalar value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param EnumInterface $value EnumInterface instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws TransformationFailedException When the transformation fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return int|string Value of EnumInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication |     public function transform($value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ($value === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (!$value instanceof $this->enumClass) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             throw new TransformationFailedException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 'Expected instance of "%s". Got "%s".', | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 $this->enumClass, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 is_object($value) ? get_class($value) : gettype($value) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $value->getValue(); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Transforms scalar enum value to enumeration instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param int|string $value Value accepted by EnumInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws TransformationFailedException When the transformation fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return EnumInterface|null A single FlaggedEnum instance or null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function reverseTransform($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($value === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return $this->enumClass::get($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } catch (InvalidValueException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             throw new TransformationFailedException($exception->getMessage(), $exception->getCode(), $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                        
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.