feat: improved performance by avoiding redundant MailJet API calls
This commit is contained in:
@@ -96,23 +96,26 @@ class NewsletterManager
|
||||
}
|
||||
}
|
||||
|
||||
$subscribedListIds = $this->subscribedMailjetListIds($normalizedEmail, $normalizedListIds);
|
||||
$missingListIds = array_values(array_diff($normalizedListIds, $subscribedListIds));
|
||||
$hasConfirmedOptIn = null !== $this->consentRepository->findActiveByEmail($normalizedEmail);
|
||||
|
||||
if ([] === $missingListIds) {
|
||||
$this->recordSubscription($normalizedEmail, $normalizedListIds, [], $normalizedFirstName, $normalizedLastName);
|
||||
// We trust our local consent state for DOI decisions. If Mailjet is ahead of the database,
|
||||
// we accept a redundant confirmation cycle and reconcile the contact on confirmation.
|
||||
if (true === $hasConfirmedOptIn) {
|
||||
$confirmedListIds = $this->confirmedMailjetListIds($normalizedEmail, $normalizedListIds);
|
||||
$missingListIds = array_values(array_diff($normalizedListIds, $confirmedListIds));
|
||||
|
||||
return new NewsletterSubscriptionRequestResult(
|
||||
$normalizedEmail,
|
||||
$normalizedListIds,
|
||||
NewsletterSubscriptionRequestResult::STATE_SUBSCRIBED,
|
||||
false,
|
||||
$this->createListStates($normalizedListIds, NewsletterSubscriptionRequestResult::LIST_STATE_ALREADY_REGISTERED),
|
||||
);
|
||||
}
|
||||
if ([] === $missingListIds) {
|
||||
$this->recordSubscription($normalizedEmail, $normalizedListIds, [], $normalizedFirstName, $normalizedLastName);
|
||||
|
||||
return new NewsletterSubscriptionRequestResult(
|
||||
$normalizedEmail,
|
||||
$normalizedListIds,
|
||||
NewsletterSubscriptionRequestResult::STATE_SUBSCRIBED,
|
||||
false,
|
||||
$this->createListStates($normalizedListIds, NewsletterSubscriptionRequestResult::LIST_STATE_ALREADY_REGISTERED),
|
||||
);
|
||||
}
|
||||
|
||||
if (true === $hasConfirmedOptIn || [] !== $subscribedListIds || true === $this->isSubscribedToKnownList($normalizedEmail, $knownNormalizedListIds, $normalizedListIds)) {
|
||||
$this->recordSubscription($normalizedEmail, $normalizedListIds, $missingListIds, $normalizedFirstName, $normalizedLastName);
|
||||
|
||||
return new NewsletterSubscriptionRequestResult(
|
||||
@@ -389,17 +392,18 @@ class NewsletterManager
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
private function subscribedMailjetListIds(string $email, array $mailjetListIds): array
|
||||
private function confirmedMailjetListIds(string $email, array $mailjetListIds): array
|
||||
{
|
||||
$subscribedListIds = [];
|
||||
$confirmedListIds = [];
|
||||
|
||||
foreach ($mailjetListIds as $mailjetListId) {
|
||||
if (true === $this->newsletterService->isSubscribed($email, $mailjetListId)) {
|
||||
$subscribedListIds[] = $mailjetListId;
|
||||
$consent = $this->consentRepository->findOneByEmailAndListId($email, $mailjetListId);
|
||||
if (null !== $consent && true === $consent->isConfirmed()) {
|
||||
$confirmedListIds[] = $mailjetListId;
|
||||
}
|
||||
}
|
||||
|
||||
return $subscribedListIds;
|
||||
return $confirmedListIds;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -484,27 +488,6 @@ class NewsletterManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $knownMailjetListIds
|
||||
* @param list<int> $alreadyCheckedListIds
|
||||
*/
|
||||
private function isSubscribedToKnownList(string $normalizedEmail, array $knownMailjetListIds, array $alreadyCheckedListIds): bool
|
||||
{
|
||||
$alreadyCheckedListIdMap = array_fill_keys($alreadyCheckedListIds, true);
|
||||
|
||||
foreach ($knownMailjetListIds as $mailjetListId) {
|
||||
if (true === isset($alreadyCheckedListIdMap[$mailjetListId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (true === $this->newsletterService->isSubscribed($normalizedEmail, $mailjetListId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function defaultMailjetListId(): int
|
||||
{
|
||||
if (null === $this->defaultMailjetListId || '' === trim($this->defaultMailjetListId)) {
|
||||
|
||||
Reference in New Issue
Block a user