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

BrowserMob::__setProxyCapabilities()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 3 Features 3
Metric Value
c 10
b 3
f 3
dl 0
loc 49
rs 5.1117
cc 17
eloc 37
nc 17
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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