BotsWrapper::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace devtoolboxuk\cerberus\Wrappers;
4
5
class BotsWrapper extends Base
6
{
7
8
    private $userAgentFound = 0;
9
10
    public function process()
11
    {
12
        $this->initWrapper($this->setLocalName());
13
14
        $this->setRealReference($this->getReference());
15
        $this->detectBot();
16
17
        if ($this->userAgentFound > 0) {
18
            $this->setScore($this->score);
19
        }
20
        $this->setResult();
21
    }
22
23
    private function setLocalName()
24
    {
25
        $name = str_replace(__NAMESPACE__ . '\\', '', __CLASS__);
26
        return str_replace('Wrapper', '', $name);
27
    }
28
29
    private function setRealReference($reference = '')
30
    {
31
        if ($reference == '') {
32
            $this->setReference($this->getUserAgent());
33
        }
34
    }
35
36
    private function getUserAgent()
37
    {
38
        return isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';
39
    }
40
41
    private function detectBot()
42
    {
43
44
        $params = $this->getParams();
45
        if (empty($params)) {
46
            return;
47
        }
48
49
        foreach ($params as $param) {
50
            if ($param != '') {
51
                $data = explode(":", $param);
52
                if (strpos(strtolower($this->sanitizeReference()), strtolower($data[0])) !== false) {
53
                    $this->detectedBot($data);
54
                }
55
            }
56
        }
57
58
    }
59
60
    private function detectedBot($data)
61
    {
62
        if (strpos(strtolower($this->getReference()), $data) !== false) {
63
            $this->setRealScore();
64
        }
65
    }
66
67
    private function setRealScore($data = [])
68
    {
69
        $this->overRideScore($data);
70
71
        if ($this->score > 0) {
72
            $this->userAgentFound++;
73
        }
74
    }
75
76
}