Completed
Pull Request — master (#164)
by Ihor
25:00 queued 13:13
created

ApiKeyAuthenticator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 12.5%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 18
loc 92
ccs 3
cts 24
cp 0.125
rs 10
wmc 9
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supportsRememberMe() 0 4 1
A getCredentials() 0 10 2
A getUser() 0 9 1
A checkCredentials() 0 4 1
A onAuthenticationSuccess() 0 4 1
A onAuthenticationFailure() 9 9 1
A start() 9 9 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
namespace AppBundle\Security;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
use Symfony\Component\Security\Core\Exception\AuthenticationException;
13
use Symfony\Component\Security\Core\User\UserProviderInterface;
14
15
class ApiKeyAuthenticator extends AbstractGuardAuthenticator
16
{
17
    /**
18
     * @var ManagerRegistry
19
     */
20
    private $registry;
21
22
    /**
23
     * @param ManagerRegistry $registry
24
     */
25 86
    public function __construct(ManagerRegistry $registry)
26
    {
27 86
        $this->registry = $registry;
28 86
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function getCredentials(Request $request)
34
    {
35
        if (!$token = $request->headers->get('API-Key-Token')) {
36
            return null;
37
        }
38
39
        return array(
40
            'token' => $token,
41
        );
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function getUser($credentials, UserProviderInterface $userProvider)
48
    {
49
        $apiKey = $credentials['token'];
50
51
        $user = $this->registry->getRepository('AppBundle:User')
52
            ->findOneBy(['apiKey' => $apiKey]);
53
54
        return $user;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function checkCredentials($credentials, UserInterface $user)
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
69
    {
70
        return null;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 View Code Duplication
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
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...
77
    {
78
        $data = [
79
            'code' => '403',
80
            'message' => 'Forbidden. You don\'t have necessary permissions for the resource'
81
        ];
82
83
        return new JsonResponse($data, Response::HTTP_FORBIDDEN);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 View Code Duplication
    public function start(Request $request, AuthenticationException $authException = null)
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...
90
    {
91
        $data = [
92
            'code' => '401',
93
            'message' => 'Authentication required'
94
        ];
95
96
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function supportsRememberMe()
103
    {
104
        return false;
105
    }
106
}
107