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.
Completed
Branch develop (4efcd8)
by Maximilian
04:45
created

ApiKeyController::requestApiKeyAction()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5.0026

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 36
ccs 20
cts 21
cp 0.9524
rs 8.439
cc 5
eloc 20
nc 16
nop 1
crap 5.0026
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\Model\User\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 6
    public function requestApiKeyAction(Request $request)
31
    {
32
        /** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */
33 6
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
34
        /** @var ClassMetadata $metadata */
35 6
        $metadata = $this->get('ma27_api_key_authentication.class_metadata');
36
        /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
37 6
        $dispatcher = $this->get('event_dispatcher');
38
39 6
        $credentials = array();
40 6
        if ($request->request->has('login')) {
41 6
            $credentials[$metadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login');
42 6
        }
43
44 6
        if ($request->request->has('password')) {
45 6
            $credentials[$metadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password');
46 6
        }
47
48 6
        $exception = null;
49 6
        $user = null;
50
        try {
51 6
            $user = $authenticationHandler->authenticate($credentials);
52 6
        } catch (CredentialException $ex) {
53 2
            $dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_EXCEPTION_THROWN, new OnCredentialExceptionThrownEvent($ex, $user));
54
55 2
            $exception = $ex;
56
        }
57
58
        /** @var OnAssembleResponseEvent $result */
59 6
        $result = $dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE, new OnAssembleResponseEvent($user, $exception));
60 6
        if (!$response = $result->getResponse()) {
61
            throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot assemble the response!', $exception);
62
        }
63
64 6
        return $response;
65
    }
66
67
    /**
68
     * Removes an api key.
69
     *
70
     * @param Request $request
71
     *
72
     * @return JsonResponse
73
     */
74 2
    public function removeSessionAction(Request $request)
75
    {
76
        /** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */
77 2
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
78
        /** @var \Doctrine\Common\Persistence\ObjectManager $om */
79 2
        $om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager'));
80
        /** @var ClassMetadata $metadata */
81 2
        $metadata = $this->get('ma27_api_key_authentication.class_metadata');
82
83 2
        if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) {
84 1
            return new JsonResponse(array('message' => 'Missing api key header!'), 400);
85
        }
86
87 1
        $repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name'));
88 1
        $user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header));
89
90 1
        $authenticationHandler->removeSession($user);
91
92 1
        return new JsonResponse(array(), 204);
93
    }
94
}
95