Completed
Push — master ( e30a3d...6fcdef )
by Francis
02:09
created
libraries/GMail.php 2 patches
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,8 +63,8 @@  discard block
 block discarded – undo
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,
67
-  string $responseType='code', string $accessType='offline', bool $prompt=false):string
66
+  public function getAuthorizeUrl(string $scope, string $redirectUri = null,
67
+  string $responseType = 'code', string $accessType = 'offline', bool $prompt = false):string
68 68
   {
69 69
     $redirectUri = $redirectUri ?? $this->redirectUri;
70 70
     if ($scope == null) throw new Exception("GMail scope cannot be null");
@@ -76,14 +76,14 @@  discard block
 block discarded – undo
76 76
       'access_type'   => $accessType
77 77
     ];
78 78
     if ($prompt) $params['prompt'] = 'consent';
79
-    return self::AUTH_URL . build_url_query($params, false);
79
+    return self::AUTH_URL.build_url_query($params, false);
80 80
   }
81 81
   /**
82 82
    * [getToken description]
83 83
    * @param  string $code [description]
84 84
    * @return [type]       [description]
85 85
    */
86
-  public function getToken(string $code, string $redirectUri=null):?object
86
+  public function getToken(string $code, string $redirectUri = null): ?object
87 87
   {
88 88
     $redirectUri = $redirectUri ?? $this->redirectUri;
89 89
     $ch = curl_init(self::TOKEN_URL);
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     ]);
102 102
     $header = [
103 103
       'Content-Type: application/x-www-form-urlencoded',
104
-      'Content-Length: ' . strlen($body)
104
+      'Content-Length: '.strlen($body)
105 105
     ];
106 106
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
107 107
     curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
    * @param  string $refreshToken [description]
120 120
    * @return [type]               [description]
121 121
    */
122
-  public function refreshAccessToken(string $refreshToken):?object
122
+  public function refreshAccessToken(string $refreshToken): ?object
123 123
   {
124 124
     $ch = curl_init(self::TOKEN_URL);
125 125
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
     ]);
136 136
     $header = [
137 137
       'Content-Type: application/x-www-form-urlencoded',
138
-      'Content-Length: ' . strlen($body)
138
+      'Content-Length: '.strlen($body)
139 139
     ];
140 140
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
141 141
     curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
@@ -155,10 +155,10 @@  discard block
 block discarded – undo
155 155
    * @param  string $user [description]
156 156
    * @return [type]       [description]
157 157
    */
158
-  public function getProfile(string $user='me'):?object
158
+  public function getProfile(string $user = 'me'): ?object
159 159
   {
160 160
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
161
-      self::API . "$user/profile",
161
+      self::API."$user/profile",
162 162
       ["Authorization: Bearer $this->token"]
163 163
     );
164 164
     if ($response !== false) return $this->process_response($code, $response);
@@ -177,8 +177,8 @@  discard block
 block discarded – undo
177 177
    * @param  string $labelFilterAction [description]
178 178
    * @return [type]                    [description]
179 179
    */
