Response   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B forge() 0 39 6
1
<?php
2
/**
3
 * This file is part of the jyggen/curl library
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright Copyright (c) Jonas Stendahl <[email protected]>
9
 * @license http://opensource.org/licenses/MIT MIT
10
 * @link https://jyggen.com/projects/jyggen-curl Documentation
11
 * @link https://packagist.org/packages/jyggen/curl Packagist
12
 * @link https://github.com/jyggen/curl GitHub
13
 */
14
15
16
namespace Jyggen\Curl;
17
18
use Jyggen\Curl\RequestInterface;
19
20
/**
21
 * Response
22
 *
23
 * Represents an HTTP response.
24
 */
25
class Response extends \Symfony\Component\HttpFoundation\Response
26
{
27
    /**
28
     * Forge a new object based on a request.
29
     * @param  RequestInterface $request
30
     * @return Response
31
     */
32
    public static function forge(RequestInterface $request)
33
    {
34
        $headerSize = $request->getInfo(CURLINFO_HEADER_SIZE);
35
        $response   = $request->getRawResponse();
36
        $content    = (strlen($response) === $headerSize) ? '' : substr($response, $headerSize);
37
38
        $rawHeaders = rtrim(substr($response, 0, $headerSize));
39
        $headers    = [];
40
41
        foreach (preg_split('/(\\r?\\n)/', $rawHeaders) as $header) {
42
            if ($header) {
43
                $headers[] = $header;
44
            } else {
45
                $headers = [];
46
            }
47
        }
48
49
        $headerBag = [];
50
        $info      = $request->getInfo();
51
        $status    = explode(' ', $headers[0]);
52
        $status    = explode('/', $status[0]);
53
54
        unset($headers[0]);
55
56
        foreach ($headers as $header) {
57
            $headerPair = explode(': ', $header);
58
59
            if (count($headerPair) > 1) {
60
                list($key, $value)     = $headerPair;
61
                $headerBag[trim($key)] = trim($value);
62
            }
63
        }
64
65
        $response = new static($content, $info['http_code'], $headerBag);
66
        $response->setProtocolVersion($status[1]);
67
        $response->setCharset(substr(strstr($response->headers->get('Content-Type'), '='), 1));
68
69
        return $response;
70
    }
71
}
72