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

StringLengthWrapper::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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