CurlResponse   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 73
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A headers() 0 3 1
A json() 0 7 2
A raw() 0 3 1
A statusCode() 0 3 1
A __construct() 0 6 1
A isError() 0 3 1
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
use PhpGuard\Curl\Collection\Headers;
23
24
class CurlResponse
25
{
26
    const JSON_PATTERN = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
27
    const XML_PATTERN = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
28
29
    /**
30
     * @var string
31
     */
32
    private $rawResponse;
33
34
    /**
35
     * @var Headers
36
     */
37
    private $headers;
38
39
    /**
40
     * @var int
41
     */
42
    private $statusCode;
43
    /**
44
     * @var array
45
     */
46
    private $info;
47
48
    /**
49
     * CurlResponse constructor.
50
     *
51
     * @param int    $statusCode
52
     * @param string $rawResponse
53
     * @param array  $headers
54
     * @param array  $info
55
     */
56 45
    public function __construct(int $statusCode, string $rawResponse, array $headers, array $info)
57
    {
58 45
        $this->statusCode = $statusCode;
59 45
        $this->rawResponse = $rawResponse;
60 45
        $this->headers = new Headers($headers);
61 45
        $this->info = $info;
62 45
    }
63
64
    /**
65
     * @return int
66
     */
67 45
    public function statusCode(): int
68
    {
69 45
        return $this->statusCode;
70
    }
71
72 3
    public function isError(): bool
73
    {
74 3
        return $this->statusCode >= 300;
75
    }
76
77
    /**
78
     * @return Headers
79
     */
80
    public function headers(): Headers
81
    {
82
        return $this->headers;
83
    }
84
85 6
    public function raw()
86
    {
87 6
        return $this->rawResponse;
88
    }
89
90 39
    public function json()
91
    {
92 39
        if (!preg_match(self::JSON_PATTERN, $this->headers['Content-Type'])) {
93 3
            return false;
94
        }
95
96 36
        return json_decode($this->rawResponse, true);
97
    }
98
}
99