@@ -9,10 +9,10 @@ |
||
| 9 | 9 | * @param boolean $urlEncode [description] |
| 10 | 10 | * @return string [description] |
| 11 | 11 | */ |
| 12 | - function build_url_query(array $params, bool $urlEncode=true):?string { |
|
| 12 | + function build_url_query(array $params, bool $urlEncode = true): ?string { |
|
| 13 | 13 | if ($params == null) return null; |
| 14 | 14 | $queryString = '?'; |
| 15 | - foreach($params as $key => $val) { |
|
| 15 | + foreach ($params as $key => $val) { |
|
| 16 | 16 | if (is_array($val)) { |
| 17 | 17 | foreach ($val as $value) { |
| 18 | 18 | $queryString .= $key."=".($urlEncode ? rawurlencode($value) : $value)."&"; |
@@ -10,7 +10,9 @@ |
||
| 10 | 10 | * @return string [description] |
| 11 | 11 | */ |
| 12 | 12 | function build_url_query(array $params, bool $urlEncode=true):?string { |
| 13 | - if ($params == null) return null; |
|
| 13 | + if ($params == null) { |
|
| 14 | + return null; |
|
| 15 | + } |
|
| 14 | 16 | $queryString = '?'; |
| 15 | 17 | foreach($params as $key => $val) { |
| 16 | 18 | if (is_array($val)) { |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | private $token; |
| 16 | 16 | private $userAgent = 'CodeIgniter GMail API'; |
| 17 | 17 | |
| 18 | - function __construct($params=null) |
|
| 18 | + function __construct($params = null) |
|
| 19 | 19 | { |
| 20 | 20 | get_instance()->load->splint('francis94c/ci-gmail', '%curl'); |
| 21 | 21 | get_instance()->load->splint('francis94c/ci-gmail', '%base64'); |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * [getClientId Get Client ID.] |
| 51 | 51 | * @return null|string Client ID. |
| 52 | 52 | */ |
| 53 | - public function getClientId():?string { |
|
| 53 | + public function getClientId(): ?string { |
|
| 54 | 54 | return $this->clientId; |
| 55 | 55 | } |
| 56 | 56 | /** |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * @param bool $prompt Add the prompt=consent query to the URL. |
| 64 | 64 | * @return string Authorize URL |
| 65 | 65 | */ |
| 66 | - public function getAuthorizeUrl(string $scope, string $redirectUri=null, string $responseType='code', string $accessType='offline', bool $prompt=false):string |
|
| 66 | + public function getAuthorizeUrl(string $scope, string $redirectUri = null, string $responseType = 'code', string $accessType = 'offline', bool $prompt = false):string |
|
| 67 | 67 | { |
| 68 | 68 | $redirectUri = $redirectUri ?? $this->redirectUri; |
| 69 | 69 | if ($scope == null) throw new Exception("GMail scope cannot be null"); |
@@ -75,14 +75,14 @@ discard block |
||
| 75 | 75 | 'access_type' => $accessType |
| 76 | 76 | ]; |
| 77 | 77 | if ($prompt) $params['prompt'] = 'consent'; |
| 78 | - return self::AUTH_URL . build_url_query($params, false); |
|
| 78 | + return self::AUTH_URL.build_url_query($params, false); |
|
| 79 | 79 | } |
| 80 | 80 | /** |
| 81 | 81 | * [getToken description] |
| 82 | 82 | * @param string $code [description] |
| 83 | 83 | * @return [type] [description] |
| 84 | 84 | */ |
| 85 | - public function getToken(string $code, string $redirectUri=null):?array |
|
| 85 | + public function getToken(string $code, string $redirectUri = null): ?array |
|
| 86 | 86 | { |
| 87 | 87 | $redirectUri = $redirectUri ?? $this->redirectUri; |
| 88 | 88 | $ch = curl_init(self::TOKEN_URL); |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | ]); |
| 101 | 101 | $header = [ |
| 102 | 102 | 'Content-Type: application/x-www-form-urlencoded', |
| 103 | - 'Content-Length: ' . strlen($body) |
|
| 103 | + 'Content-Length: '.strlen($body) |
|
| 104 | 104 | ]; |
| 105 | 105 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
| 106 | 106 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | * @param string $refreshToken [description] |
| 119 | 119 | * @return [type] [description] |
| 120 | 120 | */ |
| 121 | - public function refreshAccessToken(string $refreshToken):?array { |
|
| 121 | + public function refreshAccessToken(string $refreshToken): ?array { |
|
| 122 | 122 | $ch = curl_init(self::TOKEN_URL); |
| 123 | 123 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 124 | 124 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | ]); |
| 134 | 134 | $header = [ |
| 135 | 135 | 'Content-Type: application/x-www-form-urlencoded', |
| 136 | - 'Content-Length: ' . strlen($body) |
|
| 136 | + 'Content-Length: '.strlen($body) |
|
| 137 | 137 | ]; |
| 138 | 138 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
| 139 | 139 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
@@ -153,9 +153,9 @@ discard block |
||
| 153 | 153 | * @param string $user [description] |
| 154 | 154 | * @return [type] [description] |
| 155 | 155 | */ |
| 156 | - public function getProfile(string $user='me'):?array { |
|
| 156 | + public function getProfile(string $user = 'me'): ?array { |
|
| 157 | 157 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 158 | - self::API . "$user/profile", |
|
| 158 | + self::API."$user/profile", |
|
| 159 | 159 | ["Authorization: Bearer $this->token"] |
| 160 | 160 | ); |
| 161 | 161 | if ($response !== false) return $this->process_response($code, $response); |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | * @param string $labelFilterAction [description] |
| 175 | 175 | * @return [type] [description] |
| 176 | 176 | */ |
| 177 | - public function watch(string $topic, $labelIds=null, string $userId='me', string $labelFilterAction='include'):?array { |
|
| 177 | + public function watch(string $topic, $labelIds = null, string $userId = 'me', string $labelFilterAction = 'include'): ?array { |
|
| 178 | 178 | $body = [ |
| 179 | 179 | 'topicName' => $topic, |
| 180 | 180 | 'labelFilterAction' => $labelFilterAction |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | list($code, $response) = (new GMailCURL(GMailCURL::POST))( |
| 192 | - self::API . "$userId/watch", |
|
| 192 | + self::API."$userId/watch", |
|
| 193 | 193 | ["Authorization: Bearer $this->token"], |
| 194 | 194 | $body |
| 195 | 195 | ); |
@@ -203,10 +203,10 @@ discard block |
||
| 203 | 203 | * @param string $userId ID or Email Address of the user. |
| 204 | 204 | * @return bool [description] |
| 205 | 205 | */ |
| 206 | - public function endWatch(string $userId='me'):bool |
|
| 206 | + public function endWatch(string $userId = 'me'):bool |
|
| 207 | 207 | { |
| 208 | 208 | list($code, $response) = (new GMailCURL(GMailCURL::POST))( |
| 209 | - self::API . "$userId/stop", |
|
| 209 | + self::API."$userId/stop", |
|
| 210 | 210 | ["Authorization: Bearer $this->token"] |
| 211 | 211 | ); |
| 212 | 212 | if ($response !== false) return $code == 204; |
@@ -219,10 +219,10 @@ discard block |
||
| 219 | 219 | * @param string $userID [description] |
| 220 | 220 | * @return null|array [description] |
| 221 | 221 | */ |
| 222 | - public function getLabels(string $userId='me'):?array |
|
| 222 | + public function getLabels(string $userId = 'me'): ?array |
|
| 223 | 223 | { |
| 224 | 224 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 225 | - self::API . "$userId/labels", |
|
| 225 | + self::API."$userId/labels", |
|
| 226 | 226 | ["Authorization: Bearer $this->token"] |
| 227 | 227 | ); |
| 228 | 228 | if ($response !== false) { |
@@ -242,9 +242,9 @@ discard block |
||
| 242 | 242 | * @param [type] $truncateAfter [description] |
| 243 | 243 | * @return [type] [description] |
| 244 | 244 | */ |
| 245 | - public function getMessages(string $userId='me', array $labelIds=null, |
|
| 246 | - string $q=null, int $maxMessages=null, string $pageToken=null, bool $includeSpamTrash=false, |
|
| 247 | - $truncateAfter=null):?object |
|
| 245 | + public function getMessages(string $userId = 'me', array $labelIds = null, |
|
| 246 | + string $q = null, int $maxMessages = null, string $pageToken = null, bool $includeSpamTrash = false, |
|
| 247 | + $truncateAfter = null): ?object |
|
| 248 | 248 | { |
| 249 | 249 | $query = []; |
| 250 | 250 | |
@@ -255,14 +255,14 @@ discard block |
||
| 255 | 255 | if ($maxMessages != null) $query['maxResults'] = $maxMessages; |
| 256 | 256 | |
| 257 | 257 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 258 | - self::API . "$userId/messages" . build_url_query($query), |
|
| 258 | + self::API."$userId/messages".build_url_query($query), |
|
| 259 | 259 | ["Authorization: Bearer $this->token"] |
| 260 | 260 | ); |
| 261 | 261 | |
| 262 | 262 | if ($response !== false) { |
| 263 | 263 | if ($truncateAfter != null && $code == 200) { |
| 264 | 264 | $response = json_decode($response); |
| 265 | - $response->messages = array_filter($response->messages, function ($e) use ($truncateAfter) { |
|
| 265 | + $response->messages = array_filter($response->messages, function($e) use ($truncateAfter) { |
|
| 266 | 266 | return strcmp($truncateAfter, $e->id) < 0; |
| 267 | 267 | }); |
| 268 | 268 | $response->{self::HTTP_CODE} = $code; |
@@ -283,8 +283,8 @@ discard block |
||
| 283 | 283 | * @param [type] $metadataHeaders [description] |
| 284 | 284 | * @return Message|null [description] |
| 285 | 285 | */ |
| 286 | - public function getMessage(string $userId='me', string $messageId, |
|
| 287 | - string $format='full', array $metadataHeaders=null):?Message |
|
| 286 | + public function getMessage(string $userId = 'me', string $messageId, |
|
| 287 | + string $format = 'full', array $metadataHeaders = null): ?Message |
|
| 288 | 288 | { |
| 289 | 289 | $query = []; |
| 290 | 290 | |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | if ($metadataHeaders != null) $query['metadataHeaders'] = $metadataHeaders; |
| 293 | 293 | |
| 294 | 294 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 295 | - self::API . "$userId/messages/$messageId?" . http_build_query($query), |
|
| 295 | + self::API."$userId/messages/$messageId?".http_build_query($query), |
|
| 296 | 296 | ["Authorization: Bearer $this->token"] |
| 297 | 297 | ); |
| 298 | 298 | |
@@ -20,7 +20,9 @@ discard block |
||
| 20 | 20 | get_instance()->load->splint('francis94c/ci-gmail', '%curl'); |
| 21 | 21 | get_instance()->load->splint('francis94c/ci-gmail', '%base64'); |
| 22 | 22 | |
| 23 | - if ($params != null) $this->init($params); |
|
| 23 | + if ($params != null) { |
|
| 24 | + $this->init($params); |
|
| 25 | + } |
|
| 24 | 26 | |
| 25 | 27 | spl_autoload_register(function($name) { |
| 26 | 28 | if (file_exists(APPPATH.'splints/'.self::PACKAGE."/libraries/$name.php")) { |
@@ -66,7 +68,9 @@ discard block |
||
| 66 | 68 | public function getAuthorizeUrl(string $scope, string $redirectUri=null, string $responseType='code', string $accessType='offline', bool $prompt=false):string |
| 67 | 69 | { |
| 68 | 70 | $redirectUri = $redirectUri ?? $this->redirectUri; |
| 69 | - if ($scope == null) throw new Exception("GMail scope cannot be null"); |
|
| 71 | + if ($scope == null) { |
|
| 72 | + throw new Exception("GMail scope cannot be null"); |
|
| 73 | + } |
|
| 70 | 74 | $params = [ |
| 71 | 75 | 'client_id' => $this->clientId, |
| 72 | 76 | 'redirect_uri' => $redirectUri, |
@@ -74,7 +78,9 @@ discard block |
||
| 74 | 78 | 'response_type' => $responseType, |
| 75 | 79 | 'access_type' => $accessType |
| 76 | 80 | ]; |
| 77 | - if ($prompt) $params['prompt'] = 'consent'; |
|
| 81 | + if ($prompt) { |
|
| 82 | + $params['prompt'] = 'consent'; |
|
| 83 | + } |
|
| 78 | 84 | return self::AUTH_URL . build_url_query($params, false); |
| 79 | 85 | } |
| 80 | 86 | /** |
@@ -110,7 +116,9 @@ discard block |
||
| 110 | 116 | $response = curl_exec($ch); |
| 111 | 117 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 112 | 118 | curl_close($ch); |
| 113 | - if ($response !== false) return $this->process_response($code, $response); |
|
| 119 | + if ($response !== false) { |
|
| 120 | + return $this->process_response($code, $response); |
|
| 121 | + } |
|
| 114 | 122 | return null; |
| 115 | 123 | } |
| 116 | 124 | /** |
@@ -144,7 +152,9 @@ discard block |
||
| 144 | 152 | $response = curl_exec($ch); |
| 145 | 153 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 146 | 154 | curl_close($ch); |
| 147 | - if ($response !== false) return $this->process_response($code, $response); |
|
| 155 | + if ($response !== false) { |
|
| 156 | + return $this->process_response($code, $response); |
|
| 157 | + } |
|
| 148 | 158 | return null; |
| 149 | 159 | } |
| 150 | 160 | /** |
@@ -158,7 +168,9 @@ discard block |
||
| 158 | 168 | self::API . "$user/profile", |
| 159 | 169 | ["Authorization: Bearer $this->token"] |
| 160 | 170 | ); |
| 161 | - if ($response !== false) return $this->process_response($code, $response); |
|
| 171 | + if ($response !== false) { |
|
| 172 | + return $this->process_response($code, $response); |
|
| 173 | + } |
|
| 162 | 174 | return null; |
| 163 | 175 | } |
| 164 | 176 | |
@@ -193,7 +205,9 @@ discard block |
||
| 193 | 205 | ["Authorization: Bearer $this->token"], |
| 194 | 206 | $body |
| 195 | 207 | ); |
| 196 | - if ($response !== false) return $this->process_response($code, $response); |
|
| 208 | + if ($response !== false) { |
|
| 209 | + return $this->process_response($code, $response); |
|
| 210 | + } |
|
| 197 | 211 | return null; |
| 198 | 212 | } |
| 199 | 213 | |
@@ -209,7 +223,9 @@ discard block |
||
| 209 | 223 | self::API . "$userId/stop", |
| 210 | 224 | ["Authorization: Bearer $this->token"] |
| 211 | 225 | ); |
| 212 | - if ($response !== false) return $code == 204; |
|
| 226 | + if ($response !== false) { |
|
| 227 | + return $code == 204; |
|
| 228 | + } |
|
| 213 | 229 | return false; |
| 214 | 230 | } |
| 215 | 231 | |
@@ -248,11 +264,21 @@ discard block |
||
| 248 | 264 | { |
| 249 | 265 | $query = []; |
| 250 | 266 | |
| 251 | - if ($labelIds != null) $query['labelIds'] = $labelIds; |
|
| 252 | - if ($includeSpamTrash) $query['includeSpamTrash'] = $includeSpamTrash; |
|
| 253 | - if ($q != null) $query['q'] = $q; |
|
| 254 | - if ($pageToken != null) $query['pageToken'] = $pageToken; |
|
| 255 | - if ($maxMessages != null) $query['maxResults'] = $maxMessages; |
|
| 267 | + if ($labelIds != null) { |
|
| 268 | + $query['labelIds'] = $labelIds; |
|
| 269 | + } |
|
| 270 | + if ($includeSpamTrash) { |
|
| 271 | + $query['includeSpamTrash'] = $includeSpamTrash; |
|
| 272 | + } |
|
| 273 | + if ($q != null) { |
|
| 274 | + $query['q'] = $q; |
|
| 275 | + } |
|
| 276 | + if ($pageToken != null) { |
|
| 277 | + $query['pageToken'] = $pageToken; |
|
| 278 | + } |
|
| 279 | + if ($maxMessages != null) { |
|
| 280 | + $query['maxResults'] = $maxMessages; |
|
| 281 | + } |
|
| 256 | 282 | |
| 257 | 283 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 258 | 284 | self::API . "$userId/messages" . build_url_query($query), |
@@ -288,15 +314,21 @@ discard block |
||
| 288 | 314 | { |
| 289 | 315 | $query = []; |
| 290 | 316 | |
| 291 | - if ($format != 'full' && $format != null) $query['format'] = $format; |
|
| 292 | - if ($metadataHeaders != null) $query['metadataHeaders'] = $metadataHeaders; |
|
| 317 | + if ($format != 'full' && $format != null) { |
|
| 318 | + $query['format'] = $format; |
|
| 319 | + } |
|
| 320 | + if ($metadataHeaders != null) { |
|
| 321 | + $query['metadataHeaders'] = $metadataHeaders; |
|
| 322 | + } |
|
| 293 | 323 | |
| 294 | 324 | list($code, $response) = (new GMailCURL(GMailCURL::GET))( |
| 295 | 325 | self::API . "$userId/messages/$messageId?" . http_build_query($query), |
| 296 | 326 | ["Authorization: Bearer $this->token"] |
| 297 | 327 | ); |
| 298 | 328 | |
| 299 | - if ($response !== false) return new Message($response); |
|
| 329 | + if ($response !== false) { |
|
| 330 | + return new Message($response); |
|
| 331 | + } |
|
| 300 | 332 | |
| 301 | 333 | return null; |
| 302 | 334 | } |