Authentication   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 21.51 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 20
loc 93
ccs 28
cts 32
cp 0.875
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B processRequest() 0 26 4
A checkForRole() 9 9 3
A loadUser() 11 11 3
A handleNotAuthenticatedRequest() 0 8 2

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\Application\UserException;
7
use BrainExe\Core\Authentication\AnonymusUserVO;
8
use BrainExe\Core\Authentication\Exception\UserNotFoundException;
9
use BrainExe\Core\Authentication\LoadUser;
10
use BrainExe\Core\Authentication\UserVO;
11
use BrainExe\Core\Translation\TranslationTrait;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
15
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
16
use Symfony\Component\Routing\Route;
17
18
/**
19
 * @Middleware("Middleware.Authentication")
20
 */
21
class Authentication extends AbstractMiddleware
22
{
23
24
    use TranslationTrait;
25
26
    /**
27
     * @var LoadUser
28
     */
29
    private $loadUser;
30
31
    /**
32
     * @param LoadUser $loadUser
33
     */
34 4
    public function __construct(LoadUser $loadUser)
35
    {
36 4
        $this->loadUser = $loadUser;
37 4
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    public function processRequest(Request $request, Route $route)
43
    {
44 4
        if ($request->attributes->has('user')) {
45
            $user = $request->attributes->get('user');
46
        } else {
47 4
            $session = $request->getSession();
48 4
            $userId  = (int)$session->get('user_id');
49
50 4
            $user = $this->loadUser($userId);
51
        }
52
53 4
        $request->attributes->set('user', $user);
54 4
        $request->attributes->set('user_id', $user->getId());
55
56 4
        $this->checkForRole($route, $user);
57
58 3
        if ($route->hasDefault('_guest')) {
59
            return null;
60
        }
61
62 3
        if (empty($user->getId())) {
63 1
            return $this->handleNotAuthenticatedRequest($request);
64
        }
65
66 2
        return null;
67
    }
68
69
    /**
70
     * @param Route $route
71
     * @param UserVO $user
72
     * @throws MethodNotAllowedException
73
     */
74 4 View Code Duplication
    protected function checkForRole(Route $route, UserVO $user) : void
75
    {
76 4
        if ($route->hasDefault('_role')) {
77 1
            $role = $route->getDefault('_role');
78 1
            if (!in_array($role, $user->roles, true)) {
79 1
                throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
80
            }
81
        }
82 3
    }
83
84
    /**
85
     * @param int $userId
86
     * @return AnonymusUserVO|UserVO
87
     */
88 4 View Code Duplication
    private function loadUser(int $userId) : UserVO
89
    {
90 4
        if ($userId > 0) {
91
            try {
92 3
                return $this->loadUser->loadUserById($userId);
93
            } catch (UserNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94
            }
95
        }
96
97 1
        return new AnonymusUserVO();
98
    }
99
100
    /**
101
     * @param Request $request
102
     * @return RedirectResponse
103
     * @throws UserException
104
     */
105 1
    private function handleNotAuthenticatedRequest(Request $request) : RedirectResponse
106
    {
107 1
        if ($request->isXmlHttpRequest()) {
108
            throw new MethodNotAllowedException([]);
109
        }
110
111 1
        return new RedirectResponse('/#/login');
112
    }
113
}
114