Firewall   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 109
ccs 36
cts 36
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addRoute() 0 8 2
A isGranted() 0 12 4
A isGrantedRoute() 0 14 4
B isUriInRoutes() 0 16 5
A hasRole() 0 4 1
A getRoutes() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the "Kata 1" package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 */
13
14
namespace Component\Firewall;
15
16
/**
17
 * Firewall.
18
 */
19
class Firewall
20
{
21
    const ROLE_ADMIN = 'ROLE_ADMIN';
22
23
    /**
24
     * @var array
25
     */
26
    protected $routes = [];
27
28
    /**
29
     * @param string|array $methods
30
     * @param string       $path
31
     * @param string       $role
32
     */
33 21
    public function addRoute($methods, $path, $role)
34
    {
35 21
        $this->routes[strtolower($path)] = [
36 21
            'path' => strtolower($path),
37 21
            'role' => $role,
38 21
            'methods' => is_array($methods) ? $methods : [$methods],
39
        ];
40 21
    }
41
42
    /**
43
     * @param string                 $method
44
     * @param string                 $uri
45
     * @param HasRolesInterface|null $user
46
     *
47
     * @return bool
48
     */
49 21
    public function isGranted($method, $uri, HasRolesInterface $user = null)
50
    {
51 21
        foreach ($this->getRoutes() as $route) {
52 21
            if ($this->isUriInRoutes($method, $uri, $route)) {
53 12
                if (!$this->isGrantedRoute($method, $uri, $route, $user)) {
54 7
                    return false;
55
                }
56 9
            }
57 19
        }
58
59 19
        return true;
60
    }
61
62
    /**
63
     * @param string                 $method
64
     * @param string                 $path
65
     * @param array                  $route
66
     * @param HasRolesInterface|null $user
67
     *
68
     * @return bool
69
     */
70 12
    protected function isGrantedRoute($method, $path, $route, HasRolesInterface $user = null)
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72 12
        if (!$user) {
73 6
            return false;
74
        }
75 10
        if ($this->hasRole($user, self::ROLE_ADMIN)) {
76 6
            return true;
77
        }
78 6
        if ($this->hasRole($user, $route['role'])) {
79 4
            return true;
80
        }
81
82 2
        return false;
83
    }
84
85
    /**
86
     * @param string $method
87
     * @param string $uri
88
     * @param string $route
89
     *
90
     * @return bool
91
     */
92 21
    protected function isUriInRoutes($method, $uri, $route)
93
    {
94 21
        if (substr(strtolower($uri), 0, strlen($route['path'])) != $route['path']) {
95 12
            return false;
96
        };
97 14
        if (!$route['methods']) {
98 2
            return true;
99
        }
100 12
        foreach ($route['methods'] as $routeMethod) {
0 ignored issues
show
Bug introduced by
The expression $route['methods'] of type string is not traversable.
Loading history...
101 12
            if ($routeMethod === $method) {
102 10
                return true;
103
            }
104 3
        }
105
106 3
        return false;
107
    }
108
109
    /**
110
     * @param HasRolesInterface $user
111
     * @param string            $role
112
     *
113
     * @return bool
114
     */
115 10
    protected function hasRole(HasRolesInterface $user, $role)
116
    {
117 10
        return in_array($role, $user->getRoles());
118
    }
119
120
    /**
121
     * @return array
122
     */
123 21
    protected function getRoutes()
124
    {
125 21
        return $this->routes;
126
    }
127
}
128