| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Controller for user/whoami endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Graviton\SecurityBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Graviton\DocumentBundle\Form\Type\DocumentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Graviton\RestBundle\Controller\RestController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Graviton\RestBundle\Service\RestUtilsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Graviton\SchemaBundle\SchemaUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Graviton\SecurityBundle\Entities\SecurityUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Form\FormFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Bundle\FrameworkBundle\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Validator\Validator\ValidatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class WhoAmIController extends RestController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Response           $response    Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param RestUtilsInterface $restUtils   Rest utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param Router             $router      Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param ValidatorInterface $validator   Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param EngineInterface    $templating  Templating | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param FormFactory        $formFactory form factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param DocumentType       $formType    generic form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param ContainerInterface $container   Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param SchemaUtils        $schemaUtils schema utils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 39 |  | View Code Duplication |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         Response $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         RestUtilsInterface $restUtils, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         Router $router, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ValidatorInterface $validator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         EngineInterface $templating, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         FormFactory $formFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         DocumentType $formType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ContainerInterface $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         SchemaUtils $schemaUtils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         parent::__construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $response, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $restUtils, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $router, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $validator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $templating, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $formFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $formType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $schemaUtils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Currently authenticated user information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * If security is not enabled then header will be Not Allowed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * If User not found using correct header Anonymous user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Serialised Object transformer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return Response $response Response with result or error | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public function whoAmIAction() | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         /** @var SecurityUser $securityUser */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $securityUser = $this->getSecurityUser(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         /** @var Response $response */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $response = $this->getResponse(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $response->headers->set('Content-Type', 'application/json'); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         if (!$securityUser) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             return $this->render( | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 'GravitonRestBundle:Main:index.json.twig', | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 ['response' => json_encode(['Security is not enabled'])], | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 $response->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         return $this->render( | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             'GravitonRestBundle:Main:index.json.twig', | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             ['response' => $this->serialize($securityUser->getUser())], | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             $response | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Returns schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return Response $response Response with result or error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 100 |  | View Code Duplication |     public function whoAmiSchemaAction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         /** @var Response $response */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $response = $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $response->headers->set('Content-Type', 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $schema = $this->getModel()->getSchema(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             'GravitonRestBundle:Main:index.json.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ['response' => json_encode($schema)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.