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
Push — master ( 017a4b...9acd45 )
by Maximilian
11:50 queued 09:23
created

ApiKeyController::removeSessionAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Controller;
4
5
use Ma27\ApiKeyAuthenticationBundle\Event\AssembleResponseEvent;
6
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException;
7
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents;
8
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
15
/**
16
 * Controller which is responsible for the authentication routes.
17
 */
18
class ApiKeyController extends Controller
19
{
20
    /**
21
     * Requests an api key.
22
     *
23
     * @param Request $request
24
     *
25
     * @return JsonResponse
26
     */
27 4
    public function requestApiKeyAction(Request $request)
28
    {
29
        /** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */
30 4
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
31
        /** @var ClassMetadata $metadata */
32 4
        $metadata = $this->get('ma27_api_key_authentication.class_metadata');
33
        /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
34 4
        $dispatcher = $this->get('event_dispatcher');
35
36 4
        $credentials = array();
37 4
        if ($request->request->has('login')) {
38 4
            $credentials[$metadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY)] = $request->request->get('login');
39 4
        }
40
41 4
        if ($request->request->has('password')) {
42 4
            $credentials[$metadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY)] = $request->request->get('password');
43 4
        }
44
45 4
        $exception = null;
46 4
        $user      = null;
47
        try {
48 4
            $user = $authenticationHandler->authenticate($credentials);
49 4
        } catch (CredentialException $ex) {
50 2
            $exception = $ex;
51
        }
52
53
        /** @var AssembleResponseEvent $result */
54 4
        $result = $dispatcher->dispatch(
55 4
            Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE,
56 4
            new AssembleResponseEvent($user, $exception)
57 4
        );
58
59 4
        if (!$response = $result->getResponse()) {
60
            throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot assemble the response!');
61
        }
62
63 4
        return $response;
64
    }
65
66
    /**
67
     * Removes an api key.
68
     *
69
     * @param Request $request
70
     *
71
     * @return JsonResponse
72
     */
73 2
    public function removeSessionAction(Request $request)
74
    {
75
        /** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */
76 2
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
77
        /** @var \Doctrine\Common\Persistence\ObjectManager $om */
78 2
        $om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager'));
79
        /** @var ClassMetadata $metadata */
80 2
        $metadata = $this->get('ma27_api_key_authentication.class_metadata');
81
82 2
        if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) {
83 1
            return new JsonResponse(array('message' => 'Missing api key header!'), 400);
84
        }
85
86 1
        $repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name'));
87 1
        $user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header));
88
89 1
        $authenticationHandler->removeSession($user);
90
91 1
        return new JsonResponse(array(), 204);
92
    }
93
}
94