Completed
Push — master ( e09e40...2564b7 )
by Nils
02:09
created

Configuration::initSessionContainer()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace whm\Smoke\Config;
4
5
use phmLabs\Components\Annovent\Dispatcher;
6
use PhmLabs\Components\Init\Init;
7
use Symfony\Component\Yaml\Yaml;
8
use whm\Html\Uri;
9
use whm\Smoke\Http\Session;
10
use whm\Smoke\Rules\Rule;
11
use whm\Smoke\Scanner\SessionContainer;
12
13
class Configuration
14
{
15
    const DEFAULT_SETTINGS = 'analyze.yml';
16
17
    private $startUri;
18
19
    private $rules = [];
20
21
    private $configArray;
22
23
    private $eventDispatcher;
24
25
    private $extensions = array();
26
27
    private $runLevels = array();
28
29
    /**
30
     * @var SessionContainer
31
     */
32
    private $sessionContainer;
33
34
    public function __construct(Uri $uri, Dispatcher $eventDispatcher, array $configArray, array $defaultSettings = null)
35
    {
36
        $this->eventDispatcher = $eventDispatcher;
37
        Init::registerGlobalParameter('_configuration', $this);
38
39
        $this->initConfigArray($configArray, $defaultSettings);
40
41
        if (array_key_exists('sessions', $this->configArray)) {
42
            $this->initSessionContainer($this->configArray['sessions']);
43
        } else {
44
            $this->sessionContainer = new SessionContainer();
45
        }
46
47
        if (array_key_exists('extensions', $this->configArray)) {
48
            $this->addListener($this->configArray['extensions']);
49
        }
50
51
        if (!array_key_exists('rules', $this->configArray)) {
52
            $this->configArray['rules'] = [];
53
        }
54
55
        $this->startUri = $uri;
56
        $this->initRules($this->configArray['rules']);
57
    }
58
59
    private function initConfigArray(array $configArray, array $defaultSettings = null)
60
    {
61
        if ($defaultSettings === null) {
62
            $defaultSettings = Yaml::parse(file_get_contents(__DIR__ . '/../settings/' . self::DEFAULT_SETTINGS));
63
        }
64
65
        if (count($configArray) === 0) {
66
            $configArray = $defaultSettings;
67
        }
68
69
        if (array_key_exists('options', $configArray)) {
70
            if (array_key_exists('extendDefault', $configArray['options'])) {
71
                if ($configArray['options']['extendDefault'] === true) {
72
                    $configArray = array_replace_recursive($defaultSettings, $configArray);
73
                }
74
            }
75
        }
76
77
        $this->configArray = $configArray;
78
    }
79
80
    private function initSessionContainer(array $sessionsArray)
81
    {
82
        $this->sessionContainer = new SessionContainer();
83
84
        foreach ($sessionsArray as $sessionName => $sessionsElement) {
85
            $session = new Session();
86
87
            if (array_key_exists('cookies', $sessionsElement)) {
88
                foreach ($sessionsElement['cookies'] as $key => $value) {
89
                    $session->addCookie($key, $value);
90
                }
91
            }
92
93
            $this->sessionContainer->addSession($sessionName, $session);
94
        }
95
    }
96
97
    private function addListener(array $listenerArray)
98
    {
99
        foreach ($listenerArray as $key => $listenerConfig) {
100
            $extension = Init::initialize($listenerConfig);
101
            $this->extensions[$key] = $extension;
102
            $this->eventDispatcher->connectListener($extension);
103
        }
104
    }
105
106
    /**
107
     * @return Uri
108
     */
109
    public function getStartUri()
110
    {
111
        return $this->startUri;
112
    }
113
114
    /**
115
     * This function initializes all the rules and sets the log level.
116
     *
117
     * @param array $rulesArray
118
     */
119
    private function initRules(array $rulesArray)
120
    {
121
        $this->rules = Init::initializeAll($rulesArray);
122
    }
123
124
    /**
125
     * Returns the log level of a given rule.
126
     *
127
     * @param string $key
128
     *
129
     * @return int
130
     */
131
    public function getRuleRunLevel($key)
132
    {
133
        return $this->runLevels[$key];
134
    }
135
136
    /**
137
     * @return Rule[]
138
     */
139
    public function getRules()
140
    {
141
        return $this->rules;
142
    }
143
144
    public function getSessionContainer()
145
    {
146
        return $this->sessionContainer;
147
    }
148
149
    public function hasSection($section)
150
    {
151
        return array_key_exists($section, $this->configArray);
152
    }
153
154
    /**
155
     * @param $section
156
     *
157
     * @return array
158
     */
159
    public function getSection($section)
160
    {
161
        if ($this->hasSection($section)) {
162
            return $this->configArray[$section];
163
        } else {
164
            throw new \RuntimeException('The section (' . $section . ') you are trying to access does not exist.');
165
        }
166
    }
167
168 View Code Duplication
    public function getExtension($name)
169
    {
170
        if (array_key_exists($name, $this->extensions)) {
171
            return $this->extensions[$name];
172
        } else {
173
            throw new \RuntimeException('The extension ("' . $name . '") you are trying to access does not exist. Registered extensions are: ' . implode(' ,', array_keys($this->extensions)) . '.');
174
        }
175
    }
176
177
    public function addExtension($name, $extension)
178
    {
179
        $this->extensions[$name] = $extension;
180
        $this->eventDispatcher->connectListener($extension);
181
    }
182
183
    public function getConfigArray()
184
    {
185
        return $this->configArray;
186
    }
187
}
188