ClientBridge   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 17.65%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 168
ccs 9
cts 51
cp 0.1765
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A request() 0 16 1
A requestAsync() 0 3 1
A send() 0 3 1
A sendAsync() 0 3 1
A stream() 0 3 1
A getConfig() 0 6 3
A configureDefaults() 0 6 1
A getConfigDefaults() 0 6 1
A setConfig() 0 4 1
A createRequest() 0 22 2
A applyConfig() 0 9 2
A getPhantomJsClient() 0 9 2
1
<?php
2
3
namespace ByTIC\GouttePhantomJs\Clients\PhantomJs;
4
5
use JonnyW\PhantomJs\Client as PhantomJsBaseClient;
6
use JonnyW\PhantomJs\Http\RequestInterface;
7
use PhantomInstaller\PhantomBinary;
8
use Symfony\Contracts\HttpClient\HttpClientInterface;
9
use Symfony\Contracts\HttpClient\ResponseInterface;
10
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
11
12
/**
13
 * Class PhantomJsClientBridge
14
 * @package ByTIC\GouttePhantomJs\Clients\PhantomJs
15
 */
16
class ClientBridge implements HttpClientInterface
17
{
18
    protected $phantomJsClient = null;
19
20
    /** @var array Default request options */
21
    private $config;
22
23
    /**
24
     * ClientBridge constructor.
25
     *
26
     * @param array $config
27
     */
28 1
    public function __construct(array $config = [])
29
    {
30 1
        $this->configureDefaults($config);
31 1
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function request(string $method, string $url, array $options = []): ResponseInterface
37
    {
38
        $client  = $this->getPhantomJsClient();
39
        $request = $this->createRequest($client, $method, $url, $options);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getPhantomJsClient() on line 38 can be null; however, ByTIC\GouttePhantomJs\Cl...Bridge::createRequest() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
        $request = $this->applyConfig($request);
41
42
        /**
43
         * @see \JonnyW\PhantomJs\Http\Response
44
         **/
45
        $response = $client->getMessageFactory()->createResponse();
46
47
        // Send the request
48
        $client->send($request, $response);
49
50
        return ResponseFormatter::format($response);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function requestAsync($method, $uri = '', array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $uri is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function send(RequestInterface $request, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function sendAsync(RequestInterface $request, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function stream($responses, float $timeout = null): ResponseStreamInterface
78
    {
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function getConfig($option = null)
85
    {
86
        return $option === null
87
            ? $this->config
88
            : (isset($this->config[$option]) ? $this->config[$option] : null);
89
    }
90
91
    /**
92
     * Configures the default options for a client.
93
     *
94
     * @param array $config
95
     */
96 1
    protected function configureDefaults(array $config)
97
    {
98 1
        $defaults = $this->getConfigDefaults();
99
100 1
        $this->config = $config + $defaults;
101 1
    }
102
103
    /**
104
     * @return array
105
     */
106 1
    protected function getConfigDefaults()
107
    {
108
        return [
109 1
            'request_delay' => false
110
        ];
111
    }
112
113
    /**
114
     * @param $option
115
     * @param $value
116
     *
117
     * @return mixed
118
     */
119
    public function setConfig($option, $value)
120
    {
121
        return $this->config[$option] = $value;
122
    }
123
124
    /**
125
     * @param PhantomJsBaseClient $client
126
     * @param $method
127
     * @param string $uri
128
     * @param array $parameters
129
     *
130
     * @return RequestInterface
131
     */
132
    protected function createRequest($client, $method, $uri = '', array $parameters = [])
133
    {
134
        /**
135
         * @see \JonnyW\PhantomJs\Http\Request
136
         **/
137
        $request = $client->getMessageFactory()->createRequest($uri, $method);
138
        $request->addHeader(
139
            'User-Agent',
140
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'
141
        );
142
143
        $request->addHeader(
144
            'Content-Type',
145
            'application/x-www-form-urlencoded'
146
        );
147
148
        if (isset($parameters['form_params'])) {
149
            $request->setRequestData($parameters['form_params']);
150
        }
151
152
        return $request;
153
    }
154
155
    /**
156
     * @param RequestInterface $request
157
     *
158
     * @return RequestInterface
159
     */
160
    protected function applyConfig($request)
161
    {
162
        $requestDelay = $this->getConfig('request_delay');
163
        if ($requestDelay > 0) {
164
            $request->setDelay($requestDelay);
165
        }
166
167
        return $request;
168
    }
169
170
171
    /**
172
     * @return PhantomJsBaseClient|null
173
     */
174
    protected function getPhantomJsClient()
175
    {
176
        if ($this->phantomJsClient === null) {
177
            $this->phantomJsClient = PhantomJsBaseClient::getInstance();
178
            $this->phantomJsClient->getEngine()->setPath(PhantomBinary::getBin());
179
        }
180
181
        return $this->phantomJsClient;
182
    }
183
}
184