|
| 1 | +<?php |
| 2 | +namespace NiltonMeloX\GmailApiEasy\Gmail; |
| 3 | + |
| 4 | +use Google_Client; |
| 5 | +use Google_Service_Gmail; |
| 6 | + |
| 7 | +class Gmail { |
| 8 | + |
| 9 | + protected $config; |
| 10 | + protected $scopes; |
| 11 | + protected $accessToken; |
| 12 | + protected $emailAddress; |
| 13 | + |
| 14 | + public function __construct($config = []) |
| 15 | + { |
| 16 | + $this->config = $config; |
| 17 | + |
| 18 | + $this->scopes = implode(' ', [ |
| 19 | + Google_Service_Gmail::GMAIL_READONLY, |
| 20 | + Google_Service_Gmail::GMAIL_SEND |
| 21 | + ]); |
| 22 | + |
| 23 | + $this->accessToken = ''; |
| 24 | + $this->refreshToken = ''; |
| 25 | + |
| 26 | + $this->client = new Google_Client(); |
| 27 | + |
| 28 | + $this->getClient(); |
| 29 | + } |
| 30 | + |
| 31 | + public static function create() |
| 32 | + { |
| 33 | + return new static; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return Google_Client |
| 38 | + */ |
| 39 | + public function getClient() { |
| 40 | + $this->client->setApplicationName($this->config['app_name']); |
| 41 | + $this->client->setScopes($this->scopes); |
| 42 | + $this->client->setAuthConfig($this->getAuthConfig()); |
| 43 | + $this->client->setAccessType('offline'); |
| 44 | + $this->client->setRedirectUri($this->config['redirect_uri']); |
| 45 | + return $this->client; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + public function getAuthUrl() { |
| 52 | + |
| 53 | + $this->getClient(); |
| 54 | + $authUrl = $this->client->createAuthUrl(); |
| 55 | + |
| 56 | + return compact('authUrl'); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param $code |
| 61 | + * @return static |
| 62 | + */ |
| 63 | + public function makeAccessToken($code) { |
| 64 | + $this->getClient(); |
| 65 | + |
| 66 | + $accessToken = $this->client->fetchAccessTokenWithAuthCode($code); |
| 67 | + |
| 68 | + $this->accessToken = $accessToken['access_token']; |
| 69 | + $this->refreshToken = $accessToken['refresh_token']; |
| 70 | + |
| 71 | + $this->client->setAccessToken($this->formatAccessToken()); |
| 72 | + |
| 73 | + $me = $this->getProfile(); |
| 74 | + if($me) { |
| 75 | + $this->emailAddress = $me->emailAddress; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + return $this->saveAccessToken(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param bool $update |
| 84 | + * @return static |
| 85 | + */ |
| 86 | + public function saveAccessToken() { |
| 87 | + $config = json_decode( |
| 88 | + File::get( |
| 89 | + $file = storage_path('gmail-' . $this->emailAddress . 'json') |
| 90 | + ), |
| 91 | + true |
| 92 | + ); |
| 93 | + |
| 94 | + $config['access_token'] = $this->accessToken; |
| 95 | + $config['refresh_token'] = $this->refreshToken; |
| 96 | + $config['email'] = $this->emailAddress; |
| 97 | + |
| 98 | + File::put($file, json_encode($config)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * |
| 103 | + */ |
| 104 | + public function revokeAccess() { |
| 105 | + $this->client->revokeToken(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return \Google_Service_Gmail_Profile |
| 110 | + */ |
| 111 | + private function getProfile() { |
| 112 | + $service = new Google_Service_Gmail($this->client); |
| 113 | + return $service->users->getProfile('me'); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * |
| 118 | + */ |
| 119 | + public function isAccessTokenExpired() { |
| 120 | + |
| 121 | + if($this->client->isAccessTokenExpired()) { |
| 122 | + |
| 123 | + $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); |
| 124 | + $accessToken = $this->client->getAccessToken(); |
| 125 | + |
| 126 | + $this->accessToken = $accessToken['access_token']; |
| 127 | + $this->refreshToken = $accessToken['refresh_token']; |
| 128 | + |
| 129 | + $this->saveAccessToken(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param $accessToken |
| 135 | + * @param $refreshToken |
| 136 | + * @return $this |
| 137 | + */ |
| 138 | + public function setAccessToken($accessToken, $refreshToken) { |
| 139 | + $this->accessToken = $accessToken; |
| 140 | + $this->refreshToken = $refreshToken; |
| 141 | + $this->client->setAccessToken($this->formatAccessToken()); |
| 142 | + |
| 143 | + return $this; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + /** |
| 148 | + * @return array |
| 149 | + */ |
| 150 | + private function formatAccessToken() { |
| 151 | + return [ |
| 152 | + "access_token" => $this->accessToken, |
| 153 | + "token_type" => "Bearer", |
| 154 | + "expires_in" => 3600, |
| 155 | + "refresh_token" => $this->refreshToken, |
| 156 | + "created" => 1492614871 |
| 157 | + ]; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + private function getAuthConfig() |
| 164 | + { |
| 165 | + return [ |
| 166 | + 'installed' => [ |
| 167 | + 'client_id' => $this->config['client_id'], |
| 168 | + 'project_id' => $this->config['project_id'], |
| 169 | + 'auth_uri' => 'https://accounts.google.com/o/oauth2/auth', |
| 170 | + 'token_uri' => 'https://accounts.google.com/o/oauth2/token', |
| 171 | + 'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs', |
| 172 | + 'client_secret' => $this->config['secret'], |
| 173 | + 'redirect_uris' => [ |
| 174 | + "urn:ietf:wg:oauth:2.0:oob", |
| 175 | + "http://localhost", |
| 176 | + $this->config['redirect_uri'], |
| 177 | + ] |
| 178 | + ] |
| 179 | + ]; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param $list |
| 184 | + * @param $property |
| 185 | + * @return string|null |
| 186 | + */ |
| 187 | + public function findProperty($list, $property) { |
| 188 | + |
| 189 | + $find = $list->filter(function($e) use ($property) { |
| 190 | + return $e->name == $property; |
| 191 | + })->first(); |
| 192 | + |
| 193 | + if($find) { |
| 194 | + return $find->value; |
| 195 | + } |
| 196 | + return null; |
| 197 | + } |
| 198 | + |
| 199 | +} |
| 200 | + |
0 commit comments