Completed
Pull Request — master (#144)
by
unknown
26:16 queued 11:25
created

IpClientListener::onKernelRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 13
rs 9.4285
1
<?php
2
3
namespace AppBundle\EventListener;
4
5
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
9
class IpClientListener
10
{
11
    /**
12
     * @var ManagerRegistry
13
     */
14
    private $registry;
15
16
    public function __construct(ManagerRegistry $registry)
17
    {
18
        $this->registry = $registry;
19
    }
20
21
    /**
22
     * @param GetResponseEvent $event
23
     */
24
    public function onKernelRequest(GetResponseEvent $event)
25
    {
26
        $request = $event->getRequest();
27
        $client = $this->registry->getRepository('AppBundle:Client')
28
            ->findIpBanned($request->getClientIp());
29
        if ($client) {
30
            $response = new JsonResponse([
31
                'code' => 403,
32
                'message' => 'Forbidden. You\'re banned!',
33
            ], 403);
34
            $event->setResponse($response);
35
        }
36
    }
37
}
38