Completed
Push — master ( c9d0f8...e25395 )
by Saulius
06:08 queued 03:54
created

AccessProtector::configureDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 function Sauls\Component\Helper\array_get_value;
16
use Sauls\Bundle\Components\Component\Security\Access\Granter\Ip\IpGranterInterface;
17
use Sauls\Bundle\Components\Component\Security\Access\Granter\String\StringGranterInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
class AccessProtector implements AccessProtectorInterface
21
{
22
    /**
23
     * @var array
24
     */
25
    private $options;
26
    /**
27
     * @var StringGranterInterface
28
     */
29
    private $stringGranter;
30
    /**
31
     * @var IpGranterInterface
32
     */
33
    private $ipGranter;
34
35
    /**
36
     * RouteProtector constructor.
37
     *
38
     * @param array $options
39
     * @param StringGranterInterface $stringGranter
40
     * @param IpGranterInterface $ipGranter
41
     */
42 8
    public function __construct(
43
        array $options = [],
44
        StringGranterInterface $stringGranter,
45
        IpGranterInterface $ipGranter
46
    ) {
47 8
        $resolver = new OptionsResolver;
48 8
        $this->configureDefaults($resolver);
49
50 8
        $this->options = $resolver->resolve($options);
51 8
        $this->stringGranter = $stringGranter;
52 8
        $this->ipGranter = $ipGranter;
53 8
    }
54
55
    /**
56
     * @param OptionsResolver $resolver
57
     *
58
     * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
59
     * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
60
     */
61 8
    protected function configureDefaults(OptionsResolver $resolver): void
62
    {
63 8
        $resolver->setRequired(
64
            [
65 8
                'protected_routes',
66
                'allowed_ips',
67
            ]
68
        );
69
70 8
        $resolver->setAllowedTypes('protected_routes', ['null', 'array']);
71 8
        $resolver->setAllowedTypes('allowed_ips', ['null', 'array']);
72 8
    }
73
74
    /**
75
     * @param string $route
76
     *
77
     * @return bool
78
     * @throws \Exception
79
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
80
     * @internal param string $value
81
     */
82 5
    public function isRouteAccessProtected(string $route): bool
83
    {
84 5
        $routes = array_get_value($this->options, 'protected_routes');
85
86 5
        if (!$routes) {
87 1
            return true;
88
        }
89
90 4
        return $this->stringGranter->isGranted($route, $routes);
91
    }
92
93
    /**
94
     * @param string $ip
95
     *
96
     * @return bool
97
     * @throws \Exception
98
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
99
     */
100 5
    public function isIpAccessAllowed(string $ip): bool
101
    {
102 5
        $ips = array_get_value($this->options, 'allowed_ips');
103
104 5
        if (!$ips) {
105 1
            return true;
106
        }
107
108 4
        return $this->ipGranter->isGranted($ip, $ips);
109
    }
110
}
111