AccessProtectorSubscriber   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 42
ccs 17
cts 17
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 6 1
A isAccessProtected() 0 4 2
A onKernelRequest() 0 13 3
A __construct() 0 3 1
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\EventSubscriber;
14
15
use Sauls\Bundle\Components\Component\Security\Access\Protector\AccessProtectorInterface;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Event\RequestEvent;
19
use Symfony\Component\HttpKernel\Exception\HttpException;
20
21
class AccessProtectorSubscriber implements EventSubscriberInterface
22
{
23
    private AccessProtectorInterface $accessProtector;
24
25 3
    public function __construct(AccessProtectorInterface $accessProtector)
26
    {
27 3
        $this->accessProtector = $accessProtector;
28 3
    }
29
30 3
    public static function getSubscribedEvents(): array
31
    {
32
        return [
33
            RequestEvent::class => [
34 3
                'onKernelRequest',
35
                0,
36
            ],
37
        ];
38
    }
39
40
    /**
41
     * @throws HttpException
42
     */
43 3
    public function onKernelRequest(RequestEvent $event)
44
    {
45 3
        if (!$event->isMasterRequest()) {
46 1
            return;
47
        }
48
49 2
        $request = $event->getRequest();
50
51 2
        $route = $request->get('_route', '');
52 2
        $ip = $request->getClientIp() ?? '';
53
54 2
        if ($this->isAccessProtected($route, $ip)) {
55 1
            throw new HttpException(Response::HTTP_NOT_FOUND, 'Not found');
56
        }
57 1
    }
58
59 2
    protected function isAccessProtected(string $route, string $ip): bool
60
    {
61 2
        return $this->accessProtector->isRouteAccessProtected($route)
62 2
            && !$this->accessProtector->isIpAccessAllowed($ip);
63
    }
64
}
65