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->getInput(), $options['Rules']); |
72
|
|
|
$wrapper->process(); |
73
|
|
|
$this->addResult($wrapper->getScore(), $wrapper->getResult()); |
74
|
|
|
$this->addOutput($wrapper->getOutput()); |
75
|
|
|
$this->addHandlerName($handler->getName()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name |
81
|
|
|
* @return mixed|null |
82
|
|
|
*/ |
83
|
|
|
private function getOption($name) |
84
|
|
|
{ |
85
|
|
|
if (!$this->hasOption($name)) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->options[$name]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $name |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
private function hasOption($name) |
97
|
|
|
{ |
98
|
|
|
return isset($this->options[$name]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $score |
103
|
|
|
* @param $result |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
protected function addResult($score, $result) |
107
|
|
|
{ |
108
|
|
|
if (is_array($result)) { |
109
|
|
|
$this->addScore($score); |
110
|
|
|
$this->result = array_merge($this->result, $result); |
111
|
|
|
} |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $score |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
private function addScore($score) |
120
|
|
|
{ |
121
|
|
|
$this->score += $score; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function addOutput($value) |
126
|
|
|
{ |
127
|
|
|
$this->output = $value; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getOutput() |
132
|
|
|
{ |
133
|
|
|
return $this->output; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function addHandlerName($handlerName) |
137
|
|
|
{ |
138
|
|
|
$this->handlerName = $handlerName; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function getHandlerName() |
143
|
|
|
{ |
144
|
|
|
return $this->handlerName; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function clearResults() |
148
|
|
|
{ |
149
|
|
|
$this->results = []; |
150
|
|
|
$this->result = []; |
151
|
|
|
$this->score = 0; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
protected function getOptions() |
158
|
|
|
{ |
159
|
|
|
return $this->options; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $options |
164
|
|
|
*/ |
165
|
|
|
public function setOptions($options = []) |
166
|
|
|
{ |
167
|
|
|
$baseOptions = new BaseOptions(); |
168
|
|
|
$this->options = $this->utilityService->arrays()->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
} |