-
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathMessagingEligibilityService.php
More file actions
134 lines (107 loc) · 4.63 KB
/
MessagingEligibilityService.php
File metadata and controls
134 lines (107 loc) · 4.63 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace HiEvents\Services\Domain\Message;
use Carbon\Carbon;
use HiEvents\DomainObjects\AccountMessagingTierDomainObject;
use HiEvents\DomainObjects\AccountStripePlatformDomainObject;
use HiEvents\DomainObjects\Enums\MessagingEligibilityFailureEnum;
use HiEvents\DomainObjects\Enums\MessagingTierViolationEnum;
use HiEvents\Repository\Interfaces\AccountMessagingTierRepositoryInterface;
use HiEvents\Repository\Interfaces\AccountRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Services\Domain\Message\DTO\MessagingEligibilityFailureDTO;
use HiEvents\Services\Domain\Message\DTO\MessagingTierViolationDTO;
class MessagingEligibilityService
{
private const UNTRUSTED_TIER_NAME = 'Untrusted';
public function __construct(
private readonly AccountRepositoryInterface $accountRepository,
private readonly EventRepositoryInterface $eventRepository,
private readonly MessageRepositoryInterface $messageRepository,
private readonly AccountMessagingTierRepositoryInterface $accountMessagingTierRepository,
private readonly OrderRepositoryInterface $orderRepository,
) {
}
public function checkEligibility(int $accountId, int $eventId): ?MessagingEligibilityFailureDTO
{
$account = $this->accountRepository
->loadRelation(AccountStripePlatformDomainObject::class)
->findById($accountId);
$tier = $this->getAccountMessagingTier($account->getAccountMessagingTierId());
// Trusted and Premium tiers bypass eligibility checks
if ($tier->getName() !== self::UNTRUSTED_TIER_NAME) {
return null;
}
$failures = [];
if (!$account->isStripeSetupComplete()) {
$failures[] = MessagingEligibilityFailureEnum::STRIPE_NOT_CONNECTED;
}
if (!$this->hasPaidOrder($accountId)) {
$failures[] = MessagingEligibilityFailureEnum::NO_PAID_ORDERS;
}
$event = $this->eventRepository->findById($eventId);
if ($this->isEventTooNew($event->getCreatedAt())) {
$failures[] = MessagingEligibilityFailureEnum::EVENT_TOO_NEW;
}
if (empty($failures)) {
return null;
}
return new MessagingEligibilityFailureDTO(
accountId: $accountId,
eventId: $eventId,
failures: $failures,
);
}
public function checkTierLimits(int $accountId, int $recipientCount, string $messageContent): ?MessagingTierViolationDTO
{
$violations = [];
$account = $this->accountRepository->findById($accountId);
$tier = $this->getAccountMessagingTier($account->getAccountMessagingTierId());
$messagesInLast24h = $this->messageRepository->countMessagesInLast24Hours($accountId);
if ($messagesInLast24h >= $tier->getMaxMessagesPer24h()) {
$violations[] = MessagingTierViolationEnum::MESSAGE_LIMIT_EXCEEDED;
}
if ($recipientCount > $tier->getMaxRecipientsPerMessage()) {
$violations[] = MessagingTierViolationEnum::RECIPIENT_LIMIT_EXCEEDED;
}
if (!$tier->getLinksAllowed() && $this->containsLinks($messageContent)) {
$violations[] = MessagingTierViolationEnum::LINKS_NOT_ALLOWED;
}
if (empty($violations)) {
return null;
}
return new MessagingTierViolationDTO(
accountId: $accountId,
tierName: $tier->getName(),
violations: $violations,
);
}
private function getAccountMessagingTier(?int $tierId): AccountMessagingTierDomainObject
{
if ($tierId !== null) {
$tier = $this->accountMessagingTierRepository->findFirst($tierId);
if ($tier !== null) {
return $tier;
}
}
return $this->accountMessagingTierRepository->findFirstWhere([
'name' => self::UNTRUSTED_TIER_NAME,
]);
}
private function hasPaidOrder(int $accountId): bool
{
return $this->orderRepository->hasCompletedPaidOrderForAccount($accountId);
}
private function isEventTooNew(string $createdAt): bool
{
$eventCreatedAt = Carbon::parse($createdAt);
$twentyFourHoursAgo = Carbon::now()->subHours(24);
return $eventCreatedAt->isAfter($twentyFourHoursAgo);
}
private function containsLinks(string $content): bool
{
$urlPattern = '/https?:\/\/[^\s<>"\']+|href\s*=\s*["\'][^"\']+["\']/i';
return (bool) preg_match($urlPattern, $content);
}
}