Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

AuthenticationListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 55
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B onKernelRequest() 22 22 4
A authentication() 12 12 3

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
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\SharedKernel\Infrastructure\Symfony\EventListener;
16
17
use Http\Client\HttpClient;
18
use Http\Message\Authentication\Bearer;
19
use Http\Message\MessageFactory;
20
use Kreta\SharedKernel\Infrastructure\Symfony\Security\User;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
23
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
26 View Code Duplication
final class AuthenticationListener
0 ignored issues
show
Duplication introduced by
This class 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...
27
{
28
    private $messageFactory;
29
    private $client;
30
    private $tokenStorage;
31
    private $identityAccessHost;
32
33
    public function __construct(
34
        MessageFactory $messageFactory,
35
        HttpClient $client,
36
        TokenStorageInterface $tokenStorage,
37
        string $identityAccessHost
38
    ) {
39
        $this->messageFactory = $messageFactory;
40
        $this->client = $client;
41
        $this->tokenStorage = $tokenStorage;
42
        $this->identityAccessHost = $identityAccessHost;
43
    }
44
45
    public function onKernelRequest(GetResponseEvent $event) : void
46
    {
47
        if (!$event->isMasterRequest()) {
48
            return;
49
        }
50
51
        // Prevents to crash the Symfony profiler routes
52
        if (0 === mb_strpos($event->getRequest()->getPathInfo(), '/_')) {
53
            return;
54
        }
55
56
        $request = $this->messageFactory->createRequest('GET', $this->identityAccessHost . '/user');
57
        $request = $this->authentication($event->getRequest())->authenticate($request);
58
        $response = $this->client->sendRequest($request);
59
60
        $data = json_decode($response->getBody()->getContents(), true);
61
        if (!isset($data['user_id'])) {
62
            throw new UnauthorizedHttpException('Basic');
63
        }
64
65
        $this->tokenStorage->getToken()->setUser(new User($data['user_id']));
66
    }
67
68
    private function authentication(Request $request) : Bearer
69
    {
70
        if ($request->headers->has('Authorization')) {
71
            $token = str_replace('Bearer ', '', $request->headers->get('Authorization'));
72
        } elseif ($request->query->has('access_token')) {
73
            $token = $request->query->get('access_token');
74
        } else {
75
            throw new UnauthorizedHttpException('Basic');
76
        }
77
78
        return new Bearer($token);
79
    }
80
}
81