| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Jleagle\BattleNet\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Jleagle\BattleNet\Enums\AuthScopes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Jleagle\BattleNet\Enums\ServerLocations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Jleagle\BattleNet\Exceptions\BattleNetException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Jleagle\CurlWrapper\Curl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Jleagle\CurlWrapper\Exceptions\CurlException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Jleagle\CurlWrapper\Exceptions\CurlInvalidJsonException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | abstract class AbstractBattleNetAuth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   use BattleNetTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   protected $_apiSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   protected $_redirectUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   protected $_scopes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |    * @param string   $apiKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |    * @param string   $apiSecret | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    * @param string   $redirectUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    * @param string[] $scopes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |    * @param string   $serverLocation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |    * @param string   $responseLocale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     $apiKey, $apiSecret, $redirectUrl, $scopes = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     $serverLocation = ServerLocations::US, $responseLocale = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |   ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     $this->_apiKey = $apiKey; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     $this->_apiSecret = $apiSecret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     $this->_redirectUrl = $redirectUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     $this->_scopes = $scopes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     $this->_serverLocation = $serverLocation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     $this->_responseLocale = $responseLocale; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |   private function _getScopes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     $scopes = $this->_scopes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     if(!$scopes) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       $scopes = [AuthScopes::WOW, AuthScopes::SC2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     return implode(' ', $scopes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    * @param string|null $state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |    * @param bool        $redirect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |    * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |   public function getCode($state = null, $redirect = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     if(!$state) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |       $state = rand(11111, 99999); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     $params = http_build_query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |       [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         'client_id'     => $this->_apiKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         'scope'         => $this->_getScopes(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         'state'         => $state, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         'redirect_uri'  => $this->_redirectUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         'response_type' => 'code', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |       ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     $url = 'https://' . $this->_serverLocation . '.battle.net/oauth/authorize?' . $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     if($redirect) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |       header('Location: ' . $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |       exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     return ['redirectUrl' => $url, 'state' => $state]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |    * Each code can only be used once | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |    * @param string $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |    * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |    * @throws CurlException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |    * @throws CurlInvalidJsonException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |   public function getAccessToken($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     $url = 'https://' . $this->_serverLocation . '.battle.net/oauth/token'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |       'redirect_uri' => $this->_redirectUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |       'scope'        => $this->_getScopes(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |       'grant_type'   => 'authorization_code', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |       'code'         => $code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     return Curl::post($url, $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |       ->setBasicAuth($this->_apiKey, $this->_apiSecret) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |       ->run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |       ->getJson(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |    * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |    * @param string $accessToken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |    * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |    * @throws BattleNetException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |    */ | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |   protected function _get($path, $accessToken) | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |   { | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     $data = ['access_token' => $accessToken]; | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     try | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |       return Curl::get($this->_makeApiUrl($path), $data)->run()->getJson(); | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |     catch(CurlException $e) | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |       $json = $e->getResponse()->getJson(); | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |       $message = isset($json['reason']) ? $json['reason'] : $e->getMessage(); | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |       throw new BattleNetException($message); | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |   } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: