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
|
|
|
|
10
|
|
|
final class CurlWebLoader implements LoaderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $prefix; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $curlOptions; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DecoderManager |
24
|
|
|
*/ |
25
|
|
|
private $decoders; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $prefix |
29
|
|
|
* @param array $curlOptions |
30
|
|
|
* @param JsonDecoderInterface|DecoderManager $decoders |
31
|
|
|
*/ |
32
|
72 |
View Code Duplication |
public function __construct($prefix, array $curlOptions = null, $decoders = null) |
|
|
|
|
33
|
|
|
{ |
34
|
72 |
|
$this->prefix = $prefix; |
35
|
72 |
|
$this->setCurlOptions($curlOptions); |
36
|
|
|
|
37
|
72 |
|
if ($decoders instanceof DecoderInterface) { |
38
|
|
|
$this->decoders = new DecoderManager([$decoders]); |
39
|
|
|
} else { |
40
|
72 |
|
$this->decoders = $decoders ?: new DecoderManager(); |
|
|
|
|
41
|
|
|
} |
42
|
72 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
16 |
|
public function load($path, $defaultExtension = 'json') |
48
|
|
|
{ |
49
|
16 |
|
$uri = $this->prefix . $path; |
50
|
|
|
|
51
|
16 |
|
$extension = isset(pathinfo($path)['extension']) ? pathinfo($path)['extension'] : $defaultExtension; |
52
|
|
|
|
53
|
16 |
|
$ch = curl_init($uri); |
54
|
16 |
|
curl_setopt_array($ch, $this->curlOptions); |
55
|
16 |
|
list($response, $statusCode) = $this->getResponseBodyAndStatusCode($ch); |
56
|
16 |
|
curl_close($ch); |
57
|
|
|
|
58
|
16 |
|
if ($statusCode >= 400 || !$response) { |
59
|
4 |
|
throw JsonReference\SchemaLoadingException::create($uri); |
60
|
|
|
} |
61
|
|
|
|
62
|
12 |
|
return $this->decoders->getDecoder($extension)->decode($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param resource $ch |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
16 |
|
private function getResponseBodyAndStatusCode($ch) |
71
|
|
|
{ |
72
|
16 |
|
$response = curl_exec($ch); |
73
|
16 |
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
74
|
|
|
|
75
|
16 |
|
return [$response, $statusCode]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
72 |
|
private function getDefaultCurlOptions() |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
72 |
|
CURLOPT_RETURNTRANSFER => true, |
85
|
72 |
|
CURLOPT_FOLLOWLOCATION => true, |
86
|
72 |
|
CURLOPT_MAXREDIRS => 20, |
87
|
36 |
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array|null $curlOptions |
92
|
|
|
*/ |
93
|
72 |
|
private function setCurlOptions($curlOptions) |
94
|
|
|
{ |
95
|
72 |
|
if (is_array($curlOptions)) { |
96
|
2 |
|
$this->curlOptions = $curlOptions + $this->getDefaultCurlOptions(); |
97
|
2 |
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
70 |
|
$this->curlOptions = $this->getDefaultCurlOptions(); |
101
|
70 |
|
} |
102
|
|
|
} |
103
|
|
|
|
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.