Passed
Push — master ( 616f10...3db985 )
by Alexandre
02:45
created

CurlResponseFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 2
dl 0
loc 26
ccs 13
cts 16
cp 0.8125
crap 3.0593
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace PhpGuard\Curl;
21
22
class CurlResponseFactory
23
{
24
    /**
25
     * @param $ch
26
     * @param $content
27
     *
28
     * @return CurlResponse
29
     *
30
     * @throws CurlError
31
     */
32 13
    public function create($ch, $content)
33
    {
34 13
        if (false === $content) {
35
            $message = curl_error($ch);
36
            $code = curl_errno($ch);
37
38
            throw new CurlError($message, $code);
39
        }
40
41 13
        $info = curl_getinfo($ch);
42
43 13
        $statusCode = $info['http_code'];
44 13
        $headerSize = $info['header_size'];
45 13
        $rawHeaders = substr($content, 0, $headerSize);
46 13
        $raw = substr($content, $headerSize);
47
48
        $headers = array_reduce(explode("\n", $rawHeaders), function ($headers, $header) {
49 13
            $parts = explode(':', $header);
50 13
            if (2 == count($parts)) {
51 13
                $headers[trim($parts[0])] = trim($parts[1]);
52
            }
53
54 13
            return $headers;
55 13
        }, []);
56
57 13
        return new CurlResponse($statusCode, $raw, $headers, $info);
58
    }
59
}
60