Agent   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 3
dl 0
loc 162
rs 9.1199
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 0 34 11
B isMalicious() 0 24 6
A isBrowser() 0 16 5
A isPlatform() 0 16 5
B isDevice() 0 38 8
B isProperty() 0 24 6

How to fix   Complexity   

Complex Class

Complex classes like Agent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Agent, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Spinzar\Firewall\Middleware;
4
5
use Spinzar\Firewall\Abstracts\Middleware;
6
use Spinzar\Firewall\Events\AttackDetected;
7
use Jenssegers\Agent\Agent as Parser;
8
9
class Agent extends Middleware
10
{
11
    protected $parser;
12
13
    public function check($patterns)
14
    {
15
        $status = false;
16
17
        $this->parser = new Parser();
18
19
        if ($this->isMalicious()) {
20
            $status = true;
21
        }
22
23
        if (!$status && $this->isBrowser()) {
24
            $status = true;
25
        }
26
27
        if (!$status && $this->isPlatform()) {
28
            $status = true;
29
        }
30
31
        if (!$status && $this->isDevice()) {
32
            $status = true;
33
        }
34
35
        if (!$status && $this->isProperty()) {
36
            $status = true;
37
        }
38
39
        if ($status) {
40
            $log = $this->log();
41
42
            event(new AttackDetected($log));
43
        }
44
45
        return $status;
46
    }
47
48
    protected function isMalicious()
49
    {
50
        $agent = $this->parser->getUserAgent();
51
52
        if (empty($agent) || ($agent == '-') || strstr($agent, '<?')) {
53
            return true;
54
        }
55
56
        $patterns = [
57
            '@"feed_url@',
58
            '@}__(.*)|O:@',
59
            '@J?Simple(p|P)ie(Factory)?@',
60
        ];
61
62
        foreach ($patterns as $pattern) {
63
            if (!preg_match($pattern, $agent) == 1) {
64
                continue;
65
            }
66
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    protected function isBrowser()
74
    {
75
        if (!$browsers = config('firewall.middleware.' . $this->middleware . '.browsers')) {
76
            return false;
77
        }
78
79
        if (!empty($browsers['allow']) && !in_array((string) $this->parser->browser(), (array) $browsers['allow'])) {
80
            return true;
81
        }
82
83
        if (in_array((string) $this->parser->browser(), (array) $browsers['block'])) {
84
            return true;
85
        }
86
87
        return false;
88
    }
89
90
    protected function isPlatform()
91
    {
92
        if (!$platforms = config('firewall.middleware.' . $this->middleware . '.platforms')) {
93
            return false;
94
        }
95
96
        if (!empty($platforms['allow']) && !in_array((string) $this->parser->platform(), (array) $platforms['allow'])) {
97
            return true;
98
        }
99
100
        if (in_array((string) $this->parser->platform(), (array) $platforms['block'])) {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
107
    protected function isDevice()
108
    {
109
        if (!$devices = config('firewall.middleware.' . $this->middleware . '.devices')) {
110
            return false;
111
        }
112
113
        $list = ['Desktop', 'Mobile', 'Tablet'];
114
115
        foreach ((array) $devices['allow'] as $allow) {
116
            if (!in_array($allow, $list)) {
117
                continue;
118
            }
119
120
            $function = 'is' . ucfirst($allow);
121
122
            if ($this->$function()) {
123
                continue;
124
            }
125
126
            return true;
127
        }
128
129
        foreach ((array) $devices['block'] as $block) {
130
            if (!in_array($block, $list)) {
131
                continue;
132
            }
133
134
            $function = 'is' . ucfirst($block);
135
136
            if (!$this->$function()) {
137
                continue;
138
            }
139
140
            return true;
141
        }
142
143
        return false;
144
    }
145
146
    protected function isProperty()
147
    {
148
        if (!$agents = config('firewall.middleware.' . $this->middleware . '.properties')) {
149
            return false;
150
        }
151
152
        foreach ((array) $agents['allow'] as $allow) {
153
            if ($this->parser->is((string) $allow)) {
154
                continue;
155
            }
156
157
            return true;
158
        }
159
160
        foreach ((array) $agents['block'] as $block) {
161
            if (!$this->parser->is((string) $block)) {
162
                continue;
163
            }
164
165
            return true;
166
        }
167
168
        return false;
169
    }
170
}
171