diff --git a/Event/SendinblueListSubscriber.php b/Event/SendinblueListSubscriber.php index 359216d0..4864c9fc 100644 --- a/Event/SendinblueListSubscriber.php +++ b/Event/SendinblueListSubscriber.php @@ -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; @@ -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) { @@ -113,44 +111,6 @@ 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, @@ -158,8 +118,8 @@ public function listSubscribe(FormSavePostEvent $event): void 'includeListIds' => $listIds, 'redirectionUrl' => $redirectionUrl, 'attributes' => [ - 'FIRST_NAME' => $firstName, - 'LAST_NAME' => $lastName, + 'firstname' => $firstName, + 'lastname' => $lastName, ], ]); diff --git a/Tests/Unit/Event/SendinblueListSubscriberTest.php b/Tests/Unit/Event/SendinblueListSubscriberTest.php index d4b26dee..6df62e14 100644 --- a/Tests/Unit/Event/SendinblueListSubscriberTest.php +++ b/Tests/Unit/Event/SendinblueListSubscriberTest.php @@ -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; @@ -70,7 +69,7 @@ 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; @@ -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()); @@ -92,12 +85,12 @@ 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(); @@ -105,54 +98,7 @@ public function testlistSubscribeNotExist(): void 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);