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 ( 9426da...e7ee62 )
by Maximilian
03:12
created

ApiKeyController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Test Coverage

Coverage 97.06%

Importance

Changes 11
Bugs 2 Features 2
Metric Value
wmc 7
c 11
b 2
f 2
lcom 0
cbo 14
dl 0
loc 80
ccs 33
cts 34
cp 0.9706
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B requestApiKeyAction() 0 40 5
A removeSessionAction() 0 20 2
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Controller;
4
5
use Ma27\ApiKeyAuthenticationBundle\Event\AssembleResponseEvent;
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
     * @return JsonResponse
27
     *
28
     * @throws HttpException If the login fails.
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($user));
54
55 2
            $exception = $ex;
56
        }
57
58
        /** @var AssembleResponseEvent $result */
59 6
        $result = $dispatcher->dispatch(
60 6
            Ma27ApiKeyAuthenticationEvents::ASSEMBLE_RESPONSE,
61 6
            new AssembleResponseEvent($user, $exception)
62 6
        );
63
64 6
        if (!$response = $result->getResponse()) {
65
            throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot assemble the response!', $exception);
66
        }
67
68 6
        return $response;
69
    }
70
71
    /**
72
     * Removes an api key.
73
     *
74
     * @param Request $request
75
     *
76
     * @return JsonResponse
77
     */
78 2
    public function removeSessionAction(Request $request)
79
    {
80
        /** @var \Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface $authenticationHandler */
81 2
        $authenticationHandler = $this->get('ma27_api_key_authentication.auth_handler');
82
        /** @var \Doctrine\Common\Persistence\ObjectManager $om */
83 2
        $om = $this->get($this->container->getParameter('ma27_api_key_authentication.object_manager'));
84
        /** @var ClassMetadata $metadata */
85 2
        $metadata = $this->get('ma27_api_key_authentication.class_metadata');
86
87 2
        if (!$header = (string) $request->headers->get($this->container->getParameter('ma27_api_key_authentication.key_header'))) {
88 1
            return new JsonResponse(array('message' => 'Missing api key header!'), 400);
89
        }
90
91 1
        $repository = $om->getRepository($this->container->getParameter('ma27_api_key_authentication.model_name'));
92 1
        $user = $repository->findOneBy(array($metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $header));
93
94 1
        $authenticationHandler->removeSession($user);
95
96 1
        return new JsonResponse(array(), 204);
97
    }
98
}
99