Skip to content

Commit 6c424ab

Browse files
authored
Update SendingBlueEventListener to correctly send DoubleOpt-In emails (#351)
1 parent d519522 commit 6c424ab

File tree

2 files changed

+8
-102
lines changed

2 files changed

+8
-102
lines changed

Event/SendinblueListSubscriber.php

+3-43
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313

1414
use GuzzleHttp\ClientInterface;
1515
use SendinBlue\Client\Api\ContactsApi;
16-
use SendinBlue\Client\ApiException;
1716
use SendinBlue\Client\Configuration;
1817
use SendinBlue\Client\Model\CreateDoiContact;
19-
use SendinBlue\Client\Model\UpdateContact;
2018
use Sulu\Bundle\FormBundle\Entity\Dynamic;
2119
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2220
use Symfony\Component\HttpFoundation\RequestStack;
@@ -84,7 +82,7 @@ public function listSubscribe(FormSavePostEvent $event): void
8482
$email = '';
8583
$firstName = '';
8684
$lastName = '';
87-
$redirectionUrl = $request->getUriForPath('') . '?subscribe=true';
85+
$redirectionUrl = $request->getUriForPath($request->getPathInfo()) . '?send=true&subscribe=true';
8886
$listIdsByMailTemplate = [];
8987

9088
foreach ($form['fields'] as $field) {
@@ -113,53 +111,15 @@ public function listSubscribe(FormSavePostEvent $event): void
113111
return;
114112
}
115113

116-
$contact = null;
117-
try {
118-
$contact = $this->contactsApi->getContactInfo($email);
119-
} catch (ApiException $e) {
120-
if (404 !== $e->getCode()) {
121-
throw $e;
122-
}
123-
// Contact does not exist, ignore the exception
124-
}
125-
126-
if (null !== $contact) {
127-
$updateContact = new UpdateContact();
128-
129-
$updateContact->setAttributes(
130-
(object) \array_replace(
131-
(array) $contact->getAttributes(),
132-
[
133-
'FIRST_NAME' => $firstName,
134-
'LAST_NAME' => $lastName,
135-
]
136-
)
137-
);
138-
139-
/** @var int[] $collectedListIds */
140-
$collectedListIds = $contact->getListIds();
141-
foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) {
142-
$collectedListIds = \array_merge($collectedListIds, $listIds);
143-
}
144-
145-
$collectedListIds = \array_unique($collectedListIds);
146-
147-
$updateContact->setListIds($collectedListIds);
148-
149-
$this->contactsApi->updateContact($email, $updateContact);
150-
151-
return;
152-
}
153-
154114
foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) {
155115
$createDoiContact = new CreateDoiContact([
156116
'email' => $email,
157117
'templateId' => $mailTemplateId,
158118
'includeListIds' => $listIds,
159119
'redirectionUrl' => $redirectionUrl,
160120
'attributes' => [
161-
'FIRST_NAME' => $firstName,
162-
'LAST_NAME' => $lastName,
121+
'firstname' => $firstName,
122+
'lastname' => $lastName,
163123
],
164124
]);
165125

Tests/Unit/Event/SendinblueListSubscriberTest.php

+5-59
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Prophecy\Argument;
1818
use Prophecy\Prophecy\ObjectProphecy;
1919
use Psr\Http\Message\RequestInterface;
20-
use SendinBlue\Client\ApiException;
2120
use Sulu\Bundle\FormBundle\Configuration\FormConfiguration;
2221
use Sulu\Bundle\FormBundle\Entity\Dynamic;
2322
use Sulu\Bundle\FormBundle\Entity\Form;
@@ -70,20 +69,14 @@ public function testGetSubscribedEvents(): void
7069

7170
public function testlistSubscribeNotExist(): void
7271
{
73-
$this->requestStack->push(Request::create('http://localhost/', 'POST'));
72+
$this->requestStack->push(Request::create('http://localhost/newsletter', 'POST'));
7473
$event = $this->createFormSavePostEvent();
7574

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

81-
if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()) {
82-
$self->assertSame('GET', $request->getMethod());
83-
84-
throw new ApiException('', 404);
85-
}
86-
8780
if ('https://api.sendinblue.com/v3/contacts/doubleOptinConfirmation' === $request->getUri()->__toString()) {
8881
$self->assertSame('POST', $request->getMethod());
8982

@@ -92,67 +85,20 @@ public function testlistSubscribeNotExist(): void
9285
$self->assertSame([
9386
'email' => '[email protected]',
9487
'attributes' => [
95-
'FIRST_NAME' => 'John',
96-
'LAST_NAME' => 'Doe',
88+
'firstname' => 'John',
89+
'lastname' => 'Doe',
9790
],
9891
'includeListIds' => ['789'],
9992
'templateId' => 456,
100-
'redirectionUrl' => 'http://localhost?subscribe=true',
93+
'redirectionUrl' => 'http://localhost/newsletter?send=true&subscribe=true',
10194
], $json);
10295

10396
return new Response();
10497
}
10598

10699
throw new \RuntimeException('Unexpected request: ' . $request->getUri()->__toString());
107100
})
108-
->shouldBeCalledTimes(2);
109-
110-
// act
111-
$this->sendinblueListSubscriber->listSubscribe($event);
112-
113-
$this->assertTrue(true);
114-
}
115-
116-
public function testlistSubscribeAlreadyExist(): void
117-
{
118-
$this->requestStack->push(Request::create('http://localhost/', 'POST'));
119-
$event = $this->createFormSavePostEvent();
120-
121-
$self = $this;
122-
$this->client->send(Argument::cetera())->will(function($args) use ($self) {
123-
/** @var RequestInterface $request */
124-
$request = $args[0];
125-
126-
if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()
127-
&& 'GET' === $request->getMethod()
128-
) {
129-
return new Response(200, ['Content-Type' => 'application/json'], \json_encode([
130-
'id' => 123,
131-
'email' => '[email protected]',
132-
'attributes' => [],
133-
'listIds' => [],
134-
]));
135-
}
136-
137-
if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()
138-
&& 'PUT' === $request->getMethod()
139-
) {
140-
$json = \json_decode($request->getBody()->getContents(), true);
141-
142-
$self->assertSame([
143-
'attributes' => [
144-
'FIRST_NAME' => 'John',
145-
'LAST_NAME' => 'Doe',
146-
],
147-
'listIds' => ['789'],
148-
], $json);
149-
150-
return new Response();
151-
}
152-
153-
throw new \RuntimeException('Unexpected request (' . $request->getMethod() . '): ' . $request->getUri()->__toString());
154-
})
155-
->shouldBeCalledTimes(2);
101+
->shouldBeCalledOnce();
156102

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

0 commit comments

Comments
 (0)