Completed
Push — master ( 6249fb...a71aed )
by Nils
02:16
created

Configuration   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 223
Duplicated Lines 3.59 %

Coupling/Cohesion

Components 5
Dependencies 10

Importance

Changes 0
Metric Value
wmc 34
lcom 5
cbo 10
dl 8
loc 223
rs 9.68
c 0
b 0
f 0

17 Methods

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