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