Authentication::processRequest()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 2
crap 4.0466
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