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

WhoAmIController::whoAmiSchemaAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 2
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