ResponseFromCache::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 20
ccs 7
cts 9
cp 0.7778
rs 9.9666
c 0
b 0
f 0
cc 4
nc 6
nop 4
crap 4.1755
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class ResponseFromCache extends Response
6
{
7
    /** @var string */
8
    protected $url;
9
10
    /**
11
     * @param string $filePath
12
     * @param mixed  $headers  could be a string (the separator between headers and content or FALSE
13
     */
14 3
    public function __construct(
15
        string $filePathOrContent,
16
        ?string $url = null,
17
        array $info = [],
18
        $headers = PHP_EOL.PHP_EOL
19
    ) {
20 3
        $content = file_exists($filePathOrContent) ? file_get_contents($filePathOrContent) : $filePathOrContent;
21
22 3
        if (!$content) {
23
            throw new \Exception($filePath.' doesn\'t exist');
0 ignored issues
show
Comprehensibility Best Practice introduced by Robin
The variable $filePath does not exist. Did you maybe mean $filePathOrContent?
Loading history...
24
        }
25
26 3
        if (false !== $headers) {
27 3
            list($this->headers, $this->content) = explode($headers, $content, 2);
28
        } else {
29
            $this->content = $content;
30
        }
31
32 3
        $this->info = $info;
33 3
        $this->url = $url;
34 3
    }
35
36 3
    public function getRequest()
37
    {
38 3
        return null;
39
    }
40
41
    public function getUrl()
42
    {
43
        return $this->url;
44
    }
45
46
    public function getEffectiveUrl()
47
    {
48
        return $this->url;
49
    }
50
51
    public function getStatusCode()
52
    {
53
        if (isset($this->headers)) {
54
            $headers = $this->getHeaders();
55
            list($http, $status) = explode(' ', $headers[0], 2);
56
57
            return $status;
58
        }
59
60
        return $this->getInfo('http_code');
61
    }
62
63 3
    public function getContentType()
64
    {
65 3
        if (isset($this->headers)) {
66 3
            $headers = $this->getHeaders();
67 3
            if (isset($headers['content-type'])) {
68
                return $headers['content-type'];
69
            }
70
        }
71
72 3
        return $this->getInfo('content_type');
73
    }
74
}
75