| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GorkaLaucirica\HipchatAPIv2Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Buzz\Browser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Buzz\Client\Curl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GorkaLaucirica\HipchatAPIv2Client\Auth\AuthInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GorkaLaucirica\HipchatAPIv2Client\Exception\RequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $baseUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var AuthInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var Browser */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $browser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Client constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param AuthInterface $auth Authentication you want to use to access the api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param Browser $browser Client you want to use, by default browser with curl will be used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $baseUrl URL to the HipChat server endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return self | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(AuthInterface $auth, Browser $browser = null, $baseUrl = 'https://api.hipchat.com') | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->auth = $auth; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->baseUrl = $baseUrl; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         if ($browser === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             $client = new Curl(); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             $this->browser = new Browser($client); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         else { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             $this->browser = $browser; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Set the base URL for the requests. Defaults to the public API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      *     but this allows it to work with internal implementations too | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $url URL to the HipChat server endpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @deprecated Use constructor to change default baseUrl instead, will be removed in 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function setBaseUrl($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->baseUrl = $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Common get request for all API calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $resource The path to the resource wanted. For example v2/room | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param array $query Parameters to filter the response for example array('max-results' => 50) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return array Decoded array containing response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function get($resource, $query = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $url = $this->baseUrl . $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (count($query) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $url .= "?"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         foreach ($query as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $url .= "$key=$value&"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $headers = array("Authorization" => $this->auth->getCredential()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $response = $this->browser->get($url, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } catch (\Buzz\Exception\ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             throw new RequestException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($this->browser->getLastResponse()->getStatusCode() > 299) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return json_decode($response->getContent(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Common post request for all API calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $resource The path to the resource wanted. For example v2/room | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param array $content Parameters be posted for example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      *                              array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      *                                'name'                => 'Example name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      *                                'privacy'             => 'private', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      *                                'is_archived'         => 'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      *                                'is_guest_accessible' => 'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      *                                'topic'               => 'New topic', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      *                              ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return array Decoded array containing response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @throws Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 106 |  | View Code Duplication |     public function post($resource, $content) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $url = $this->baseUrl . $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $headers = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'Content-Type' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'Authorization' => $this->auth->getCredential() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $response = $this->browser->post($url, $headers, json_encode($content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } catch (\Buzz\Exception\ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             throw new RequestException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if ($this->browser->getLastResponse()->getStatusCode() > 299) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return json_decode($response->getContent(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Common put request for all API calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $resource The path to the resource wanted. For example v2/room | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param array $content Parameters be putted for example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      *                              array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      *                                'name'                => 'Example name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      *                                'privacy'             => 'private', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      *                                'is_archived'         => 'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      *                                'is_guest_accessible' => 'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      *                                'topic'               => 'New topic', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      *                              ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return array Decoded array containing response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @throws Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 144 |  | View Code Duplication |     public function put($resource, $content = array()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $url = $this->baseUrl . $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $headers = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             'Content-Type' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             'Authorization' => $this->auth->getCredential() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $response = $this->browser->put($url, $headers, json_encode($content)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         } catch (\Buzz\Exception\ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             throw new RequestException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         if ($this->browser->getLastResponse()->getStatusCode() > 299) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return json_decode($response->getContent(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * Common delete request for all API calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param string $resource The path to the resource wanted. For example v2/room | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @return array Decoded array containing response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @throws Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 173 |  | View Code Duplication |     public function delete($resource) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $url = $this->baseUrl . $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $headers = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             'Authorization' => $this->auth->getCredential() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $response = $this->browser->delete($url, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } catch (\Buzz\Exception\ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             throw new RequestException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         if ($this->browser->getLastResponse()->getStatusCode() > 299) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return json_decode($response->getContent(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.