TokenAuthentication   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 26.92 %

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 78
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\Core\Annotations\Middleware;
6
use BrainExe\Core\Authentication\AnonymusUserVO;
7
use BrainExe\Core\Authentication\Exception\UserNotFoundException;
8
use BrainExe\Core\Authentication\LoadUser;
9
use BrainExe\Core\Authentication\Token;
10
use BrainExe\Core\Authentication\UserVO;
11
use BrainExe\Core\Translation\TranslationTrait;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
14
use Symfony\Component\Routing\Route;
15
16
/**
17
 * @Middleware("Middleware.TokenAuthentication")
18
 */
19
class TokenAuthentication extends AbstractMiddleware
20
{
21
    use TranslationTrait;
22
23
    /**
24
     * @var LoadUser
25
     */
26
    private $loadUser;
27
28
    /**
29
     * @var Token
30
     */
31
    private $token;
32
33
    /**
34
     * @param LoadUser $loadUser
35
     * @param Token $token
36
     */
37 3
    public function __construct(LoadUser $loadUser, Token $token)
38
    {
39 3
        $this->loadUser = $loadUser;
40 3
        $this->token    = $token;
41 3
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function processRequest(Request $request, Route $route)
47
    {
48 3
        $token = $request->get('accessToken');
49
50 3
        if (null === $token) {
51 1
            return null;
52
        }
53
54 2
        $userId = $this->token->hasUserForRole($token);
55 2
        if (null === $userId) {
56 1
            return null;
57
        }
58
59 1
        $user = $this->loadUser($userId);
60
61 1
        $request->attributes->set('user', $user);
62 1
    }
63
64
    /**
65
     * @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...
66
     * @param Route $route
67
     * @param UserVO $user
68
     * @throws MethodNotAllowedException
69
     */
70 View Code Duplication
    protected function checkForRole(Route $route, UserVO $user)
71
    {
72
        if ($route->hasDefault('_role')) {
73
            $role = $route->getDefault('_role');
74
            if (!in_array($role, $user->roles, true)) {
75
                throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
76
            }
77
        }
78
    }
79
80
    /**
81
     * @param int $userId
82
     * @return AnonymusUserVO|UserVO
83
     */
84 1 View Code Duplication
    private function loadUser(int $userId) : UserVO
85
    {
86 1
        if ($userId > 0) {
87
            try {
88 1
                return $this->loadUser->loadUserById($userId);
89
            } catch (UserNotFoundException $e) {
90
                return new AnonymusUserVO();
91
            }
92
        } else {
93
            return new AnonymusUserVO();
94
        }
95
    }
96
}
97