Completed
Push — master ( e3c4d3...a92d5c )
by Alexandre
02:53
created

CurlResponseFactory::create()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 1
nop 2
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) 2018 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
23
class CurlResponseFactory
24
{
25 12
    public function create($result, array $info)
26
    {
27 12
        $statusCode = $info['http_code'];
28 12
        $headerSize = $info['header_size'];
29 12
        $rawHeaders = substr($result, 0, $headerSize);
30 12
        $raw = substr($result, $headerSize);
31
32
        $headers = array_reduce(explode("\n", $rawHeaders), function ($headers, $header) {
33 12
            $parts = explode(':', $header);
34 12
            if (count($parts) == 2) {
35 12
                $headers[trim($parts[0])] = trim($parts[1]);
36
            }
37 12
            return $headers;
38 12
        }, []);
39
40 12
        return new CurlResponse($statusCode, $raw, $headers, $info);
41
    }
42
}