Completed
Push — master ( 467822...2b6000 )
by Anton
04:04
created

PersistentCurl::send()   C

Complexity

Conditions 16
Paths 56

Size

Total Lines 73
Code Lines 50

Duplication

Lines 22
Ratio 30.14 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 73
rs 5.4156
cc 16
eloc 50
nc 56
nop 1

How to fix   Long Method    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
3
namespace Covery\Client\Transport;
4
5
use Covery\Client\IoException;
6
use Covery\Client\TimeoutException;
7
use Covery\Client\TransportInterface;
8
use GuzzleHttp\Psr7\Response;
9
use Psr\Http\Message\RequestInterface;
10
11
/**
12
 * Class PersistentCurl
13
 *
14
 * CURL implementation of Covery Transport, which does not close connections.
15
 * For usage in workers only - in most cases this implementation will reduce impact of SSL and improve latency
16
 * This transport is experimental
17
 *
18
 * @package Covery\Client\Transport
19
 */
20
class PersistentCurl implements TransportInterface
21
{
22
    private $timeoutMillis;
23
    private $curl;
24
25
    /**
26
     * Curl constructor.
27
     *
28
     * @param int|float $timeout Timeout in seconds
29
     * @throws \Exception
30
     */
31 View Code Duplication
    public function __construct($timeout)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        if (!is_int($timeout) && !is_float($timeout)) {
34
            throw new \InvalidArgumentException('Timeout must be integer or float');
35
        }
36
        if (!function_exists('curl_init')) {
37
            throw new \Exception('cURL extension not installed/enabled');
38
        }
39
40
        $this->timeoutMillis = floor($timeout * 1000);
41
        $this->curl = null;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function send(RequestInterface $request)
48
    {
49
        $headers = [];
50 View Code Duplication
        foreach ($request->getHeaders() as $name => $values) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            $headers[] = $name . ': ' . implode(', ', $values);
52
        }
53
54
        $before = microtime(true);
55
        if ($this->curl == null) {
56
            $this->curl = curl_init();
57
        }
58
        curl_setopt($this->curl, CURLOPT_URL, strval($request->getUri()));
59
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT_MS, $this->timeoutMillis);
60
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
61
        curl_setopt($this->curl, CURLOPT_POST, true);
62
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $request->getMethod());
63
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request->getBody()->getContents());
64
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
65
        curl_setopt($this->curl, CURLOPT_HEADER, 1);
66
67
        try {
68
            $request->getBody()->close();
69
        } catch (\Exception $ignore) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
        }
71
72
        $response = curl_exec($this->curl);
73
74
        $status = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
75
        $headerSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
76
        $errno  = curl_errno($this->curl);
77
        $error  = curl_error($this->curl);
78
79
        if ($errno === CURLE_OPERATION_TIMEOUTED) {
80
            // Timeout
81
            throw new TimeoutException(sprintf('Transport timeout after %.2f seconds wait', microtime(true) - $before));
82 View Code Duplication
        } elseif ($errno === CURLE_SSL_CACERT
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            || $errno === CURLE_SSL_CERTPROBLEM
84
            || $errno === CURLE_SSL_CIPHER
85
            || $errno === CURLE_SSL_CONNECT_ERROR
86
            || $errno === CURLE_SSL_PEER_CERTIFICATE
87
            || $errno === CURLE_SSL_ENGINE_NOTFOUND
88
            || $errno === CURLE_SSL_ENGINE_SETFAILED
89
        ) {
90
            $this->curl = null;
91
            // SSL error
92
            throw new IoException('Transport SSL error ' . $error, intval($errno));
93
        } elseif ($errno !== CURLE_OK) {
94
            $this->curl = null;
95
            // Other error
96
            throw new IoException('Curl error ' . $error, intval($errno));
97
        }
98
99
        if ($response === false) {
100
            $this->curl = null;
101
            throw new IoException(sprintf('Curl error. Received status %s, curl error %s', $status, $error));
102
        }
103
104
        $rawHeaders = substr($response, 0, $headerSize);
105
        $body = trim(substr($response, $headerSize));
106
107
        $rawHeaders = explode("\n", $rawHeaders);
108
        $headers = array();
109 View Code Duplication
        foreach ($rawHeaders as $row) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            $row = trim($row);
111
            $index = strpos($row, ':');
112
            if ($index > 0) {
113
                $headers[substr($row, 0, $index)] = trim(substr($row, $index + 1));
114
            }
115
        }
116
117
        // Building response
118
        return new Response($status, $headers, $body);
119
    }
120
}
121