AccessProtector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of the sauls/security package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2017
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Bundle\Components\Component\Security\Access\Protector;
14
15
use Exception;
16
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;
17
use Symfony\Component\OptionsResolver\Exception\AccessException;
18
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
19
use function Sauls\Component\Helper\array_get_value;
20
use Sauls\Bundle\Components\Component\Security\Access\Granter\Ip\IpGranterInterface;
21
use Sauls\Bundle\Components\Component\Security\Access\Granter\String\StringGranterInterface;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
class AccessProtector implements AccessProtectorInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    private $options;
30
    /**
31
     * @var StringGranterInterface
32
     */
33
    private $stringGranter;
34
    /**
35
     * @var IpGranterInterface
36
     */
37
    private $ipGranter;
38
39
    /**
40
     * RouteProtector constructor.
41
     *
42
     * @param array $options
43
     * @param StringGranterInterface $stringGranter
44
     * @param IpGranterInterface $ipGranter
45
     */
46 8
    public function __construct(
47
        array $options = [],
48
        StringGranterInterface $stringGranter,
49
        IpGranterInterface $ipGranter
50
    ) {
51 8
        $resolver = new OptionsResolver;
52 8
        $this->configureDefaults($resolver);
53
54 8
        $this->options = $resolver->resolve($options);
55 8
        $this->stringGranter = $stringGranter;
56 8
        $this->ipGranter = $ipGranter;
57 8
    }
58
59
    /**
60
     * @param OptionsResolver $resolver
61
     *
62
     * @throws UndefinedOptionsException
63
     * @throws AccessException
64
     */
65 8
    protected function configureDefaults(OptionsResolver $resolver): void
66
    {
67 8
        $resolver->setRequired(
68
            [
69 8
                'protected_routes',
70
                'allowed_ips',
71
            ]
72
        );
73
74 8
        $resolver->setAllowedTypes('protected_routes', ['null', 'array']);
75 8
        $resolver->setAllowedTypes('allowed_ips', ['null', 'array']);
76 8
    }
77
78
    /**
79
     * @param string $route
80
     *
81
     * @return bool
82
     * @throws Exception
83
     * @throws PropertyNotAccessibleException
84
     * @internal param string $value
85
     */
86 5
    public function isRouteAccessProtected(string $route): bool
87
    {
88 5
        $routes = array_get_value($this->options, 'protected_routes');
89
90 5
        if (!$routes) {
91 1
            return true;
92
        }
93
94 4
        return $this->stringGranter->isGranted($route, $routes);
95
    }
96
97
    /**
98
     * @param string $ip
99
     *
100
     * @return bool
101
     * @throws Exception
102
     * @throws PropertyNotAccessibleException
103
     */
104 5
    public function isIpAccessAllowed(string $ip): bool
105
    {
106 5
        $ips = array_get_value($this->options, 'allowed_ips');
107
108 5
        if (!$ips) {
109 1
            return true;
110
        }
111
112 4
        return $this->ipGranter->isGranted($ip, $ips);
113
    }
114
}
115