180
-  public function watch(string $topic, $labelIds=null, string $userId='me',
181
-  string $labelFilterAction='include'):?object
180
+  public function watch(string $topic, $labelIds = null, string $userId = 'me',
181
+  string $labelFilterAction = 'include'): ?object
182 182
   {
183 183
     $body = [
184 184
       'topicName'         => $topic,
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
     }
195 195
 
196 196
     list($code, $response) = (new GMailCURL(GMailCURL::POST))(
197
-      self::API . "$userId/watch",
197
+      self::API."$userId/watch",
198 198
       ["Authorization: Bearer $this->token"],
199 199
       $body
200 200
     );
@@ -208,10 +208,10 @@  discard block
 block discarded – undo
208 208
    * @param  string     $userId ID or Email Address of the user.
209 209
    * @return bool               [description]
210 210
    */
211
-  public function endWatch(string $userId='me'):bool
211
+  public function endWatch(string $userId = 'me'):bool
212 212
   {
213 213
     list($code, $response) = (new GMailCURL(GMailCURL::POST))(
214
-      self::API . "$userId/stop",
214
+      self::API."$userId/stop",
215 215
       ["Authorization: Bearer $this->token"]
216 216
     );
217 217
     if ($response !== false) return $code == 204;
@@ -224,10 +224,10 @@  discard block
 block discarded – undo
224 224
    * @param  string     $userID [description]
225 225
    * @return null|array         [description]
226 226
    */
227
-  public function getLabels(string $userId='me'):?object
227
+  public function getLabels(string $userId = 'me'): ?object
228 228
   {
229 229
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
230
-      self::API . "$userId/labels",
230
+      self::API."$userId/labels",
231 231
       ["Authorization: Bearer $this->token"]
232 232
     );
233 233
     if ($response !== false) {
@@ -247,9 +247,9 @@  discard block
 block discarded – undo
247 247
    * @param  [type]     $truncateAfter    [description]
248 248
    * @return [type]                       [description]
249 249
    */
250
-  public function getMessages(string $userId='me', array $labelIds=null,
251
-  string $q=null, int $maxMessages=null, string $pageToken=null, bool $includeSpamTrash=false,
252
-  $truncateAfter=null):?object
250
+  public function getMessages(string $userId = 'me', array $labelIds = null,
251
+  string $q = null, int $maxMessages = null, string $pageToken = null, bool $includeSpamTrash = false,
252
+  $truncateAfter = null): ?object
253 253
   {
254 254
     $query = [];
255 255
 
@@ -260,14 +260,14 @@  discard block
 block discarded – undo
260 260
     if ($maxMessages != null) $query['maxResults'] = $maxMessages;
261 261
 
262 262
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
263
-      self::API . "$userId/messages" . build_url_query($query),
263
+      self::API."$userId/messages".build_url_query($query),
264 264
       ["Authorization: Bearer $this->token"]
265 265
     );
266 266
 
267 267
     if ($response !== false) {
268 268
       if ($truncateAfter != null && $code == 200) {
269 269
         $response = json_decode($response);
270
-        $response->messages = array_filter($response->messages, function ($e) use ($truncateAfter) {
270
+        $response->messages = array_filter($response->messages, function($e) use ($truncateAfter) {
271 271
           return strcmp($truncateAfter, $e->id) < 0;
272 272
         });
273 273
         $response->{self::HTTP_CODE} = $code;
@@ -288,8 +288,8 @@  discard block
 block discarded – undo
288 288
    * @param  [type]       $metadataHeaders [description]
289 289
    * @return Message|null                  [description]
290 290
    */
291
-  public function getMessage(string $userId='me', string $messageId,
292
-  string $format='full', array $metadataHeaders=null):?Message
291
+  public function getMessage(string $userId = 'me', string $messageId,
292
+  string $format = 'full', array $metadataHeaders = null): ?Message
293 293
   {
294 294
     $query = [];
295 295
 
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
     if ($metadataHeaders != null) $query['metadataHeaders'] = $metadataHeaders;
298 298
 
299 299
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
300
-      self::API . "$userId/messages/$messageId?" . http_build_query($query),
300
+      self::API."$userId/messages/$messageId?".http_build_query($query),
301 301
       ["Authorization: Bearer $this->token"]
302 302
     );
303 303
 
@@ -311,7 +311,7 @@  discard block
 block discarded – undo
311 311
    * @param  string $response [description]
312 312
    * @return [type]           [description]
313 313
    */
314
-  private function process_response(int $code, string $response):?object {
314
+  private function process_response(int $code, string $response): ?object {
315 315
     $response = json_decode($response);
316 316
     $response->{self::HTTP_CODE} = $code;
317 317
     return $response;
Please login to merge, or discard this patch.
Braces   +48 added lines, -16 removed lines patch added patch discarded remove patch
@@ -20,7 +20,9 @@  discard block
 block discarded – undo
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")) {
@@ -67,7 +69,9 @@  discard block
 block discarded – undo
67 69
   string $responseType='code', string $accessType='offline', bool $prompt=false):string
68 70
   {
69 71
     $redirectUri = $redirectUri ?? $this->redirectUri;
70
-    if ($scope == null) throw new Exception("GMail scope cannot be null");
72
+    if ($scope == null) {
73
+      throw new Exception("GMail scope cannot be null");
74
+    }
71 75
     $params = [
72 76
       'client_id'     => $this->clientId,
73 77
       'redirect_uri'  => $redirectUri,
@@ -75,7 +79,9 @@  discard block
 block discarded – undo
75 79
       'response_type' => $responseType,
76 80
       'access_type'   => $accessType
77 81
     ];
78
-    if ($prompt) $params['prompt'] = 'consent';
82
+    if ($prompt) {
83
+      $params['prompt'] = 'consent';
84
+    }
79 85
     return self::AUTH_URL . build_url_query($params, false);
80 86
   }
81 87
   /**
@@ -111,7 +117,9 @@  discard block
 block discarded – undo
111 117
     $response = curl_exec($ch);
112 118
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
113 119
     curl_close($ch);
114
-    if ($response !== false) return $this->process_response($code, $response);
120
+    if ($response !== false) {
121
+      return $this->process_response($code, $response);
122
+    }
115 123
     return null;
116 124
   }
117 125
   /**
@@ -146,7 +154,9 @@  discard block
 block discarded – undo
146 154
     $response = curl_exec($ch);
147 155
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
148 156
     curl_close($ch);
149
-    if ($response !== false) return $this->process_response($code, $response);
157
+    if ($response !== false) {
158
+      return $this->process_response($code, $response);
159
+    }
150 160
     return null;
151 161
   }
152 162
   /**
@@ -161,7 +171,9 @@  discard block
 block discarded – undo
161 171
       self::API . "$user/profile",
162 172
       ["Authorization: Bearer $this->token"]
163 173
     );
164
-    if ($response !== false) return $this->process_response($code, $response);
174
+    if ($response !== false) {
175
+      return $this->process_response($code, $response);
176
+    }
165 177
     return null;
166 178
   }
167 179
 
@@ -198,7 +210,9 @@  discard block
 block discarded – undo
198 210
       ["Authorization: Bearer $this->token"],
199 211
       $body
200 212
     );
201
-    if ($response !== false) return $this->process_response($code, $response);
213
+    if ($response !== false) {
214
+      return $this->process_response($code, $response);
215
+    }
202 216
     return null;
203 217
   }
204 218
 
@@ -214,7 +228,9 @@  discard block
 block discarded – undo
214 228
       self::API . "$userId/stop",
215 229
       ["Authorization: Bearer $this->token"]
216 230
     );
217
-    if ($response !== false) return $code == 204;
231
+    if ($response !== false) {
232
+      return $code == 204;
233
+    }
218 234
     return false;
219 235
   }
220 236
 
@@ -253,11 +269,21 @@  discard block
 block discarded – undo
253 269
   {
254 270
     $query = [];
255 271
 
256
-    if ($labelIds != null) $query['labelIds'] = $labelIds;
257
-    if ($includeSpamTrash) $query['includeSpamTrash'] = $includeSpamTrash;
258
-    if ($q != null) $query['q'] = $q;
259
-    if ($pageToken != null) $query['pageToken'] = $pageToken;
260
-    if ($maxMessages != null) $query['maxResults'] = $maxMessages;
272
+    if ($labelIds != null) {
273
+      $query['labelIds'] = $labelIds;
274
+    }
275
+    if ($includeSpamTrash) {
276
+      $query['includeSpamTrash'] = $includeSpamTrash;
277
+    }
278
+    if ($q != null) {
279
+      $query['q'] = $q;
280
+    }
281
+    if ($pageToken != null) {
282
+      $query['pageToken'] = $pageToken;
283
+    }
284
+    if ($maxMessages != null) {
285
+      $query['maxResults'] = $maxMessages;
286
+    }
261 287
 
262 288
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
263 289
       self::API . "$userId/messages" . build_url_query($query),
@@ -293,15 +319,21 @@  discard block
 block discarded – undo
293 319
   {
294 320
     $query = [];
295 321
 
296
-    if ($format != 'full' && $format != null) $query['format'] = $format;
297
-    if ($metadataHeaders != null) $query['metadataHeaders'] = $metadataHeaders;
322
+    if ($format != 'full' && $format != null) {
323
+      $query['format'] = $format;
324
+    }
325
+    if ($metadataHeaders != null) {
326
+      $query['metadataHeaders'] = $metadataHeaders;
327
+    }
298 328
 
299 329
     list($code, $response) = (new GMailCURL(GMailCURL::GET))(
300 330
       self::API . "$userId/messages/$messageId?" . http_build_query($query),
301 331
       ["Authorization: Bearer $this->token"]
302 332
     );
303 333
 
304
-    if ($response !== false) return new Message($response);
334
+    if ($response !== false) {
335
+      return new Message($response);
336
+    }
305 337
 
306 338
     return null;
307 339
   }
Please login to merge, or discard this patch.