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