Completed
Push — feature/evo-2472-whoami ( 843346...066c1c )
by
unknown
83:53 queued 67:03
created

WhoAmIController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
ccs 0
cts 23
cp 0
rs 9.0856
cc 1
eloc 20
nc 1
nop 9
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\Form\FormFactory;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Bundle\FrameworkBundle\Routing\Router;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
20
/**
21
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
22
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
23
 * @link     http://swisscom.ch
24
 */
25
class WhoAmIController extends RestController
26
{
27
    /**
28
     * @param Response           $response    Response
29
     * @param RestUtilsInterface $restUtils   Rest utils
30
     * @param Router             $router      Router
31
     * @param ValidatorInterface $validator   Validator
32
     * @param EngineInterface    $templating  Templating
33
     * @param FormFactory        $formFactory form factory
34
     * @param DocumentType       $formType    generic form
35
     * @param ContainerInterface $container   Container
36
     * @param SchemaUtils        $schemaUtils schema utils
37
     */
38 View Code Duplication
    public function __construct(
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...
39
        Response $response,
40
        RestUtilsInterface $restUtils,
41
        Router $router,
42
        ValidatorInterface $validator,
43
        EngineInterface $templating,
44
        FormFactory $formFactory,
45
        DocumentType $formType,
46
        ContainerInterface $container,
47
        SchemaUtils $schemaUtils
48
    ) {
49
        parent::__construct(
50
            $response,
51
            $restUtils,
52
            $router,
53
            $validator,
54
            $templating,
55
            $formFactory,
56
            $formType,
57
            $container,
58
            $schemaUtils
59
        );
60
    }
61
62
    /**
63
     * Return Consultant object
64
     *
65
     * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error
66
     */
67
    public function whoAmIAction()
68
    {
69
        if ('anonymous' !== $this->getUser()->getUsername()) {
70
            $user = $this->serialize($this->getUser());
71
        } else {
72
            $user = json_encode(['username' => $this->getUser()->getUsername()]);
73
        }
74
75
        $response = $this->getResponse()->setStatusCode(Response::HTTP_OK);
76
        $response->headers->set('Content-Type', 'application/json');
77
78
        return $this->render(
79
            'GravitonRestBundle:Main:index.json.twig',
80
            ['response' => $user],
81
            $response
82
        );
83
    }
84
85
    /**
86
     * Returns schema
87
     *
88
     * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error
89
     */
90 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...
91
    {
92
        $response = $this->getResponse()->setStatusCode(Response::HTTP_OK);
93
        $response->headers->set('Content-Type', 'application/json');
94
95
        $schema = $this->getModel()->getSchema();
96
        return $this->render(
97
            'GravitonRestBundle:Main:index.json.twig',
98
            ['response' => json_encode($schema)],
99
            $response
100
        );
101
    }
102
}
103