Completed
Push — master ( 56ea51...11e69c )
by Rob
04:30 queued 10s
created

BotWrapper::setRealReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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