Completed
Push — master ( 88a5ed...6cb890 )
by Joel
03:51
created

ResponseReader::readResponse()   C

Complexity

Conditions 10
Paths 36

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.003
Metric Value
dl 0
loc 51
ccs 31
cts 32
cp 0.9688
rs 6
cc 10
eloc 28
nc 36
nop 2
crap 10.003

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 Http\Client\Socket;
4
5
use Http\Client\Exception\NetworkException;
6
use Http\Message\MessageFactory;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Method for reading response
12
 *
13
 * Mainly used by SocketHttpClient
14
 *
15
 * @author Joel Wurtz <[email protected]>
16
 */
17
trait ResponseReader
18
{
19
    /**
20
     * @var MessageFactory
21
     */
22
    protected $messageFactory;
23
24
    /**
25
     * Read a response from a socket
26
     *
27
     * @param RequestInterface $request
28
     * @param resource         $socket
29
     *
30
     * @throws NetworkException When the response cannot be read
31
     *
32
     * @return ResponseInterface
33
     */
34 59
    protected function readResponse(RequestInterface $request, $socket)
35
    {
36 59
        $headers  = [];
37 59
        $reason   = null;
38
39 59
        while (($line = fgets($socket)) !== false) {
40 58
            if (rtrim($line) === '') {
41 58
                break;
42
            }
43 58
            $headers[] = trim($line);
44 58
        }
45
46 59
        $metadatas = stream_get_meta_data($socket);
47
48 59
        if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
49
            throw new NetworkException("Error while reading response, stream timed out", $request);
50
        }
51
52 59
        $parts = explode(' ', array_shift($headers), 3);
53
54 59
        if (count($parts) <= 1) {
55 1
            throw new NetworkException('Cannot read the response', $request);
56
        }
57
58 58
        $protocol = substr($parts[0], -3);
59 58
        $status   = $parts[1];
60
61 58
        if (isset($parts[2])) {
62 58
            $reason = $parts[2];
63 58
        }
64
65
        // Set the size on the stream if it was returned in the response
66 58
        $responseHeaders = [];
67
68 58
        foreach ($headers as $header) {
69 58
            $headerParts = explode(':', $header, 2);
70
71 58
            if (!array_key_exists(trim($headerParts[0]), $responseHeaders)) {
72 58
                $responseHeaders[trim($headerParts[0])] = [];
73 58
            }
74
75 58
            $responseHeaders[trim($headerParts[0])][] = isset($headerParts[1])
76 58
                ? trim($headerParts[1])
77 58
                : '';
78 58
        }
79
80 58
        $response = $this->messageFactory->createResponse($status, $reason, $responseHeaders, null, $protocol);
81 58
        $stream   = $this->createStream($socket, $response);
82
83 58
        return $response->withBody($stream);
84
    }
85
86
    /**
87
     * Create the stream
88
     *
89
     * @param $socket
90
     * @param ResponseInterface $response
91
     *
92
     * @return Stream
93
     */
94 58
    protected function createStream($socket, ResponseInterface $response)
95
    {
96 58
        $size = null;
97
98 58
        if ($response->hasHeader('Content-Length')) {
99
            $size = (int)$response->getHeaderLine('Content-Length');
100
        }
101
102 58
        return new Stream($socket, $size);
103
    }
104
}
105