IPFilter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Someshwer\Firewall\Lib;
4
5
/**
6
 * This class filters ips forwarded from FirewallMiddleware.
7
 *
8
 * IPFilter class
9
 */
10
class IPFilter
11
{
12
    /**
13
     * Determines whether to block current request or not.
14
     *
15
     * @var bool
16
     */
17
    private $block;
18
19
    /**
20
     * Sets the constant to  'BLACKLIST'.
21
     *
22
     * @var string
23
     */
24
    private $black_list;
25
26
    /**
27
     * Sets the constant to 'WHITELIST'.
28
     *
29
     * @var string
30
     */
31
    private $white_list;
32
33
    /**
34
     * Sets the constant to 'NONE'.
35
     *
36
     * @var string
37
     */
38
    private $none;
39
40
    /**
41
     * Constructor function.
42
     */
43
    public function __construct()
44
    {
45
        $this->block = true;
46
        $this->black_list = 'BLACKLIST';
47
        $this->white_list = 'WHITELIST';
48
        $this->none = 'NONE';
49
    }
50
51
    /**
52
     * Returns one of the filter type i.e; 'BLACKLIST', 'WHITELIST', or 'NONE'.
53
     *
54
     * @return string $list
55
     */
56
    public function getFilterType()
57
    {
58
        $filter_type = $this->none;
59
        if (config('firewall.enable_whitelist') && config('firewall.enable_blacklist')) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        if (/** @scrutinizer ignore-call */ config('firewall.enable_whitelist') && config('firewall.enable_blacklist')) {
Loading history...
60
            $filter_type = $this->black_list;
61
        }
62
        if (config('firewall.enable_whitelist') && (!config('firewall.enable_blacklist'))) {
63
            $filter_type = $this->white_list;
64
        }
65
        if (config('firewall.enable_blacklist') && (!config('firewall.enable_whitelist'))) {
66
            $filter_type = $this->black_list;
67
        }
68
69
        return $filter_type;
70
    }
71
72
    /**
73
     * Filters whitelist and returns 'true' if current request ip is not in whitelist.
74
     *
75
     * @param object $request
76
     *
77
     * @return bool
78
     */
79
    public function filterWhiteList($request)
80
    {
81
        if (in_array($request->ip(), config('firewall.whitelist'))) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        if (in_array($request->ip(), /** @scrutinizer ignore-call */ config('firewall.whitelist'))) {
Loading history...
82
            return $this->block;
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Filters blacklist and returns 'true' if current request ip is available in blacklist.
90
     *
91
     * @param [type] $request
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
92
     *
93
     * @return bool
94
     */
95
    public function filterBlackList($request)
96
    {
97
        if (in_array($request->ip(), config('firewall.blacklist'))) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        if (in_array($request->ip(), /** @scrutinizer ignore-call */ config('firewall.blacklist'))) {
Loading history...
98
            return $this->block;
99
        }
100
101
        return false;
102
    }
103
}
104