GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ApiKeyController::processAuthentication()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
cc 2
eloc 12
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Controller;
4
5
use Ma27\ApiKeyAuthenticationBundle\Event\OnAssembleResponseEvent;
6
use Ma27\ApiKeyAuthenticationBundle\Event\OnCredentialExceptionThrownEvent;
7
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException;
8
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents;
9
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\HttpException;
15
16
/**
17
 * Controller which is responsible for the authentication routes.
18
 */
19
class ApiKeyController extends Controller
20
{
21
    /**
22
     * Requests an api key.
23
     *
24
     * @param Request $request
25
     *
26
     * @throws HttpException If the login fails.
27
     *
28
     * @return JsonResponse
29
     */
30 12
    public function requestApiKeyAction(Request $request)
31
    {
32
        /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
33 12
        $dispatcher = $this->get('event_dispatcher');
34
35 12
        $credentials = [];
36 12
        if ($request->request->has('login')) {
37 12
            $credentials[$this->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login');
38
        }
39 12
        if ($request->request->has('password')) {
40 12
            $credentials[$this->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password');
41
        }
42 12
        [$user, $exception] = $this->processAuthentication($credentials);
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $exception does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
44
        /** @var OnAssembleResponseEvent $result */
45 12
        $result = $dispatcher->dispatch(
46 12
            Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE,
47 12
            new OnAssembleResponseEvent($user, $exception)
48
        );
49
50 12
        if (!$response = $result->getResponse()) {
51
            throw new HttpException(
52
                Response::HTTP_INTERNAL_SERVER_ERROR,
53
                'Cannot assemble the response!',
54
                $exception
55
            );
56
        }
57
58 12
        return $response;
59
    }
60
61
    /**
62
     * Removes an api key.
63
     *
64
     * @param Request $request
65
     *
66
     * @return JsonResponse
67
     */
68 4
    public function removeSessionAction(Request $request)
69
    {
70
        /** @var \Doctrine\Common\Persistence\ObjectManager $om */
71 4
        $om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager'));
72
73 4
        if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) {
74 2
            return new JsonResponse(['message' => 'Missing api key header!'], 400);
75
        }
76
77
        $user = $om
78 2
            ->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name'))
79 2
            ->findOneBy([
80 2
                $this->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $header,
81
            ]);
82
83 2
        $this->get('ma27_api_key_authentication.auth_handler')->removeSession($user);
84
85 2
        return new JsonResponse([], 204);
86
    }
87
88
    /**
89
     * Internal utility to handle the authentication process based on the credentials.
90
     *
91
     * @param array $credentials
92
     *
93
     * @return array
94
     */
95 12
    private function processAuthentication(array $credentials)
96
    {
97
        /** @var \Ma27\ApiKeyAuthenticationBundle\Service\Auth\AuthenticationHandlerInterface $authenticationHandler */
98 12
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
99
        /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
100 12
        $dispatcher = $this->get('event_dispatcher');
101
102
        try {
103 12
            $user = $authenticationHandler->authenticate($credentials);
104 4
        } catch (CredentialException $ex) {
105 4
            $userOrNull = $user ?? null;
106 4
            $dispatcher->dispatch(
107 4
                Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN,
108 4
                new OnCredentialExceptionThrownEvent($ex, $userOrNull)
109
            );
110
111 4
            return [$userOrNull, $ex];
112
        }
113
114 8
        return [$user, null];
115
    }
116
117
    /**
118
     * Returns the actual property name by the given metadata alias.
119
     *
120
     * @param string $internalMetadataAlias
121
     *
122
     * @return string
123
     */
124 12
    private function getPropertyName($internalMetadataAlias)
125
    {
126 12
        return $this->get('ma27_api_key_authentication.class_metadata')->getPropertyName($internalMetadataAlias);
127
    }
128
}
129