44
55namespace Dacastro4 \LaravelGmail ;
66
7+ use Dacastro4 \LaravelGmail \Contracts \TokenRepository ;
78use Dacastro4 \LaravelGmail \Traits \Configurable ;
89use Dacastro4 \LaravelGmail \Traits \HasLabels ;
910use Google_Client ;
1011use Google_Service_Gmail ;
1112use Illuminate \Container \Container ;
1213use Illuminate \Http \Request ;
13- use Illuminate \Support \Facades \Storage ;
1414
1515class GmailConnection extends Google_Client
1616{
@@ -33,10 +33,14 @@ class GmailConnection extends Google_Client
3333
3434 public ?string $ userId = null ;
3535
36- public function __construct (mixed $ config = null , ?string $ userId = null )
36+ protected TokenRepository $ tokenRepository ;
37+
38+ public function __construct (TokenRepository $ tokenRepository , mixed $ config = null , ?string $ userId = null )
3739 {
3840 $ this ->app = Container::getInstance ();
3941
42+ $ this ->tokenRepository = $ tokenRepository ;
43+
4044 $ this ->userId = $ userId ;
4145
4246 $ this ->configConstruct ($ config );
@@ -53,29 +57,29 @@ public function __construct(mixed $config = null, ?string $userId = null)
5357
5458 }
5559
60+ public function getTokenRepository (): TokenRepository
61+ {
62+ return $ this ->tokenRepository ;
63+ }
64+
5665 public function getUserId (): ?string
5766 {
5867 return $ this ->userId ;
5968 }
6069
6170 /**
6271 * Check and return true if the user has previously logged in without checking if the token needs to refresh
63- *
64- * @return bool
6572 */
6673 public function checkPreviouslyLoggedIn (): bool
6774 {
6875 $ fileName = $ this ->getFileName ();
69- $ file = "gmail/tokens/ $ fileName.json " ;
70- $ disk = Storage::disk ('local ' );
76+ $ allowJsonEncrypt = $ this ->_config ['gmail.allow_json_encrypt ' ];
7177
72- if (! $ disk -> exists ( $ file )) {
78+ if (! $ this -> tokenRepository -> tokenExists ( $ fileName )) {
7379 return false ;
7480 }
7581
76- $ contents = $ disk ->get ($ file );
77- $ allowJsonEncrypt = $ this ->_config ['gmail.allow_json_encrypt ' ];
78- $ savedConfigToken = json_decode ($ allowJsonEncrypt ? decrypt ($ contents ) : $ contents , true );
82+ $ savedConfigToken = $ this ->tokenRepository ->getToken ($ fileName , $ allowJsonEncrypt );
7983
8084 return ! empty ($ savedConfigToken ['access_token ' ]);
8185 }
@@ -152,39 +156,14 @@ public function setBothAccessToken(array|string $token): void
152156 */
153157 public function saveAccessToken (array $ config ): void
154158 {
155- $ disk = Storage::disk ('local ' );
156159 $ fileName = $ this ->getFileName ();
157- $ file = "gmail/tokens/ $ fileName.json " ;
158160 $ allowJsonEncrypt = $ this ->_config ['gmail.allow_json_encrypt ' ];
159161 $ config ['email ' ] = $ this ->emailAddress ;
160162
161- if ($ disk ->exists ($ file )) {
162-
163- if (empty ($ config ['email ' ])) {
164- if ($ allowJsonEncrypt ) {
165- $ savedConfigToken = json_decode (decrypt ($ disk ->get ($ file )), true );
166- } else {
167- $ savedConfigToken = json_decode ($ disk ->get ($ file ), true );
168- }
169- if (isset ($ savedConfigToken ['email ' ])) {
170- $ config ['email ' ] = $ savedConfigToken ['email ' ];
171- }
172- }
173-
174- $ disk ->delete ($ file );
175- }
176-
177- if ($ allowJsonEncrypt ) {
178- $ disk ->put ($ file , encrypt (json_encode ($ config )));
179- } else {
180- $ disk ->put ($ file , json_encode ($ config ));
181- }
182-
163+ $ this ->tokenRepository ->storeToken ($ fileName , $ config , $ allowJsonEncrypt );
183164 }
184165
185166 /**
186- * @return array|string
187- *
188167 * @throws \Exception
189168 */
190169 public function makeToken (Request $ request ): array |string
@@ -213,8 +192,6 @@ public function makeToken(Request $request): array|string
213192
214193 /**
215194 * Check
216- *
217- * @return bool
218195 */
219196 public function check (): bool
220197 {
@@ -223,8 +200,6 @@ public function check(): bool
223200
224201 /**
225202 * Gets user profile from Gmail
226- *
227- * @return \Google_Service_Gmail_Profile
228203 */
229204 public function getProfile (): \Google_Service_Gmail_Profile
230205 {
@@ -246,22 +221,10 @@ public function logout(): void
246221 */
247222 public function deleteAccessToken (): void
248223 {
249- $ disk = Storage::disk ('local ' );
250224 $ fileName = $ this ->getFileName ();
251- $ file = "gmail/tokens/ $ fileName.json " ;
252-
253225 $ allowJsonEncrypt = $ this ->_config ['gmail.allow_json_encrypt ' ];
254226
255- if ($ disk ->exists ($ file )) {
256- $ disk ->delete ($ file );
257- }
258-
259- if ($ allowJsonEncrypt ) {
260- $ disk ->put ($ file , encrypt (json_encode ([])));
261- } else {
262- $ disk ->put ($ file , json_encode ([]));
263- }
264-
227+ $ this ->tokenRepository ->deleteToken ($ fileName , $ allowJsonEncrypt );
265228 }
266229
267230 private function haveReadScope (): bool
@@ -275,8 +238,6 @@ private function haveReadScope(): bool
275238 * users.stop receiving push notifications for the given user mailbox.
276239 *
277240 * @param string $userEmail Email address
278- * @param array $optParams
279- * @return \Google_Service_Gmail_Stop
280241 */
281242 public function stopWatch (string $ userEmail , array $ optParams = []): \Google_Service_Gmail_Stop
282243 {
@@ -299,8 +260,6 @@ public function setWatch(string $userEmail, \Google_Service_Gmail_WatchRequest $
299260
300261 /**
301262 * Lists the history of all changes to the given mailbox. History results are returned in chronological order (increasing historyId).
302- *
303- * @return \Google\Service\Gmail\ListHistoryResponse
304263 */
305264 public function historyList (string $ userEmail , array $ params ): \Google \Service \Gmail \ListHistoryResponse
306265 {
0 commit comments