| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | final class CurlWebLoader implements LoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $curlOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var DecoderManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $decoderManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string                          $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param array                           $curlOptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param DecoderInterface|DecoderManager $decoderManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 66 |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 |  | View Code Duplication |     public function __construct($prefix, array $curlOptions = null, $decoderManager = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 66 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 66 |  |         $this->prefix = $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 66 |  |         $this->setCurlOptions($curlOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 66 |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if ($decoderManager instanceof DecoderInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->decoderManager = new DecoderManager([null => $decoderManager]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->decoderManager = $decoderManager ?: new DecoderManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 14 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 14 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 14 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 14 |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 14 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 14 |  |     public function load($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 14 |  |         $uri = $this->prefix . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |         $ch = curl_init($uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         curl_setopt_array($ch, $this->curlOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         list($response, $statusCode, $headers) = $this->getResponseBodyAndStatusCode($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 10 |  |         curl_close($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($statusCode >= 400 || !$response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw JsonReference\SchemaLoadingException::create($uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $type = determineMediaType(['headers' => $headers, 'uri' => $uri]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->decoderManager->getDecoder($type)->decode($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 14 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 14 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 14 |  |      * @param resource $ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 14 |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function getResponseBodyAndStatusCode($ch) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $response    = curl_exec($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $statusCode  = curl_getinfo($ch, CURLINFO_HTTP_CODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 66 |  |         $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $headers     = substr($response, 0, $header_size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $headers     = $this->parseResponseHeader($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 66 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 66 |  |         return [$response, $statusCode, $headers]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 66 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 33 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     private function getDefaultCurlOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 66 |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             CURLOPT_RETURNTRANSFER => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 66 |  |             CURLOPT_FOLLOWLOCATION => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |             CURLOPT_MAXREDIRS      => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 64 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 64 |  |      * @param array|null $curlOptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function setCurlOptions($curlOptions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (is_array($curlOptions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->curlOptions = $curlOptions + $this->getDefaultCurlOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->curlOptions = $this->getDefaultCurlOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Parse http headers returned by $http_response_header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @link https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param array $headers | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |     public static function parseResponseHeader($header_text) | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |         $headers = []; | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         foreach (explode("\r\n", $header_text) as $i => $line) { | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |             if ($i === 0) { | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |                 $headers['http_code'] = $line; | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |             } else { | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |                 list ($key, $value) = explode(': ', $line); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |                 $headers[$key] = $value; | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |         return $headers; | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  | 
            
                        
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.