Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SendingBlueEventListener to correctly send DoubleOpt-In emails #351

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 3 additions & 42 deletions Event/SendinblueListSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

use GuzzleHttp\ClientInterface;
use SendinBlue\Client\Api\ContactsApi;
use SendinBlue\Client\ApiException;
use SendinBlue\Client\Configuration;
use SendinBlue\Client\Model\CreateDoiContact;
use SendinBlue\Client\Model\UpdateContact;
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -113,54 +111,17 @@ public function listSubscribe(FormSavePostEvent $event): void
return;
}

$contact = null;
try {
$contact = $this->contactsApi->getContactInfo($email);
} catch (ApiException $e) {
if (404 !== $e->getCode()) {
throw $e;
}
// Contact does not exist, ignore the exception
}

if (null !== $contact) {
$updateContact = new UpdateContact();

$updateContact->setAttributes(
(object) \array_replace(
(array) $contact->getAttributes(),
[
'FIRST_NAME' => $firstName,
'LAST_NAME' => $lastName,
]
)
);

/** @var int[] $collectedListIds */
$collectedListIds = $contact->getListIds();
foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) {
$collectedListIds = \array_merge($collectedListIds, $listIds);
}

$collectedListIds = \array_unique($collectedListIds);

$updateContact->setListIds($collectedListIds);

$this->contactsApi->updateContact($email, $updateContact);

return;
}

foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) {
$createDoiContact = new CreateDoiContact([
'email' => $email,
'templateId' => $mailTemplateId,
'includeListIds' => $listIds,
'redirectionUrl' => $redirectionUrl,
'attributes' => [
'FIRST_NAME' => $firstName,
'LAST_NAME' => $lastName,
'firstname' => $firstName,
'lastname' => $lastName,
],
'updateEnabled' => true,
]);

$this->contactsApi->createDoiContact($createDoiContact);
Expand Down
61 changes: 4 additions & 57 deletions Tests/Unit/Event/SendinblueListSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\RequestInterface;
use SendinBlue\Client\ApiException;
use Sulu\Bundle\FormBundle\Configuration\FormConfiguration;
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Sulu\Bundle\FormBundle\Entity\Form;
Expand Down Expand Up @@ -78,12 +77,6 @@ public function testlistSubscribeNotExist(): void
/** @var RequestInterface $request */
$request = $args[0];

if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()) {
$self->assertSame('GET', $request->getMethod());

throw new ApiException('', 404);
}

if ('https://api.sendinblue.com/v3/contacts/doubleOptinConfirmation' === $request->getUri()->__toString()) {
$self->assertSame('POST', $request->getMethod());

Expand All @@ -92,67 +85,21 @@ public function testlistSubscribeNotExist(): void
$self->assertSame([
'email' => 'john.doe@example.org',
'attributes' => [
'FIRST_NAME' => 'John',
'LAST_NAME' => 'Doe',
'firstname' => 'John',
'lastname' => 'Doe',
],
'includeListIds' => ['789'],
'templateId' => 456,
'redirectionUrl' => 'http://localhost?subscribe=true',
'updateEnable' => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'updateEnable' => true,
'updateEnabled' => true,

], $json);

return new Response();
}

throw new \RuntimeException('Unexpected request: ' . $request->getUri()->__toString());
})
->shouldBeCalledTimes(2);

// act
$this->sendinblueListSubscriber->listSubscribe($event);

$this->assertTrue(true);
}

public function testlistSubscribeAlreadyExist(): void
{
$this->requestStack->push(Request::create('http://localhost/', 'POST'));
$event = $this->createFormSavePostEvent();

$self = $this;
$this->client->send(Argument::cetera())->will(function($args) use ($self) {
/** @var RequestInterface $request */
$request = $args[0];

if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()
&& 'GET' === $request->getMethod()
) {
return new Response(200, ['Content-Type' => 'application/json'], \json_encode([
'id' => 123,
'email' => 'john.doe@example.org',
'attributes' => [],
'listIds' => [],
]));
}

if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()
&& 'PUT' === $request->getMethod()
) {
$json = \json_decode($request->getBody()->getContents(), true);

$self->assertSame([
'attributes' => [
'FIRST_NAME' => 'John',
'LAST_NAME' => 'Doe',
],
'listIds' => ['789'],
], $json);

return new Response();
}

throw new \RuntimeException('Unexpected request (' . $request->getMethod() . '): ' . $request->getUri()->__toString());
})
->shouldBeCalledTimes(2);
->shouldBeCalledOnce();

// act
$this->sendinblueListSubscriber->listSubscribe($event);
Expand Down