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

CurlResponseFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 2
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
}