1 | <?php |
||
12 | class GmailConnection extends Google_Client |
||
13 | { |
||
14 | |||
15 | protected $emailAddress; |
||
16 | protected $refreshToken; |
||
17 | protected $app; |
||
18 | protected $accessToken; |
||
19 | protected $token; |
||
20 | private $config; |
||
|
|||
21 | |||
22 | public function __construct(Config $config = null) |
||
23 | { |
||
24 | $this->app = Container::getInstance(); |
||
25 | $this->config = $config; |
||
26 | parent::__construct( $this->getConfigs() ); |
||
27 | $this->setScopes( $this->getUserScopes() ); |
||
28 | $this->setAccessType( 'offline' ); |
||
29 | $this->refreshTokenIfNeeded(); |
||
30 | } |
||
31 | |||
32 | public function getAccessToken() |
||
33 | { |
||
34 | $token = parent::getAccessToken() ?: $this->config(); |
||
35 | return $token; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return array|string |
||
40 | * @throws \Exception |
||
41 | */ |
||
42 | public function makeToken() |
||
62 | |||
63 | public function setToken( $token ) |
||
67 | |||
68 | public function check() |
||
72 | |||
73 | public function isAccessTokenExpired() |
||
74 | { |
||
75 | $token = parent::getAccessToken() ?: $this->config(); |
||
76 | $this->setAccessToken($token); |
||
77 | |||
78 | return parent::isAccessTokenExpired(); |
||
79 | } |
||
80 | |||
81 | public function logout() |
||
86 | |||
87 | |||
88 | private function refreshTokenIfNeeded() |
||
89 | { |
||
96 | |||
97 | /** |
||
98 | * @return \Google_Service_Gmail_Profile |
||
99 | */ |
||
100 | public function getProfile() |
||
106 | |||
107 | public function setAccessToken($token) |
||
112 | |||
113 | /** |
||
114 | * Save the credentials in a file |
||
115 | * |
||
116 | * @param array $config |
||
117 | */ |
||
118 | public function saveAccessToken( array $config) |
||
128 | |||
129 | /** |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getConfigs() |
||
140 | |||
141 | public function config( $string = null, $email = null ) |
||
163 | |||
164 | private function getFileName() |
||
169 | |||
170 | private function getUserScopes() |
||
177 | |||
178 | } |