| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace NotificationChannels\HipChat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Client as HttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Psr7\MultipartStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function GuzzleHttp\Psr7\modify_request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function GuzzleHttp\Psr7\stream_for; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class HipChat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var HttpClient */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $room; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param HttpClient $http | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param string|null $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(HttpClient $http, $url, $token, $room) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->http = $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->url = rtrim($url ?: 'https://api.hipchat.com', '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->token = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->room = $room; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Returns default room id or name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function room() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return $this->room; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Returns HipChat base url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function url() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $this->url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Send a message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string|int $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function sendMessage($to, $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $url = $this->url.'/v2/room/'.urlencode($to).'/notification'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this->post($url, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'headers' => $this->getHeaders(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'json' => $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Share a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string|int $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public function shareFile($to, $file) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $parts[] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             'headers' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 'Content-Type' => $file['file_type'] ?: 'application/octet-stream', | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             'name' => 'file', | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             'contents' => stream_for($file['content']), | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             'filename' => $file['filename'] ?: 'untitled', | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         if (!empty($file['message'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             $parts[] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 'headers' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                     'Content-Type' => 'application/json', | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                 'name' => 'metadata', | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 'contents' => json_encode(['message' => $file['message']]), | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $url = $this->url.'/v2/room/'.urlencode($to).'/share/file'; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         return $this->postMultipartRelated($url, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             'headers' => $this->getHeaders(), | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             'multipart' => $parts, | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Make a simple post request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function post($url, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this->http->post($url, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Make a multipart/related request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * Unfortunately Guzzle doesn't support multipart/related requests out of the box. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @return \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     protected function postMultipartRelated($url, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $headers = isset($options['headers']) ? $options['headers'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $body = new MultipartStream($options['multipart']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $version = isset($options['version']) ? $options['version'] : '1.1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $request = new Request('POST', $url, $headers, $body, $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $changeContentType['set_headers']['Content-Type'] = 'multipart/related; boundary=' . $request->getBody()->getBoundary(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $request = modify_request($request, $changeContentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $this->http->send($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * Get common request headers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function getHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             'Authorization' => 'Bearer '.$this->token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.