Completed
Pull Request — master (#20)
by
unknown
13:16
created

getResponseBodyStatusCodeAndContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 12
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonReference\Loader;
4
5
use League\JsonReference;
6
use League\JsonReference\DecoderManager;
7
use League\JsonReference\DecoderInterface;
8
use League\JsonReference\LoaderInterface;
9
use function League\JsonReference\determineMediaType;
10
use function League\JsonReference\parseHttpResponseHeader;
11
12
final class CurlWebLoader implements LoaderInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $prefix;
18
19
    /**
20
     * @var array
21
     */
22
    private $curlOptions;
23
24
    /**
25
     * @var DecoderManager
26
     */
27
    private $decoderManager;
28
29
    /**
30
     * @param string                          $prefix
31
     * @param array                           $curlOptions
32 66
     * @param DecoderInterface|DecoderManager $decoderManager
33
     */
34 66 View Code Duplication
    public function __construct($prefix, array $curlOptions = null, $decoderManager = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35 66
    {
36 66
        $this->prefix = $prefix;
37 66
        $this->setCurlOptions($curlOptions);
38
        
39
        if ($decoderManager instanceof DecoderInterface) {
40
            $this->decoderManager = new DecoderManager([null => $decoderManager]);
41
        } else {
42 14
            $this->decoderManager = $decoderManager ?: new DecoderManager();
43
        }
44 14
    }
45 14
46 14
    /**
47 14
     * {@inheritdoc}
48 14
     */
49
    public function load($path)
50 14
    {
51 4
        $uri = $this->prefix . $path;
52
        $ch = curl_init($uri);
53
        curl_setopt_array($ch, $this->curlOptions);
54 10
        list($response, $statusCode, $type) = $this->getResponseBodyStatusCodeAndContentType($ch);
55
        curl_close($ch);
56
57
        if ($statusCode >= 400 || !$response) {
58
            throw JsonReference\SchemaLoadingException::create($uri);
59
        }
60
61
        $type = determineMediaType(['Content-Type' => $type, 'uri' => $uri]);
62 14
        return $this->decoderManager->getDecoder($type)->decode($response);
63
    }
64 14
65 14
    /**
66
     * @param resource $ch
67 14
     *
68
     * @return array
69
     */
70
    private function getResponseBodyStatusCodeAndContentType($ch)
71
    {
72
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73 66
        curl_setopt($ch, CURLOPT_HEADER, 1);
74
        $response    = curl_exec($ch);
75
        $statusCode  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76 66
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
77 66
        $body        = substr($response, $header_size);
78 66
        $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
79 33
80
        return [$body, $statusCode, $contentType];
81
    }
82
83
    /**
84
     * @return array
85 66
     */
86
    private function getDefaultCurlOptions()
87 66
    {
88 2
        return [
89 2
            CURLOPT_RETURNTRANSFER => true,
90
            CURLOPT_FOLLOWLOCATION => true,
91
            CURLOPT_MAXREDIRS      => 20,
92 64
        ];
93 64
    }
94
95
    /**
96
     * @param array|null $curlOptions
97
     */
98
    private function setCurlOptions($curlOptions)
99
    {
100
        if (is_array($curlOptions)) {
101
            $this->curlOptions = $curlOptions + $this->getDefaultCurlOptions();
102
            return;
103
        }
104
105
        $this->curlOptions = $this->getDefaultCurlOptions();
106
    }
107
}
108