Skip to content

Commit 2814efc

Browse files
committed
Check for duplicated contact email if creation fails due to validation (#86)
1 parent c8e0470 commit 2814efc

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

spec/MessageHandler/Contact/ContactCreateHandlerSpec.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace spec\Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact;
66

7+
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
78
use Tests\Webgriffe\SyliusActiveCampaignPlugin\App\Entity\Customer\CustomerInterface;
89
use InvalidArgumentException;
910
use PhpSpec\ObjectBehavior;
@@ -16,6 +17,7 @@
1617
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaign\ContactInterface;
1718
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaignAwareInterface;
1819
use Webgriffe\SyliusActiveCampaignPlugin\ValueObject\Response\CreateResourceResponseInterface;
20+
use Webgriffe\SyliusActiveCampaignPlugin\ValueObject\Response\ListResourcesResponseInterface;
1921
use Webgriffe\SyliusActiveCampaignPlugin\ValueObject\Response\ResourceResponseInterface;
2022

2123
class ContactCreateHandlerSpec extends ObjectBehavior
@@ -30,6 +32,7 @@ public function let(
3032
$contactMapper->mapFromCustomer($customer)->willReturn($contact);
3133

3234
$customer->getActiveCampaignId()->willReturn(null);
35+
$customer->getEmail()->willReturn('email');
3336

3437
$customerRepository->find(12)->willReturn($customer);
3538

@@ -91,4 +94,24 @@ public function it_creates_contact_on_active_campaign(
9194

9295
$this->__invoke(new ContactCreate(12));
9396
}
97+
98+
public function it_search_for_contact_id_if_validation_fails_due_to_duplicated_email(
99+
ContactInterface $contact,
100+
ActiveCampaignResourceClientInterface $activeCampaignContactClient,
101+
CustomerInterface $customer,
102+
CustomerRepositoryInterface $customerRepository,
103+
ResourceResponseInterface $contactResponse,
104+
ListResourcesResponseInterface $searchContactsForEmail,
105+
): void {
106+
$contactResponse->getId()->willReturn(3423);
107+
$activeCampaignContactClient->create($contact)->shouldBeCalledOnce()->willThrow(new UnprocessableEntityHttpException());
108+
$activeCampaignContactClient->list(['email' => 'email'])->shouldBeCalledOnce()->willReturn($searchContactsForEmail);
109+
110+
$searchContactsForEmail->getResourceResponseLists()->willReturn([$contactResponse]);
111+
112+
$customer->setActiveCampaignId(3423)->shouldBeCalledOnce();
113+
$customerRepository->add($customer)->shouldBeCalledOnce();
114+
115+
$this->__invoke(new ContactCreate(12));
116+
}
94117
}

src/MessageHandler/Contact/ContactCreateHandler.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use InvalidArgumentException;
88
use Sylius\Component\Core\Model\CustomerInterface;
99
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
10+
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
1011
use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface;
1112
use Webgriffe\SyliusActiveCampaignPlugin\Mapper\ContactMapperInterface;
1213
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactCreate;
1314
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaignAwareInterface;
15+
use Webgriffe\SyliusActiveCampaignPlugin\ValueObject\Response\Contact\ContactResponse;
1416

1517
final class ContactCreateHandler
1618
{
@@ -37,8 +39,20 @@ public function __invoke(ContactCreate $message): void
3739
if ($activeCampaignId !== null) {
3840
throw new InvalidArgumentException(sprintf('The Customer with id "%s" has been already created on ActiveCampaign on the contact with id "%s"', $customerId, $activeCampaignId));
3941
}
40-
$createContactResponse = $this->activeCampaignContactClient->create($this->contactMapper->mapFromCustomer($customer));
41-
$customer->setActiveCampaignId($createContactResponse->getResourceResponse()->getId());
42+
try {
43+
$createContactResponse = $this->activeCampaignContactClient->create($this->contactMapper->mapFromCustomer($customer));
44+
$activeCampaignContactId = $createContactResponse->getResourceResponse()->getId();
45+
} catch (UnprocessableEntityHttpException $e) {
46+
// If validation fails try to check if contact already exists
47+
$searchContactsForEmail = $this->activeCampaignContactClient->list(['email' => (string) $customer->getEmail()])->getResourceResponseLists();
48+
if (count($searchContactsForEmail) < 1) {
49+
throw $e;
50+
}
51+
/** @var ContactResponse $contact */
52+
$contact = reset($searchContactsForEmail);
53+
$activeCampaignContactId = $contact->getId();
54+
}
55+
$customer->setActiveCampaignId($activeCampaignContactId);
4256
$this->customerRepository->add($customer);
4357
}
4458
}

tests/Integration/MessageHandler/Contact/ContactCreateHandlerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected function setUp(): void
3434
self::getContainer()->get('webgriffe.sylius_active_campaign_plugin.mapper.contact'),
3535
self::getContainer()->get('webgriffe.sylius_active_campaign_plugin.client_stub.active_campaign.contact'),
3636
$this->customerRepository,
37-
self::getContainer()->get('messenger.default_bus'),
3837
);
3938
}
4039

0 commit comments

Comments
 (0)