Passed
Branch master (8516d0)
by Rob
06:46
created

AbstractCerberus::setOptionsWithFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace devtoolboxuk\cerberus;
4
5
use devtoolboxuk\soteria\SoteriaService;
6
7
abstract class AbstractCerberus
8
{
9
10
    protected $options = [];
11
    protected $results = [];
12
    protected $result = [];
13
    protected $score = 0;
14
    protected $soteria;
15
16
    public function __construct()
17
    {
18
        $this->soteria = new SoteriaService();
19
        $this->setOptions();
20
    }
21
22
    /**
23
     * @param array $options
24
     */
25 4
    public function setOptions($options = [])
26
    {
27 4
        $baseOptions = new BaseOptions();
28
        $this->options = $this->arrayMergeRecursiveDistinct($baseOptions->getOptions(), $options);
29 4
    }
30
31 4
    /**
32 4
     * @param $file
33 4
     */
34
    public function setOptionsWithFile($file)
35 4
    {
36
        if ($this->isYamlLoaded()) {
37
            $this->soteria->sanitise()->disinfect($file, 'url');
38
            if ($this->soteria->sanitise()->result()->isValid()) {
39
                $this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
40
            } else {
41 4
                if (file_exists($file)) {
42
                    $this->options = $this->arrayMergeRecursiveDistinct($this->options, yaml_parse_file($file));
43 4
                }
44
            }
45
        }
46
    }
47 4
48
    /**
49
     * @return bool
50
     */
51
    private function isYamlLoaded()
52
    {
53
        return extension_loaded('yaml');
54 4
    }
55
56 4
    /**
57
     * @param array $merged
58
     * @param array $array2
59
     * @return array
60
     */
61
    private function arrayMergeRecursiveDistinct($merged = [], $array2 = [])
62
    {
63
        if (empty($array2)) {
64 4
            return $merged;
65
        }
66 4
67 2
        foreach ($array2 as $key => &$value) {
68 2
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
69
                $merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
70 4
            } else {
71
                $merged[$key] = $value;
72
            }
73
        }
74
        return $merged;
75
    }
76
77 2
    /**
78
     * @param $handler
79 2
     */
80 2
    protected function processWrappers($handler)
81
    {
82
        $options = $this->getOption('Detection');
83
84 4
        foreach ($handler->getWrappers() as $wrapper) {
85
86 4
            $wrapper->setOptions($handler->getValue(), $options['Rules']);
87 4
            $wrapper->process();
88 4
            $this->addResult($wrapper->getScore(), $wrapper->getResult());
89 4
        }
90
    }
91
92
    /**
93
     * @param $name
94
     * @return mixed|null
95
     */
96
    private function getOption($name)
97
    {
98
        if (!$this->hasOption($name)) {
99
            return null;
100
        }
101
102
        return $this->options[$name];
103
    }
104
105
    /**
106
     * @param $name
107
     * @return bool
108
     */
109
    private function hasOption($name)
110 4
    {
111
        return isset($this->options[$name]);
112
    }
113 4
114 4
    /**
115
     * @param $score
116 4
     * @param $result
117 4
     * @return $this
118
     */
119
    protected function addResult($score, $result)
120
    {
121
        if (is_array($result)) {
122
            $this->addScore($score);
123
            $this->result = array_merge($this->result, $result);
124
        }
125
        return $this;
126
    }
127
128
    /**
129
     * @param $score
130
     * @return $this
131 4
     */
132 4
    private function addScore($score)
133
    {
134
        $this->score += $score;
135
        return $this;
136
    }
137
138
    protected function clearResults()
139 4
    {
140
        $this->results = [];
141 4
        $this->result = [];
142
        $this->score = 0;
143
    }
144
145 4
    /**
146 4
     * @return array
147 4
     */
148
    protected function getOptions()
149 4
    {
150
        return $this->options;
151
    }
152 4
153
154
}