Completed
Pull Request — master (#15)
by Mischa
04:46
created

CurlClient::parse_http_headers()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 6.7273
cc 7
eloc 19
nc 7
nop 1
1
<?php
2
3
/**
4
 * This file is part of the PHP SDK library for the Superdesk Content API.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace Superdesk\ContentApiSdk\Client;
16
17
use Superdesk\ContentApiSdk\ContentApiSdk;
18
use Superdesk\ContentApiSdk\Exception\ClientException;
19
20
/**
21
 * Request service that implements functionality for basic http requests using
22
 * curl.
23
 */
24
class CurlClient implements ClientInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function makeCall(
30
        $url,
31
        array $headers = array(),
32
        array $options = array(),
33
        $method = 'GET',
34
        $content = null
35
    ) {
36
        $curlOptions = array(
37
            CURLOPT_URL => $url,
38
            CURLOPT_HTTPHEADER => $headers,
39
            CURLOPT_FOLLOWLOCATION => true,
40
            CURLOPT_HEADER => true,
41
            CURLOPT_TIMEOUT => 30,
42
            CURLOPT_USERAGENT => ContentApiSdk::USERAGENT,
43
            CURLOPT_RETURNTRANSFER => true,
44
        );
45
46
        switch (strtoupper($method)) {
47
            case 'POST':
48
                $curlOptions[CURLOPT_POST] = true;
49
                $curlOptions[CURLOPT_POSTFIELDS] = $content;
50
                break;
51
            default:
52
            case 'GET':
0 ignored issues
show
Unused Code introduced by
case 'GET': break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
53
                break;
54
        }
55
56
        $curlHandler = curl_init();
57
        if (!$curlHandler) {
58
            throw new ClientException('Could not instantiate cURL.');
59
        }
60
61
        curl_setopt_array($curlHandler, $curlOptions);
62
        $response = curl_exec($curlHandler);
63
64
        if ($response === false) {
65
            throw new ClientException(sprintf('%s (%s)', 'A cURL error occured.', curl_error($curlHandler)), curl_errno($curlHandler));
66
        }
67
68
        $header_size = curl_getinfo($curlHandler, CURLINFO_HEADER_SIZE);
69
70
        $responseArray = array(
71
            'headers' => $this->parse_http_headers(substr($response, 0, $header_size)),
72
            'status' => curl_getinfo($curlHandler, CURLINFO_HTTP_CODE),
73
            'body' => substr($response, $header_size)
74
        );
75
76
        curl_close($curlHandler);
77
78
        return $responseArray;
79
    }
80
81
    /**
82
     * Parse header string from Curl request and returns an array. Based on:
83
     *     http://php.net/manual/en/function.http-parse-headers.php#112986
84
     *     http://php.net/manual/en/function.http-parse-headers.php#112987
85
     *
86
     * @param  string $headerString
87
     *
88
     * @return array
89
     */
90
    private function parse_http_headers($headerString)
91
    {
92
        $headers = array();
93
        $key = '';
94
95
        foreach (explode("\n", $headerString) as $i => $h) {
96
            $h = explode(':', $h, 2);
97
98
            if (isset($h[1])) {
99
                if (!isset($headers[$h[0]])) {
100
                    $headers[$h[0]] = trim($h[1]);
101
                } elseif (is_array($headers[$h[0]])) {
102
                    $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
103
                } else {
104
                    $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
105
                }
106
107
                $key = $h[0];
108
            } else {
109
                if (substr($h[0], 0, 1) == "\t") {
110
                    $headers[$key] .= "\r\n\t".trim($h[0]);
111
                } elseif (!$key) {
112
                    $headers[0] = trim($h[0]);
113
                }
114
            }
115
        }
116
117
        return $headers;
118
    }
119
}
120