|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Client; |
|
4
|
|
|
|
|
5
|
|
|
use App\DataProvider\EmailTemplateProvider; |
|
6
|
|
|
use App\Enum\PropertyFilter; |
|
7
|
|
|
use App\Exception\GmailApiException; |
|
8
|
|
|
use App\Exception\GoogleInsufficientPermissionException; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Google\Service\Gmail\Label; |
|
11
|
|
|
use Google\Service\Gmail\Message; |
|
12
|
|
|
use Google_Service_Gmail; |
|
13
|
|
|
|
|
14
|
|
|
class GmailApiClient |
|
15
|
|
|
{ |
|
16
|
2 |
|
public function __construct( |
|
17
|
|
|
private Google_Service_Gmail $gmailService, |
|
18
|
|
|
private EmailTemplateProvider $emailTemplateProvider |
|
19
|
2 |
|
) {} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return Message[] |
|
23
|
|
|
* |
|
24
|
|
|
* @throws GmailApiException|GoogleInsufficientPermissionException |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function getMessages(array $criteria, string $accessToken, string $userId = 'me'): array |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->gmailService->getClient()->setAccessToken($accessToken); |
|
29
|
|
|
|
|
30
|
2 |
|
$messages = []; |
|
31
|
2 |
|
$labelId = $criteria[PropertyFilter::GMAIL_LABEL] ?? null; |
|
32
|
2 |
|
$provider = $criteria[PropertyFilter::PROVIDER] ?? null; |
|
33
|
2 |
|
$newerThan = $criteria[PropertyFilter::NEWER_THAN]; |
|
34
|
|
|
|
|
35
|
|
|
// Prepare the Gmail query |
|
36
|
2 |
|
$params = ['q' => $this->buildMessagesQuery($provider, $newerThan)]; |
|
37
|
2 |
|
if (!empty($labelId)) { |
|
38
|
1 |
|
$params['labelIds'] = [$labelId]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
do { |
|
42
|
|
|
try { |
|
43
|
2 |
|
$response = $this->gmailService->users_messages->listUsersMessages($userId, $params); |
|
44
|
|
|
} catch (Exception $e) { |
|
45
|
|
|
$error = json_decode($e->getMessage(), true); |
|
46
|
|
|
|
|
47
|
|
|
if (403 === $error['error']['code']) { |
|
48
|
|
|
throw new GoogleInsufficientPermissionException($error['error']['message']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new GmailApiException($error['error']['message']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
$messages[] = $response->getMessages(); |
|
55
|
2 |
|
} while (null !== $params['pageToken'] = $response->getNextPageToken()); |
|
56
|
|
|
|
|
57
|
|
|
// Flatten the array |
|
58
|
2 |
|
return array_merge(...$messages); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws GmailApiException|GoogleInsufficientPermissionException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getMessage(string $messageId, string $userId = 'me'): Message |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
return $this->gmailService->users_messages->get($userId, $messageId); |
|
68
|
|
|
} catch (Exception $e) { |
|
69
|
|
|
$error = json_decode($e->getMessage(), true); |
|
70
|
|
|
|
|
71
|
|
|
if (403 === $error['error']['code']) { |
|
72
|
|
|
throw new GoogleInsufficientPermissionException($error['error']['message']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new GmailApiException($error['error']['message']); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Label[] |
|
81
|
|
|
* |
|
82
|
|
|
* @throws GmailApiException|GoogleInsufficientPermissionException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getLabels(string $accessToken, string $userId = 'me'): array |
|
85
|
|
|
{ |
|
86
|
|
|
$this->gmailService->getClient()->setAccessToken($accessToken); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
return $this->gmailService->users_labels->listUsersLabels($userId)->getLabels(); |
|
90
|
|
|
} catch (Exception $e) { |
|
91
|
|
|
$error = json_decode($e->getMessage(), true); |
|
92
|
|
|
|
|
93
|
|
|
if (403 === $error['error']['code']) { |
|
94
|
|
|
throw new GoogleInsufficientPermissionException($error['error']['message']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
throw new GmailApiException($error['error']['message']); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
private function buildMessagesQuery(string $provider = null, int $newerThan = null): string |
|
102
|
|
|
{ |
|
103
|
2 |
|
$addresses = null !== $provider ? |
|
104
|
1 |
|
$this->emailTemplateProvider->getAddressesByMainProvider($provider) : |
|
105
|
1 |
|
$this->emailTemplateProvider->getAllAddresses(); |
|
106
|
|
|
|
|
107
|
2 |
|
$fromFilter = sprintf('from:(%s)', implode(' | ', $addresses)); |
|
108
|
2 |
|
$dateFilter = sprintf('newer_than:%dd', $newerThan); |
|
109
|
|
|
|
|
110
|
2 |
|
return implode(' ', [$fromFilter, $dateFilter]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|