Completed
Push — feature/other-validation ( 994f75...586e28 )
by Narcotic
10:05
created

WhoAmIController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 24.07 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 5
dl 13
loc 54
ccs 0
cts 29
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A whoAmIAction() 0 23 2
A whoAmiSchemaAction() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
30
     * Currently authenticated user information.
31
     * If security is not enabled then header will be Not Allowed.
32
     * If User not found using correct header Anonymous user
33
     * Serialised Object transformer
34
     *
35
     * @return Response $response Response with result or error
36
     */
37
    public function whoAmIAction()
38
    {
39
        /** @var SecurityUser $securityUser */
40
        $securityUser = $this->getSecurityUser();
41
42
        /** @var Response $response */
43
        $response = $this->getResponse();
44
        $response->headers->set('Content-Type', 'application/json');
45
46
        if (!$securityUser) {
47
            return $this->render(
48
                'GravitonRestBundle:Main:index.json.twig',
49
                ['response' => json_encode(['Security is not enabled'])],
50
                $response->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED)
51
            );
52
        }
53
54
        return $this->render(
55
            'GravitonRestBundle:Main:index.json.twig',
56
            ['response' => $this->serialize($securityUser->getUser())],
57
            $response
58
        );
59
    }
60
61
    /**
62
     * Returns schema
63
     *
64
     * @return Response $response Response with result or error
65
     */
66 View Code Duplication
    public function whoAmiSchemaAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
67
    {
68
        /** @var Response $response */
69
        $response = $this->getResponse();
70
        $response->headers->set('Content-Type', 'application/json');
71
72
        $schema = $this->getModel()->getSchema();
73
        return $this->render(
74
            'GravitonRestBundle:Main:index.json.twig',
75
            ['response' => json_encode($schema)],
76
            $response
77
        );
78
    }
79
}
80