| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace UserAgentParser\Provider\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use UserAgentParser\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use UserAgentParser\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DeviceAtlasCom extends AbstractHttpProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Name of the provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $name = 'DeviceAtlasCom'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Homepage of the provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $homepage = 'https://deviceatlas.com/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $detectionCapabilities = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'browser' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'name'    => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'version' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'renderingEngine' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'name'    => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'version' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'operatingSystem' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'name'    => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'version' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'device' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             'model'    => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'brand'    => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'type'     => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'isMobile' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'isTouch'  => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         'bot' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'isBot' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'name'  => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'type'  => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private static $uri = 'http://region0.deviceatlascloud.com/v1/detect/properties'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     private $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 15 |  |     public function __construct(Client $client, $apiKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 15 |  |         parent::__construct($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 15 |  |         $this->apiKey = $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 10 |  |     protected function getResult($userAgent, array $headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          * an empty UserAgent makes no sense | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 10 |  |         if ($userAgent == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 9 |  |         $parameters = '?licencekey=' . urlencode($this->apiKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 9 |  |         $parameters .= '&useragent=' . urlencode($userAgent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 9 |  |         $uri = self::$uri . $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // key to lower | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |         $headers = array_change_key_case($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |         $newHeaders = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |         foreach ($headers as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             $newHeaders['X-DA-' . $key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 9 |  |         $newHeaders['User-Agent'] = 'ThaDafinser/UserAgentParser:v1.4'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 9 |  |         $request = new Request('GET', $uri, $newHeaders); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 9 |  |             $response = $this->getResponse($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 9 |  |         } catch (Exception\RequestException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             /* @var $prevEx \GuzzleHttp\Exception\ClientException */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             $prevEx = $ex->getPrevious(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 | 2 | View Code Duplication |             if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                 throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             throw $ex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |          * no json returned? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 7 |  |         $contentType = $response->getHeader('Content-Type'); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 112 | 7 | View Code Duplication |         if (! isset($contentType[0]) || $contentType[0] != 'application/json; charset=UTF-8') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |             throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 6 |  |         $content = json_decode($response->getBody()->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 118 | 6 | View Code Duplication |         if (! $content instanceof stdClass || ! isset($content->properties)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |          * No result found? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 5 |  |         if (! $content->properties instanceof stdClass || count((array) $content->properties) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 4 |  |         return $content->properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param Model\Browser $browser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param stdClass      $resultRaw | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 4 |  |     private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 4 |  |         if (isset($resultRaw->browserName) && $this->isRealResult($resultRaw->browserName) === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 1 |  |             $browser->setName($resultRaw->browserName); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 4 |  |         if (isset($resultRaw->browserVersion) && $this->isRealResult($resultRaw->browserVersion) === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 1 |  |             $browser->getVersion()->setComplete($resultRaw->browserVersion); | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param Model\RenderingEngine $engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param stdClass              $resultRaw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 4 |  |     private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 4 |  |         if (isset($resultRaw->browserRenderingEngine) && $this->isRealResult($resultRaw->browserRenderingEngine) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |             $engine->setName($resultRaw->browserRenderingEngine); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param Model\OperatingSystem $os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param stdClass              $resultRaw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 4 |  |     private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 4 |  |         if (isset($resultRaw->osName) && $this->isRealResult($resultRaw->osName) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |             $os->setName($resultRaw->osName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 4 |  |         if (isset($resultRaw->osVersion) && $this->isRealResult($resultRaw->osVersion) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |             $os->getVersion()->setComplete($resultRaw->osVersion); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param Model\UserAgent $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param stdClass        $resultRaw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 4 |  |     private function hydrateDevice(Model\Device $device, stdClass $resultRaw) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 183 | 4 | View Code Duplication |         if (isset($resultRaw->primaryHardwareType) && $this->isRealResult($resultRaw->primaryHardwareType) === true) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 1 |  |             $device->setType($resultRaw->primaryHardwareType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 10 |  |     public function parse($userAgent, array $headers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 10 |  |         $resultRaw = $this->getResult($userAgent, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |          * Hydrate the model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 4 |  |         $result = new Model\UserAgent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 4 |  |         $result->setProviderResultRaw($resultRaw); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |          * hydrate the result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 4 |  |         $this->hydrateBrowser($result->getBrowser(), $resultRaw); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 4 |  |         $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 4 |  |         $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 4 |  |         $this->hydrateDevice($result->getDevice(), $resultRaw); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  | 
            
                        
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.