-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuotaService.php
67 lines (52 loc) · 1.9 KB
/
QuotaService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Sendportal\Base\Services;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Sendportal\Base\Adapters\BaseMailAdapter;
use Sendportal\Base\Factories\MailAdapterFactory;
use Sendportal\Base\Interfaces\QuotaServiceInterface;
use Sendportal\Base\Models\EmailService;
use Sendportal\Base\Models\EmailServiceType;
class QuotaService implements QuotaServiceInterface
{
public function exceedsQuota(EmailService $emailService, int $messageCount): bool
{
switch ($emailService->type_id) {
case EmailServiceType::SES:
return $this->exceedsSesQuota($emailService, $messageCount);
case EmailServiceType::SENDGRID:
case EmailServiceType::MAILGUN:
case EmailServiceType::POSTMARK:
case EmailServiceType::MAILJET:
case EmailServiceType::SMTP:
return false;
}
throw new \DomainException('Unrecognised email service type');
}
protected function resolveMailAdapter(EmailService $emailService): BaseMailAdapter
{
return app(MailAdapterFactory::class)->adapter($emailService);
}
protected function exceedsSesQuota(EmailService $emailService, int $messageCount): bool
{
$mailAdapter = $this->resolveMailAdapter($emailService);
$quota = $mailAdapter->getSendQuota();
if (empty($quota)) {
Log::error(
'Failed to fetch quota from SES',
[
'email_service_id' => $emailService->id,
]
);
return false;
}
$limit = Arr::get($quota, 'Max24HourSend');
// -1 signifies an unlimited quota
if ($limit === -1) {
return false;
}
$sent = Arr::get($quota, 'SentLast24Hours');
$remaining = (int)floor($limit - $sent);
return $messageCount > $remaining;
}
}