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

Wrapper::overRideScore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace devtoolboxuk\cerberus\Wrappers;
4
5
use devtoolboxuk\soteria\SoteriaService;
6
7
abstract class Wrapper
8
{
9
10
    protected $soteria;
11
    private $options = [];
12
    private $results = null;
13
    private $score = 0;
14
    private $realScore = 0;
15
    private $params = [];
16
    private $name;
17
    private $active;
18
    private $reference;
19
20 4
    public function __construct()
21
    {
22 4
        $this->soteria = new SoteriaService();
23 4
    }
24
25 2
    public function getRealScore()
26
    {
27 2
        if (!$this->hasRealScore()) {
28
            return 0;
29
        }
30
31 2
        return $this->realScore;
32
    }
33
34 2
    private function hasRealScore()
35
    {
36 2
        return isset($this->realScore);
37
    }
38
39 4
    protected function overRideScore($data)
40
    {
41 4
        $sanitise = $this->soteria->sanitise(true);
42 4
        $this->score = $this->getRealScore();
43 4
44
        if (isset($data[1])) {
45
            $sanitise->disinfect($data[1], 'int');
46
            $this->score = (int)$sanitise->result()->getOutput();
47
        }
48
    }
49
50
    public function setOptions($reference, $options = [])
51
    {
52
        $this->reference = $reference;
53
        $this->options = $options;
54
        return $this;
55
    }
56
57
    public function inParams($name)
58
    {
59 4
        if (in_array($name, $this->getParams())) {
60
            return true;
61 4
        }
62 4
        return false;
63
    }
64
65
    protected function getParams()
66
    {
67
        return $this->params;
68
    }
69
70
    protected function setParams($params)
71
    {
72
        $this->params = explode("|", $params);
73
        return $this;
74
    }
75
76
    public function getRuleOption($name, $score)
77
    {
78
        if (!$this->hasRuleOption($name)) {
79 4
            return $score;
80
        }
81 4
82
        return $this->options[$this->name][$name];
83
    }
84
85
    public function hasRuleOption($name)
86
    {
87
        return isset($this->options[$this->name][$name]);
88
    }
89
90
    public function getResult()
91
    {
92
        return $this->results;
93
    }
94 4
95
    protected function pushResult($result)
96 4
    {
97
        array_unshift($this->results, $result);
98
    }
99
100
    protected function sanitizeReference()
101
    {
102
        return str_replace(" ", "", strip_tags(trim($this->getReference())));
103
    }
104
105 4
    protected function getReference()
106
    {
107 4
        return $this->reference;
108 4
    }
109 4
110
    protected function setReference($reference)
111 4
    {
112
        $this->reference = $reference;
113 4
        return $this;
114 4
    }
115
116
    protected function initWrapper($name)
117 4
    {
118
        $this->setName($name);
119 4
        $this->setRules();
120
    }
121 4
122 4
    protected function setName($name)
123 4
    {
124 4
        $this->name = $name;
125
        return $this;
126 4
    }
127
128 4
    private function setRules()
129 2
    {
130
        $this->options = $this->getOption($this->getName());
131
132 4
        $this->setRealScore($this->getOption('score'));
133
        $this->setActive($this->getOption('active'));
134
        $this->setParams($this->getOption('params'));
135 4
    }
136
137 4
    public function getOption($name)
138
    {
139
        if (!$this->hasOption($name)) {
140 4
            return null;
141
        }
142 4
143
        return $this->options[$name];
144
    }
145 4
146
    public function hasOption($name)
147 4
    {
148 4
        return isset($this->options[$name]);
149
    }
150 4
151
    private function getName()
152 4
    {
153 4
        return $this->name;
154
    }
155 2
156
    private function setRealScore($score)
157 2
    {
158 2
        $this->realScore = $score;
159 2
    }
160
161
    private function setActive($active)
162
    {
163 2
        $this->active = $active;
164
    }
165
166 2
    protected function setResult()
167
    {
168 2
        if ($this->getActive()) {
169
            $this->results[$this->getName()] = $this->getScore();
170
            $this->score = $this->getScore();
171 4
        } else {
172
            $this->score = 0;
173 4
        }
174
        return $this;
175
    }
176
177 4
    private function getActive()
178
    {
179
        return $this->active;
180 2
    }
181
182 2
    public function getScore()
183 2
    {
184
        if (!$this->hasScore()) {
185 4
            return 0;
186
        }
187 4
188
        return $this->score;
189
    }
190
191
    protected function setScore($score)
192
    {
193
        $this->score = $score;
194
    }
195
196
    private function hasScore()
197
    {
198
        return isset($this->score);
199
    }
200
201
}