Completed
Push — master ( fe5a84...c56050 )
by Greg
05:20 queued 38s
created

BrowserMob::__setProxyCapabilities()   C

Complexity

Conditions 13
Paths 34

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 44
rs 5.1234
cc 13
eloc 32
nc 34
nop 0

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
10
class BrowserMob extends Module
11
{
12
13
    protected $config = ['host', 'port', 'blacklist', 'whitelist', 'limits', 'timeouts', 'redirect', 'retry'];
14
15
    protected $requiredFields = ['host'];
16
17
    protected $lastResponse;
18
19
    private $bmp;
20
21
    /**
22
     * @codeCoverageIgnore
23
     * @ignore Codeception specific
24
     */
25
    public function _initialize()
26
    {
27
        $host = $this->config['host'];
28
        if (isset($this->config['port'])) {
29
            $host = $host.':'.$this->config['port'];
30
        }
31
32
        // test if proxy is available
33
        if (static::__pingProxy($host)) {
34
            $this->bmp = new BMP($host);
35
        } else {
36
            throw new ModuleConfigException(__CLASS__, "Proxy '{$host}' cannot be reached");
37
        }
38
39
        // start a new BrowserMobProxy session
40
        $this->bmp->open();
41
42
        // set BrowserMobProxy options
43
        $this->__setProxyCapabilities();
44
45
    }
46
47
    protected static function __pingProxy($url)
48
    {
49
        try {
50
            $response = Requests::get('http://'.$url.'/proxy/');
51
        } catch(\Exception $e) {
52
            throw new ModuleException(__CLASS__, $e->getMessage());
53
        }
54
55
        return $response->success;
56
    }
57
58
    protected function __setProxyCapabilities()
59
    {
60
        $response = null;
61
62
        foreach ($this->config as $config => $data) {
63
            try {
64
                if (false === empty($data)) {
65
                    switch ($config) {
66
                        case 0: // fix a weird PHP behaviour: when $config === 0 then go in 'blacklist'
67
                            break;
68
                        case 'blacklist':
69
                            $response = $this->bmp->blacklist($data);
0 ignored issues
show
Bug introduced by
The call to blacklist() misses a required argument $status_code.

This check looks for function calls that miss required arguments.

Loading history...
70
                            break;
71
                        case 'whitelist':
72
                            $response = $this->bmp->whitelist($data);
0 ignored issues
show
Bug introduced by
The call to whitelist() misses a required argument $status_code.

This check looks for function calls that miss required arguments.

Loading history...
73
                            break;
74
                        case 'limits':
75
                            $response = $this->bmp->limits($data);
76
                            break;
77
                        case 'timeouts':
78
                            $response = $this->bmp->timeouts($data);
79
                            break;
80
                        case 'redirect':
81
                            $response = $this->bmp->remapHosts($data);
0 ignored issues
show
Bug introduced by
The call to remapHosts() misses a required argument $ip_address.

This check looks for function calls that miss required arguments.

Loading history...
82
                            break;
83
                        case 'retry':
84
                            $response = $this->bmp->retry($data);
85
                            break;
86
                        default:
87
                            // do nothing
88
                    }
89
                }
90
            } catch(\Exception $e) {
91
                throw new ModuleConfigException(__CLASS__, $e->getMessage());
92
            }
93
            
94
            if (get_class($response) === 'Request')
95
            {
96
                if (false === $response->success) {
97
                    throw new ModuleConfigException(__CLASS__, "Proxy response error '{$reponse->status_code}' {$respone->body}");
98
                }
99
            }
100
        }
101
    }
102
103
}
104