Firewall::exceptionLog()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 20
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Someshwer\Firewall\Lib;
4
5
use Someshwer\Firewall\src\Repo\FirewallRepository;
6
7
/**
8
 * Class Firewall.
9
 *
10
 * @author Someshwer
11
 * Date: 15-08-2018
12
 */
13
class Firewall
14
{
15
    /**
16
     * @var \Illuminate\Config\Repository|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Config\Repository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
     */
18
    private $whitelist;
19
20
    /**
21
     * @var \Illuminate\Config\Repository|mixed
22
     */
23
    private $blacklist;
24
25
    /**
26
     * @var \Illuminate\Config\Repository|mixed
27
     */
28
    private $accept_list;
29
30
    /**
31
     * @var \Illuminate\Config\Repository|mixed
32
     */
33
    private $reject_list;
34
35
    /**
36
     * @var FirewallRepository
37
     */
38
    private $repo;
39
40
    /**
41
     * Firewall constructor.
42
     *
43
     * @param FirewallRepository $firewallRepository
44
     */
45
    public function __construct(FirewallRepository $firewallRepository)
46
    {
47
        $this->repo = $firewallRepository;
48
        $this->whitelist = 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

48
        $this->whitelist = /** @scrutinizer ignore-call */ config('firewall.whitelist');
Loading history...
49
        $this->blacklist = config('firewall.blacklist');
50
        $this->accept_list = config('firewall.accept');
51
        $this->reject_list = config('firewall.reject');
52
    }
53
54
    /**
55
     * This package provides some useful information about the package.
56
     *
57
     * @return array
58
     */
59
    public function info()
60
    {
61
        return [
62
            'package_name' => 'Laravel - Firewall',
63
            'description'  => 'Laravel Firewall package detects unknown ip addresses based on 
64
            blacklist and whitelist ip addresses. Whitelist and Blacklist are two configuration options 
65
            any one of them you can set to TRUE based on your requirement. For example if you set blacklist
66
            to TRUE and have added some ip addresses to blacklist then in that case any request to the 
67
            application will be blocked by the firewall from those ip addresses that listed in blacklist.
68
            If you have added them to whitelist only the request from the whitelisted ips can be accepted 
69
            and remaining all requests will be blocked by the firewall. If you set both black and whitelist 
70
            to TRUE then in that case the preference will be given to blacklist',
71
            'latest_release' => '2.2.1',
72
            'stable_version' => '2.2.1',
73
            'author'         => 'Someshwer Bandapally<[email protected]>',
74
        ];
75
    }
76
77
    /**
78
     * Returns all whitelisted ip addresses.
79
     *
80
     * @return array
81
     */
82
    public function whitelist()
83
    {
84
        return ['whitelisted_ips' => $this->whitelist];
85
    }
86
87
    /**
88
     * Returns all black listed ip addresses.
89
     *
90
     * @return array
91
     */
92
    public function blacklist()
93
    {
94
        return ['blacklisted_ips' => $this->blacklist];
95
    }
96
97
    /**
98
     * Returns both combination of white and black listed ip addresses.
99
     *
100
     * @return array
101
     */
102
    public function whiteAndBlackList()
103
    {
104
        $common_list = array_intersect($this->whitelist, $this->blacklist);
0 ignored issues
show
Bug introduced by
It seems like $this->whitelist can also be of type Illuminate\Config\Repository; however, parameter $array1 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        $common_list = array_intersect(/** @scrutinizer ignore-type */ $this->whitelist, $this->blacklist);
Loading history...
Bug introduced by
It seems like $this->blacklist can also be of type Illuminate\Config\Repository; however, parameter $array2 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        $common_list = array_intersect($this->whitelist, /** @scrutinizer ignore-type */ $this->blacklist);
Loading history...
105
        $whitelist = array_diff($this->whitelist, $common_list);
0 ignored issues
show
Bug introduced by
It seems like $this->whitelist can also be of type Illuminate\Config\Repository; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

105
        $whitelist = array_diff(/** @scrutinizer ignore-type */ $this->whitelist, $common_list);
Loading history...
106
        $blacklist = array_diff($this->blacklist, $common_list);
107
        $list_groups = [
108
            'white'  => $whitelist,
109
            'black'  => $blacklist,
110
            'common' => $common_list,
111
        ];
112
        $result_list = [];
113
        foreach ($list_groups as $list_type => $list_group) {
114
            $result_list[] = $this->repo->getList($list_group, 2, $list_type);
115
        }
116
117
        return array_collapse($result_list);
0 ignored issues
show
Bug introduced by
The function array_collapse 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

117
        return /** @scrutinizer ignore-call */ array_collapse($result_list);
Loading history...
118
    }
119
120
    /**
121
     * Returns accept listed ip addresses.
122
     *
123
     * @return array
124
     */
125
    public function acceptList()
126
    {
127
        return ['accept_listed_ips' => $this->accept_list];
128
    }
129
130
    /**
131
     * Returns reject listed ip addresses.
132
     *
133
     * @return array
134
     */
135
    public function rejectList()
136
    {
137
        return ['reject_listed_ips' => $this->reject_list];
138
    }
139
140
    /**
141
     * Returns both combination of accept and reject listed ip addresses.
142
     *
143
     * @return array
144
     */
145
    public function acceptAndRejectList()
146
    {
147
        $common_list = array_intersect($this->accept_list, $this->reject_list);
0 ignored issues
show
Bug introduced by
It seems like $this->reject_list can also be of type Illuminate\Config\Repository; however, parameter $array2 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

147
        $common_list = array_intersect($this->accept_list, /** @scrutinizer ignore-type */ $this->reject_list);
Loading history...
Bug introduced by
It seems like $this->accept_list can also be of type Illuminate\Config\Repository; however, parameter $array1 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

147
        $common_list = array_intersect(/** @scrutinizer ignore-type */ $this->accept_list, $this->reject_list);
Loading history...
148
        $accept_list = array_diff($this->accept_list, $common_list);
0 ignored issues
show
Bug introduced by
It seems like $this->accept_list can also be of type Illuminate\Config\Repository; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

148
        $accept_list = array_diff(/** @scrutinizer ignore-type */ $this->accept_list, $common_list);
Loading history...
149
        $reject_list = array_diff($this->reject_list, $common_list);
150
        $list_groups = [
151
            'accept' => $accept_list,
152
            'reject' => $reject_list,
153
            'common' => $common_list,
154
        ];
155
        $result_list = [];
156
        foreach ($list_groups as $list_type => $list_group) {
157
            $result_list[] = $this->repo->getList($list_group, 2, $list_type);
158
        }
159
160
        return array_collapse($result_list);
0 ignored issues
show
Bug introduced by
The function array_collapse 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

160
        return /** @scrutinizer ignore-call */ array_collapse($result_list);
Loading history...
161
    }
162
163
    /**
164
     * Returns all ip addresses with their statuses.
165
     *
166
     * @return array
167
     */
168
    public function getAllIpAddresses()
169
    {
170
        $ip_addresses = $this->repo->getUniqueIpAddresses();
171
        $ip_list_with_statuses = [];
172
        $index = 0;
173
        foreach ($ip_addresses as $ip_address) {
174
            $ip_list_with_statuses[] = $this->repo->getIpStatus($ip_address);
175
            $index++;
176
        }
177
178
        return $ip_list_with_statuses;
179
    }
180
181
    /**
182
     * Returns firewall log data along with pagination if pagination is enabled in configuration.
183
     *
184
     * @param null $from_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from_date is correct as it would always require null to be passed?
Loading history...
185
     * @param null $to_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $to_date is correct as it would always require null to be passed?
Loading history...
186
     *
187
     * @return mixed|\Someshwer\Firewall\src\Entities\FirewallLog|\Someshwer\Firewall\src\Entities\FirewallRequestsLogModel
188
     */
189
    public function log($from_date = null, $to_date = null)
190
    {
191
        $log = $this->repo->getLogInstance();
192
        if ((($from_date != null) || ($to_date != null))) {
0 ignored issues
show
introduced by
The condition $to_date != null is always false.
Loading history...
193
            if (!$this->repo->validateDates($from_date, $to_date)) {
194
                $log = $this->repo->addWhereBetweenClause($log, $from_date, $to_date);
195
            }
196
        }
197
        $log = $this->repo->addPagination($log);
198
199
        return $log;
200
    }
201
202
    /**
203
     * Returns firewall requests log data along with pagination if pagination is enabled in configuration.
204
     *
205
     * @param null $from_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from_date is correct as it would always require null to be passed?
Loading history...
206
     * @param null $to_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $to_date is correct as it would always require null to be passed?
Loading history...
207
     *
208
     * @return mixed|\Someshwer\Firewall\src\Entities\FirewallLog|\Someshwer\Firewall\src\Entities\FirewallRequestsLogModel
209
     */
210
    public function requestLog($from_date = null, $to_date = null)
211
    {
212
        $log = $this->repo->getLogInstance('request_log');
213
        if ((($from_date != null) || ($to_date != null))) {
0 ignored issues
show
introduced by
The condition $to_date != null is always false.
Loading history...
214
            if (!$this->repo->validateDates($from_date, $to_date)) {
215
                $log = $this->repo->addWhereBetweenClause($log, $from_date, $to_date);
216
            }
217
        }
218
        $log = $this->repo->addPagination($log, 'request_log');
219
220
        return $log;
221
    }
222
223
    /**
224
     * Returns exceptions log data along with pagination if pagination is enabled in configuration.
225
     *
226
     * @param null $from_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from_date is correct as it would always require null to be passed?
Loading history...
227
     * @param null $to_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $to_date is correct as it would always require null to be passed?
Loading history...
228
     *
229
     * @return mixed|\Someshwer\Firewall\src\Entities\ExceptionLog|\Someshwer\Firewall\src\Entities\FirewallLog|\Someshwer\Firewall\src\Entities\FirewallRequestsLogModel
230
     */
231
    public function exceptionLog($from_date = null, $to_date = null)
232
    {
233
        $log = $this->repo->getLogInstance('exception_log');
234
        if ((($from_date != null) || ($to_date != null))) {
0 ignored issues
show
introduced by
The condition $to_date != null is always false.
Loading history...
235
            if (!$this->repo->validateDates($from_date, $to_date)) {
236
                $log = $this->repo->addWhereBetweenClause($log, $from_date, $to_date);
237
            }
238
        }
239
        $log = $this->repo->addPagination($log, 'exception_log');
240
241
        return $log;
242
    }
243
}
244