Completed
Push — master ( b89c1c...93e915 )
by Nils
08:46
created

Configuration   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 198
Duplicated Lines 4.04 %

Coupling/Cohesion

Components 5
Dependencies 7

Importance

Changes 0
Metric Value
wmc 31
lcom 5
cbo 7
dl 8
loc 198
rs 9.8
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 4
B initConfigArray() 0 20 6
A initSessionContainer() 0 16 4
A addListener() 0 8 2
A getStartUri() 0 4 1
A initRules() 0 4 1
A getRuleRunLevel() 0 4 1
A getRules() 0 4 1
A getSessionContainer() 0 4 1
A hasSection() 0 4 1
A getSection() 0 8 2
A getExtension() 8 8 2
A addExtension() 0 5 1
A getConfigArray() 0 4 1
A getClient() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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