Skip to content

Commit 1d1e725

Browse files
authored
Merge branch 'v7' into codex/add-basic-unit-tests-and-setup-database
2 parents 46da3cb + 67881f2 commit 1d1e725

15 files changed

+347
-117
lines changed

src/Contracts/TokenRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Dacastro4\LaravelGmail\Contracts;
6+
7+
interface TokenRepository
8+
{
9+
public function tokenExists(string $fileName): bool;
10+
11+
public function getToken(string $fileName, bool $allowJsonEncrypt): array;
12+
13+
public function storeToken(string $fileName, array $config, bool $allowJsonEncrypt): void;
14+
15+
public function deleteToken(string $fileName, bool $allowJsonEncrypt): void;
16+
}

src/GmailConnection.php

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Dacastro4\LaravelGmail;
66

7+
use Dacastro4\LaravelGmail\Contracts\TokenRepository;
78
use Dacastro4\LaravelGmail\Traits\Configurable;
89
use Dacastro4\LaravelGmail\Traits\HasLabels;
910
use Google_Client;
1011
use Google_Service_Gmail;
1112
use Illuminate\Container\Container;
1213
use Illuminate\Http\Request;
13-
use Illuminate\Support\Facades\Storage;
1414

1515
class 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
{

src/LaravelGmailClass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44

55
namespace Dacastro4\LaravelGmail;
66

7+
use Dacastro4\LaravelGmail\Contracts\TokenRepository;
78
use Dacastro4\LaravelGmail\Exceptions\AuthException;
89
use Dacastro4\LaravelGmail\Services\Message;
910
use Illuminate\Http\RedirectResponse;
1011
use Illuminate\Support\Facades\Redirect;
1112

1213
class LaravelGmailClass extends GmailConnection
1314
{
14-
public function __construct(mixed $config, ?string $userId = null)
15+
public function __construct(mixed $config, TokenRepository $tokenRepository, ?string $userId = null)
1516
{
1617
if (class_basename($config) === 'Application') {
1718
$config = $config['config'];
1819
}
1920

20-
parent::__construct($config, $userId);
21+
parent::__construct($tokenRepository, $config, $userId);
2122
}
2223

2324
/**

src/LaravelGmailServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Dacastro4\LaravelGmail;
66

7+
use Dacastro4\LaravelGmail\Contracts\TokenRepository;
8+
use Dacastro4\LaravelGmail\Repositories\StorageTokenRepository;
79
use Illuminate\Support\Facades\App;
810
use Illuminate\Support\ServiceProvider;
911

@@ -18,9 +20,11 @@ public function register(): void
1820
{
1921
$this->mergeConfigFrom(__DIR__.'/config/gmail.php', 'gmail');
2022

23+
$this->app->bind(TokenRepository::class, fn (): TokenRepository => new StorageTokenRepository);
24+
2125
// Main Service
2226
$this->app->bind('laravelgmail', function ($app): LaravelGmailClass {
23-
return new LaravelGmailClass($app['config']);
27+
return new LaravelGmailClass($app['config'], $app->make(TokenRepository::class));
2428
});
2529
}
2630
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Dacastro4\LaravelGmail\Repositories;
6+
7+
use Dacastro4\LaravelGmail\Contracts\TokenRepository;
8+
use Illuminate\Support\Facades\Storage;
9+
10+
class StorageTokenRepository implements TokenRepository
11+
{
12+
public function tokenExists(string $fileName): bool
13+
{
14+
$file = "gmail/tokens/$fileName.json";
15+
16+
return Storage::disk('local')->exists($file);
17+
}
18+
19+
public function getToken(string $fileName, bool $allowJsonEncrypt): array
20+
{
21+
$file = "gmail/tokens/$fileName.json";
22+
$disk = Storage::disk('local');
23+
24+
if (! $disk->exists($file)) {
25+
return [];
26+
}
27+
28+
$contents = $disk->get($file);
29+
30+
return json_decode($allowJsonEncrypt ? decrypt($contents) : $contents, true) ?? [];
31+
}
32+
33+
public function storeToken(string $fileName, array $config, bool $allowJsonEncrypt): void
34+
{
35+
$disk = Storage::disk('local');
36+
$file = "gmail/tokens/$fileName.json";
37+
38+
if ($disk->exists($file)) {
39+
$savedConfigToken = $this->getToken($fileName, $allowJsonEncrypt);
40+
if (empty($config['email']) && isset($savedConfigToken['email'])) {
41+
$config['email'] = $savedConfigToken['email'];
42+
}
43+
$disk->delete($file);
44+
}
45+
46+
if ($allowJsonEncrypt) {
47+
$disk->put($file, encrypt(json_encode($config)));
48+
} else {
49+
$disk->put($file, json_encode($config));
50+
}
51+
}
52+
53+
public function deleteToken(string $fileName, bool $allowJsonEncrypt): void
54+
{
55+
$disk = Storage::disk('local');
56+
$file = "gmail/tokens/$fileName.json";
57+
58+
if ($disk->exists($file)) {
59+
$disk->delete($file);
60+
}
61+
62+
if ($allowJsonEncrypt) {
63+
$disk->put($file, encrypt(json_encode([])));
64+
} else {
65+
$disk->put($file, json_encode([]));
66+
}
67+
}
68+
}

src/Services/Message.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Dacastro4\LaravelGmail\Traits\Filterable;
1010
use Dacastro4\LaravelGmail\Traits\SendsParameters;
1111
use Google_Service_Gmail;
12-
use Google_Service_Gmail_ListMessagesResponse;
1312
use Google_Service_Gmail_Message;
1413
use Illuminate\Support\Collection;
1514

@@ -71,7 +70,7 @@ public function all(?string $pageToken = null): Collection
7170

7271
if (! $this->preload) {
7372
foreach ($messages as $message) {
74-
$mails[] = new Mail($message, $this->preload, $this->client->userId);
73+
$mails[] = new Mail($this->client->getTokenRepository(), $message, $this->preload, $this->client->userId);
7574
}
7675
} else {
7776
$mails = count($messages) > 0 ? $this->batchRequest($messages) : [];
@@ -107,7 +106,7 @@ public function get(string $id): Mail
107106
{
108107
$message = $this->getRequest($id);
109108

110-
return new Mail($message, false, $this->client->userId);
109+
return new Mail($this->client->getTokenRepository(), $message, false, $this->client->userId);
111110
}
112111

113112
/**
@@ -130,7 +129,7 @@ public function batchRequest(array $allMessages): array
130129
$messages = [];
131130

132131
foreach ($messagesBatch as $message) {
133-
$messages[] = new Mail($message, false, $this->client->userId);
132+
$messages[] = new Mail($this->client->getTokenRepository(), $message, false, $this->client->userId);
134133
}
135134

136135
return $messages;

0 commit comments

Comments
 (0)