FirewallRepository   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 4
Metric Value
eloc 78
dl 0
loc 213
ccs 0
cts 124
cp 0
rs 10
c 6
b 1
f 4
wmc 28

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addPagination() 0 14 4
B prepareList() 0 29 6
A getUniqueIpAddresses() 0 11 1
A getLogInstance() 0 10 3
A initializeIpStatusList() 0 11 1
A getIpStatus() 0 17 5
A getList() 0 18 6
A addWhereBetweenClause() 0 5 1
A validateDates() 0 11 1
1
<?php
2
3
namespace Someshwer\Firewall\src\Repo;
4
5
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon 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...
6
use Illuminate\Support\Facades\Validator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Validator 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...
7
use Someshwer\Firewall\src\Entities\ExceptionLog;
8
use Someshwer\Firewall\src\Entities\FirewallLog;
9
use Someshwer\Firewall\src\Entities\FirewallRequestsLogModel;
10
11
/**
12
 * @author Someshwer
13
 * Date: 15-08-2018
14
 */
15
class FirewallRepository
16
{
17
    /**
18
     * Prepares list with white, black, accept, and reject ips.
19
     *
20
     * @param $item
21
     * @param $list_type
22
     *
23
     * @return array
24
     */
25
    private function prepareList($item, $list_type)
26
    {
27
        $record = [
28
            'ip_address'     => $item,
29
            'in_whitelist'   => false,
30
            'in_blacklist'   => false,
31
            'in_accept_list' => false,
32
            'in_reject_list' => false,
33
        ];
34
        if ($list_type == 'white') {
35
            $record['in_whitelist'] = true;
36
        }
37
        if ($list_type == 'black') {
38
            $record['in_blacklist'] = true;
39
        }
40
        if ($list_type == 'accept') {
41
            $record['in_accept_list'] = true;
42
        }
43
        if ($list_type == 'reject') {
44
            $record['in_reject_list'] = true;
45
        }
46
        if ($list_type == 'common') {
47
            $record['in_whitelist'] = true;
48
            $record['in_blacklist'] = true;
49
            $record['in_accept_list'] = true;
50
            $record['in_reject_list'] = true;
51
        }
52
53
        return $record;
54
    }
55
56
    /**
57
     * Fetches list with white, black, accept, and reject ips.
58
     *
59
     * @param $list
60
     * @param $flag
61
     * @param $list_type
62
     *
63
     * @return array
64
     */
65
    public function getList($list, $flag, $list_type)
66
    {
67
        $result_list = [];
68
        foreach ($list as $item) {
69
            if ($flag == 2) {
70
                $set = $this->prepareList($item, $list_type);
71
                if (($list_type == 'white') || $list_type == 'black') {
72
                    $result_list[] = array_except($set, ['in_accept_list', 'in_reject_list']);
0 ignored issues
show
Bug introduced by
The function array_except 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

72
                    $result_list[] = /** @scrutinizer ignore-call */ array_except($set, ['in_accept_list', 'in_reject_list']);
Loading history...
73
                } else {
74
                    $result_list[] = array_except($set, ['in_whitelist', 'in_blacklist']);
75
                }
76
            }
77
            if ($flag == 4) {
78
                $result_list[] = $this->prepareList($item, $list_type);
79
            }
80
        }
81
82
        return $result_list;
83
    }
84
85
    /**
86
     * Get log instance based on type.
87
     *
88
     * @param null $log_type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $log_type is correct as it would always require null to be passed?
Loading history...
89
     *
90
     * @return ExceptionLog|FirewallLog|FirewallRequestsLogModel
91
     */
92
    public function getLogInstance($log_type = null)
93
    {
94
        if ($log_type == 'request_log') {
95
            return $request_log = new FirewallRequestsLogModel();
0 ignored issues
show
Unused Code introduced by
The assignment to $request_log is dead and can be removed.
Loading history...
96
        }
97
        if ($log_type == 'exception_log') {
98
            return $request_log = new ExceptionLog();
99
        }
100
101
        return $log = new FirewallLog();
0 ignored issues
show
Unused Code introduced by
The assignment to $log is dead and can be removed.
Loading history...
102
    }
103
104
    /**
105
     * Validating dates and dates must be in Y-m-d format.
106
     *
107
     * @param $from_date
108
     * @param $to_date
109
     *
110
     * @return bool
111
     */
112
    public function validateDates($from_date, $to_date)
113
    {
114
        $validation = Validator::make([
115
            'from_date' => $from_date,
116
            'to_date'   => $to_date,
117
        ], [
118
            'from_date' => 'date_format:Y-m-d',
119
            'to_date'   => 'date_format:Y-m-d|after:from_date',
120
        ]);
121
122
        return $validation->fails();
123
    }
124
125
    /**
126
     * Add where clause to a query if dates are present
127
     * in specified format i.e.; Y-m-d.
128
     *
129
     * @param $log
130
     * @param $from_date
131
     * @param $to_date
132
     *
133
     * @return mixed
134
     */
135
    public function addWhereBetweenClause($log, $from_date, $to_date)
136
    {
137
        // $from = Carbon::createFromFormat('Y-m-d', $from_date)->format('Y-m-d');
138
        // $to = Carbon::createFromFormat('Y-m-d', $to_date)->format('Y-m-d');
139
        return $log->whereDate('created_at', '>=', $from_date)->whereDate('created_at', '<=', $to_date);
140
    }
141
142
    /**
143
     * Adding pagination if config option for pagination is enabled.
144
     *
145
     * @param $log
146
     * @param null $log_type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $log_type is correct as it would always require null to be passed?
Loading history...
147
     *
148
     * @return mixed
149
     */
150
    public function addPagination($log, $log_type = null)
151
    {
152
        if ($log_type == 'request_log') {
153
            $is_pagination = config('firewall.firewall_requests_log_pagination.enabled');
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

153
            $is_pagination = /** @scrutinizer ignore-call */ config('firewall.firewall_requests_log_pagination.enabled');
Loading history...
154
            $records_per_page = config('firewall.firewall_requests_log_pagination.per_page');
155
        } elseif ($log_type == 'exception_log') {
156
            $is_pagination = config('firewall.exception_log_pagination.enabled');
157
            $records_per_page = config('firewall.exception_log_pagination.per_page');
158
        } else {
159
            $is_pagination = config('firewall.firewall_log_pagination.enabled');
160
            $records_per_page = config('firewall.firewall_log_pagination.per_page');
161
        }
162
163
        return $is_pagination ? $log->paginate($records_per_page) : $log->get();
164
    }
165
166
    /**
167
     * Fetches unique ip addresses.
168
     *
169
     * @return array
170
     */
171
    public function getUniqueIpAddresses()
172
    {
173
        $ip_addresses = array_merge_recursive(
174
            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

174
            /** @scrutinizer ignore-call */ 
175
            config('firewall.whitelist'),
Loading history...
175
            config('firewall.blacklist'),
176
            config('firewall.accept'),
177
            config('firewall.reject')
178
        );
179
        $unique_ip_addresses = array_unique($ip_addresses);
180
181
        return $unique_ip_addresses;
182
    }
183
184
    /**
185
     * Initializes ip status list with default values.
186
     *
187
     * @param $ip_address
188
     *
189
     * @return array
190
     */
191
    private function initializeIpStatusList($ip_address)
192
    {
193
        $ip_statuses = [
194
            'ip_address'     => $ip_address,
195
            'in_whitelist'   => false,
196
            'in_blacklist'   => false,
197
            'in_accept_list' => false,
198
            'in_reject_list' => false,
199
        ];
200
201
        return $ip_statuses;
202
    }
203
204
    /**
205
     * Fetches the statuses for ip addresses.
206
     *
207
     * @param $ip_address
208
     *
209
     * @return array
210
     */
211
    public function getIpStatus($ip_address)
212
    {
213
        $initialized_list = $this->initializeIpStatusList($ip_address);
214
        if (in_array($ip_address, 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

214
        if (in_array($ip_address, /** @scrutinizer ignore-call */ config('firewall.whitelist'))) {
Loading history...
215
            $initialized_list['in_whitelist'] = true;
216
        }
217
        if (in_array($ip_address, config('firewall.blacklist'))) {
218
            $initialized_list['in_blacklist'] = true;
219
        }
220
        if (in_array($ip_address, config('firewall.accept'))) {
221
            $initialized_list['in_accept_list'] = true;
222
        }
223
        if (in_array($ip_address, config('firewall.reject'))) {
224
            $initialized_list['in_reject_list'] = true;
225
        }
226
227
        return $initialized_list;
228
    }
229
}
230