Completed
Push — master ( f4ceda...50d506 )
by Greg
02:19
created

BrowserMob   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 180
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 15
Bugs 6 Features 3
Metric Value
wmc 41
c 15
b 6
f 3
lcom 1
cbo 6
dl 18
loc 180
rs 8.2769

10 Methods

Rating   Name   Duplication   Size   Complexity  
B _initialize() 0 21 5
A __pingProxy() 0 10 2
C __setProxyCapabilities() 0 49 17
A getProxyPort() 0 4 1
A openProxy() 0 13 3
A closeProxy() 0 8 2
A startHar() 9 9 2
A startPage() 9 9 2
A getHar() 0 4 1
B __call() 0 26 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BrowserMob often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BrowserMob, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Codeception\Extension;
3
4
use Codeception\Module;
5
use Codeception\Exception\ModuleException;
6
use Codeception\Exception\ModuleConfigException;
7
use \PHPBrowserMobProxy_Client as BMP;
8
use \Requests;
9
use \RuntimeException;
10
11
/**
12
 * @method void _open()
13
 * @method void _close()
14
 * @method string _newHar(string $label='')
15
 * @method string _newPage(string $label='')
16
 * @method string _blacklist(string $regexp, integer $status_code)
17
 * @method string _whitelist(string $regexp, integer $status_code)
18
 * @method string _basicAuth(string $domain, string[] $options)
19
 * @method string _headers(string[] $options)
20
 * @method string _responseInterceptor(string $js)
21
 * @method string _requestInterceptor(string $js)
22
 * @method Requests_Response _limits(string[] $options)
23
 * @method Requests_Response _timeouts(string[] $options)
24
 * @method string _remapHosts(string $address, string $ip_address)
25
 * @method string _waitForTrafficToStop(integer $quiet_period, integer $timeout)
26
 * @method string _clearDnsCache()
27
 * @method string _rewriteUrl(string $match, string $replace)
28
 * @method string _retry(integer $retry_count)
29
 */
30
class BrowserMob extends Module
31
{
32
33
    protected $config = ['host', 'port', 'autostart', 'blacklist', 'whitelist', 'limits', 'timeouts', 'redirect', 'retry', 'basicAuth', 'littleproxy'];
34
35
    protected $requiredFields = ['host'];
36
37
    protected $response;
38
39
    private $bmp;
40
41
    /**
42
     * @codeCoverageIgnore
43
     * @ignore Codeception specific
44
     */
45
    public function _initialize()
46
    {
47
        $host = $this->config['host'];
48
        if (isset($this->config['port'])) {
49
            $host = $host.':'.$this->config['port'];
50
        }
51
52
        // test if proxy is available
53
        if (static::__pingProxy($host)) {
54
            $this->bmp = new BMP($host);
55
        } else {
56
            throw new ModuleConfigException(__CLASS__, "Proxy '{$host}' cannot be reached");
57
        }
58
59
        // start a new BrowserMobProxy session
60
        if (isset($this->config['autostart'])) {
61
            if (true === (bool) $this->config['autostart']) {
62
                $this->openProxy();
63
            }
64
        }
65
    }
66
67
    protected static function __pingProxy($url)
68
    {
69
        try {
70
            $response = Requests::get('http://'.$url.'/proxy/');
71
        } catch (\Exception $e) {
72
            throw new ModuleException(__CLASS__, $e->getMessage());
73
        }
74
75
        return $response->success;
76
    }
77
78
    protected function __setProxyCapabilities($options)
79
    {
80
        foreach ($options as $config => $data) {
81
            if (false === empty($data)) {
82
                switch ((string) $config) {
83
                    case 'blacklist':
84
                        foreach ($data['patterns'] as $pattern) {
85
                            $this->_blacklist($pattern, $data['code']);
86
                            if (false === $this->response->success) {
87
                                break;
88
                            }
89
                        }
90
                        break;
91
                    case 'whitelist':
92
                        $patterns = implode(',', $data['patterns']);
93
                        $this->_whitelist($patterns, $data['code']);
94
                        if (false === $this->response->success) {
95
                            break;
96
                        }
97
                        break;
98
                    case 'limits':
99
                        $this->_limits($data);
100
                        break;
101
                    case 'timeouts':
102
                        $this->_timeouts($data);
103
                        break;
104
                    case 'redirect':
105
                        foreach ($data as $entry) {
106
                            $this->_remapHosts($entry['domain'], $entry['ip']);
107
                            if (false === $this->response->success) break;
108
                        }
109
                        break;
110
                    case 'retry':
111
                        $this->_retry($data);
112
                        break;
113
                    case 'basicAuth':
114
                        foreach ($data as $entry) {
115
                            $this->_basicAuth($entry['domain'], $entry['options']);
116
                            if (false === $this->response->success) {
117
                                break;
118
                            }
119
                        }
120
                        break;
121
                    default:
122
                        // do nothing
123
                }
124
            }
125
        }
126
    }
127
128
    public function getProxyPort()
129
    {
130
        return $this->bmp->port;
131
    }
132
133
    public function openProxy($capabilities = null)
134
    {
135
        try {
136
            $this->bmp->open();
137
        } catch (\Exception $e) {
138
            throw new ModuleException(__CLASS__, $e->getMessage());
139
        }
140
        if (empty($capabilities)) {
141
            $capabilities = $this->config;
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $capabilities. This often makes code more readable.
Loading history...
142
        }
143
        $this->__setProxyCapabilities($capabilities);
144
        return $this->getProxyPort();
145
    }
146
147
    public function closeProxy()
148
    {
149
        try {
150
            $this->bmp->close();
151
        } catch (\Exception $e) {
152
            throw new ModuleException(__CLASS__, $e->getMessage());
153
        }
154
    }
155
156 View Code Duplication
    public function startHar()
157
    {
158
        try {
159
            $this->response = $this->bmp->newHar();
160
        } catch (\Exception $e) {
161
            throw new ModuleException(__CLASS__, $e->getMessage());
162
        }
163
        return $this->response->success;
164
    }
165
166 View Code Duplication
    public function startPage()
167
    {
168
        try {
169
            $this->response = $this->bmp->newPage();
170
        } catch (\Exception $e) {
171
            throw new ModuleException(__CLASS__, $e->getMessage());
172
        }
173
        return $this->response->success;
174
    }
175
176
    public function getHar()
177
    {
178
        return $this->bmp->har;
179
    }
180
181
    // magic function that exposes BrowserMobProxy API pulic methods
182
    public function __call($name, $args)
183
    {
184
        // check if is a command call
185
        if (preg_match('/^_[A-z]+$/', $name)) {
186
            // extract standard method name
187
            $name = preg_filter('/_/', '', $name);
188
            // set call array for calling method
189
            $call = array($this->bmp, $name);
190
            // check if method is callable
191
            if (is_callable($call)) {
192
                $ret = call_user_func_array($call, $args);
193
                if (get_class($ret) === 'Requests_Response') {
194
                    $this->response = $ret;
195
                    if (false === $ret->success) {
196
                        throw new ModuleConfigException(__CLASS__, "Proxy response error '{$ret->status_code}' {$ret->body}");
197
                    }
198
                }
199
            } else {
200
                throw new RuntimeException("Method ${name} does not exist or is not callable");
201
            }
202
        } else {
203
            throw new RuntimeException("Method ${method} does not exist or is not callable");
204
        }
205
        $ret = (isset($ret)) ? $ret : null;
206
        return $ret;
207
    }
208
209
}
210