Skip to content

Commit

Permalink
Update SendingBlueEventListener to correctly send DoubleOpt-In emails (
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn authored Feb 21, 2023
1 parent d519522 commit 6c424ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 102 deletions.
46 changes: 3 additions & 43 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 @@ -84,7 +82,7 @@ public function listSubscribe(FormSavePostEvent $event): void
$email = '';
$firstName = '';
$lastName = '';
$redirectionUrl = $request->getUriForPath('') . '?subscribe=true';
$redirectionUrl = $request->getUriForPath($request->getPathInfo()) . '?send=true&subscribe=true';
$listIdsByMailTemplate = [];

foreach ($form['fields'] as $field) {
Expand Down Expand Up @@ -113,53 +111,15 @@ 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,
],
]);

Expand Down
64 changes: 5 additions & 59 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 @@ -70,20 +69,14 @@ public function testGetSubscribedEvents(): void

public function testlistSubscribeNotExist(): void
{
$this->requestStack->push(Request::create('http://localhost/', 'POST'));
$this->requestStack->push(Request::create('http://localhost/newsletter', '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()) {
$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,20 @@ 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',
'redirectionUrl' => 'http://localhost/newsletter?send=true&subscribe=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

0 comments on commit 6c424ab

Please sign in to comment.