Completed
Push — master ( 1f2b71...3f1d1d )
by Matze
07:04
created

TokenAuthentication   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 26.58 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 65.38%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 21
loc 79
ccs 17
cts 26
cp 0.6538
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A processRequest() 0 17 3
A checkForRole() 9 9 3
A loadUser() 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
namespace BrainExe\Core\Middleware;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Core\Annotations\Middleware;
7
8
use BrainExe\Core\Authentication\AnonymusUserVO;
9
use BrainExe\Core\Authentication\Exception\UserNotFoundException;
10
use BrainExe\Core\Authentication\LoadUser;
11
use BrainExe\Core\Authentication\Token;
12
use BrainExe\Core\Authentication\UserVO;
13
use BrainExe\Core\Translation\TranslationTrait;
14
15
use Symfony\Component\HttpFoundation\Request;
16
17
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
18
use Symfony\Component\Routing\Route;
19
20
/**
21
 * @Middleware("Middleware.TokenAuthentication")
22
 */
23
class TokenAuthentication extends AbstractMiddleware
24
{
25
    use TranslationTrait;
26
27
    /**
28
     * @var LoadUser
29
     */
30
    private $loadUser;
31
32
    /**
33
     * @var Token
34
     */
35
    private $token;
36
37
    /**
38
     * @Inject
39
     * @param LoadUser $loadUser
40
     * @param Token $token
41
     */
42 4
    public function __construct(LoadUser $loadUser, Token $token)
43
    {
44 4
        $this->loadUser = $loadUser;
45 4
        $this->token    = $token;
46 4
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 3
    public function processRequest(Request $request, Route $route)
52
    {
53 3
        $token = $request->get('accessToken');
54
55 3
        if (empty($token)) {
56 1
            return null;
57
        }
58
59 2
        $userId = $this->token->hasUserForRole($token);
60 2
        if (empty($userId)) {
61 1
            return null;
62
        }
63
64 1
        $user = $this->loadUser($userId);
65
66 1
        $request->attributes->set('user', $user);
67 1
    }
68
69
    /**
70
     * @todo check/implement
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
71
     * @param Route $route
72
     * @param UserVO $user
73
     * @throws MethodNotAllowedException
74
     */
75 View Code Duplication
    protected function checkForRole(Route $route, UserVO $user)
0 ignored issues
show
Duplication introduced by
This method 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...
76
    {
77
        if ($route->hasDefault('_role')) {
78
            $role = $route->getDefault('_role');
79
            if (!in_array($role, $user->roles)) {
80
                throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
81
            }
82
        }
83
    }
84
85
    /**
86
     * @param int $userId
87
     * @return AnonymusUserVO|UserVO
88
     */
89 1 View Code Duplication
    private function loadUser(int $userId) : UserVO
0 ignored issues
show
Duplication introduced by
This method 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...
90
    {
91 1
        if ($userId > 0) {
92
            try {
93 1
                return $this->loadUser->loadUserById($userId);
94
            } catch (UserNotFoundException $e) {
95
                return new AnonymusUserVO();
96
            }
97
        } else {
98
            return new AnonymusUserVO();
99
        }
100
    }
101
}
102