1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtoolboxuk\cerberus; |
4
|
|
|
|
5
|
|
|
use devtoolboxuk\soteria\SoteriaService; |
6
|
|
|
use devtoolboxuk\utilitybundle\UtilityService; |
7
|
|
|
|
8
|
|
|
abstract class AbstractService |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $options = []; |
12
|
|
|
protected $results = []; |
13
|
|
|
protected $result = []; |
14
|
|
|
protected $score = 0; |
15
|
|
|
protected $soteria; |
16
|
|
|
|
17
|
|
|
protected $output; |
18
|
|
|
protected $handlerName; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
protected $arrayUtility; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->initiateServices(); |
26
|
|
|
$this->setOptions(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function initiateServices() |
30
|
|
|
{ |
31
|
|
|
$this->soteria = new SoteriaService(); |
32
|
|
|
$utilityService = new UtilityService(); |
33
|
|
|
$this->arrayUtility = $utilityService->arrays(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @TODO - Add pass in json file |
38
|
|
|
* @param $file |
39
|
|
|
*/ |
40
|
|
|
public function setOptionsWithFile($file) |
41
|
|
|
{ |
42
|
|
|
if ($this->isYamlLoaded()) { |
43
|
|
|
$this->soteria->sanitise()->disinfect($file, 'url'); |
44
|
|
|
if ($this->soteria->sanitise()->result()->isValid()) { |
45
|
|
|
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file)); |
46
|
|
|
} else { |
47
|
|
|
if (file_exists($file)) { |
48
|
|
|
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function isYamlLoaded() |
58
|
|
|
{ |
59
|
|
|
return extension_loaded('yaml'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $handler |
64
|
|
|
*/ |
65
|
|
|
protected function processWrappers($handler) |
66
|
|
|
{ |
67
|
|
|
$options = $this->getOption('Detection'); |
68
|
|
|
|
69
|
|
|
foreach ($handler->getWrappers() as $wrapper) { |
70
|
|
|
|
71
|
|
|
$wrapper->setOptions($handler->getInternalInput(), $options['Rules']); |
72
|
|
|
$wrapper->process(); |
73
|
|
|
$this->addResult($wrapper->getScore(), $wrapper->getResult()); |
74
|
|
|
$this->addOutput($wrapper->getOutput()); |
75
|
|
|
$handler->setInternalInput($wrapper->getOutput()); |
76
|
|
|
|
77
|
|
|
$this->addHandlerName($handler->getHandlerReference()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $name |
83
|
|
|
* @return mixed|null |
84
|
|
|
*/ |
85
|
|
|
private function getOption($name) |
86
|
|
|
{ |
87
|
|
|
if (!$this->hasOption($name)) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->options[$name]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $name |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
private function hasOption($name) |
99
|
|
|
{ |
100
|
|
|
return isset($this->options[$name]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $score |
105
|
|
|
* @param $result |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
protected function addResult($score, $result) |
109
|
|
|
{ |
110
|
|
|
if (is_array($result)) { |
111
|
|
|
$this->addScore($score); |
112
|
|
|
$this->result = array_merge($this->result, $result); |
113
|
|
|
} |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $score |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
private function addScore($score) |
122
|
|
|
{ |
123
|
|
|
$this->score += $score; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function addOutput($value) |
128
|
|
|
{ |
129
|
|
|
$this->output = $value; |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function addHandlerName($handlerName) |
134
|
|
|
{ |
135
|
|
|
$this->handlerName = $handlerName; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function getOutput() |
140
|
|
|
{ |
141
|
|
|
return $this->output; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function getHandlerName() |
145
|
|
|
{ |
146
|
|
|
return $this->handlerName; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function clearResults() |
150
|
|
|
{ |
151
|
|
|
$this->results = []; |
152
|
|
|
$this->result = []; |
153
|
|
|
$this->score = 0; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function getOptions() |
160
|
|
|
{ |
161
|
|
|
return $this->options; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $options |
166
|
|
|
*/ |
167
|
|
|
public function setOptions($options = []) |
168
|
|
|
{ |
169
|
|
|
$baseOptions = new BaseOptions(); |
170
|
|
|
$this->options = $this->arrayUtility->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
} |