IpRestricter::restrict()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Tequilarapido\RestrictAccess\Restricter;
4
5
use Symfony\Component\HttpFoundation\Response;
6
7
class IpRestricter extends Restricter
8
{
9
    use IpHelper;
10
11
    /**
12
     * Do we need to restric.
13
     */
14
    public function isRestrictionEnabled()
15
    {
16
        return config('restrict_access.by_ip.enabled');
17
    }
18
19
    /**
20
     * Protection method.
21
     *
22
     * @return mixed
23
     */
24
    public function restrict()
25
    {
26
        if (! $this->isRestrictionEnabled()) {
27
            return false;
28
        }
29
30
31
        if (! $this->isAllowed($this->getClientIp())) {
32
            return $this->getNotAllowedResponse();
33
        }
34
35
        return false;
36
    }
37
38
    /**
39
     * Returns forbidden response.
40
     *
41
     * @return Response
42
     */
43
    protected function getNotAllowedResponse()
44
    {
45
        $headers = ['X-RESTRICT-ACCESS' => 'Not allowed client ip'];
46
47
        return new Response('Not allowed.', 403, $headers);
48
    }
49
}
50