Skip to content

Commit

Permalink
Remove dispatching tags and lists subscriber message from contact han…
Browse files Browse the repository at this point in the history
…dlers (#53)
  • Loading branch information
lruozzi9 committed Apr 26, 2022
1 parent 9a9ac18 commit 38c25f4
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 42 deletions.
15 changes: 3 additions & 12 deletions spec/MessageHandler/Contact/ContactCreateHandlerSpec.php
Expand Up @@ -7,16 +7,11 @@
use App\Entity\Customer\CustomerInterface;
use InvalidArgumentException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\CustomerInterface as SyliusCustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Mapper\ContactMapperInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactCreate;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactListsSubscriber;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactTagsAdder;
use Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact\ContactCreateHandler;
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaign\ContactInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaignAwareInterface;
Expand All @@ -30,16 +25,15 @@ public function let(
ContactInterface $contact,
CustomerInterface $customer,
ActiveCampaignResourceClientInterface $activeCampaignContactClient,
CustomerRepositoryInterface $customerRepository,
MessageBusInterface $messageBus
CustomerRepositoryInterface $customerRepository
): void {
$contactMapper->mapFromCustomer($customer)->willReturn($contact);

$customer->getActiveCampaignId()->willReturn(null);

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

$this->beConstructedWith($contactMapper, $activeCampaignContactClient, $customerRepository, $messageBus);
$this->beConstructedWith($contactMapper, $activeCampaignContactClient, $customerRepository);
}

public function it_is_initializable(): void
Expand Down Expand Up @@ -87,16 +81,13 @@ public function it_creates_contact_on_active_campaign(
CustomerInterface $customer,
CustomerRepositoryInterface $customerRepository,
CreateResourceResponseInterface $createContactResponse,
ResourceResponseInterface $contactResponse,
MessageBusInterface $messageBus
ResourceResponseInterface $contactResponse
): void {
$contactResponse->getId()->willReturn(3423);
$createContactResponse->getResourceResponse()->willReturn($contactResponse);
$activeCampaignContactClient->create($contact)->shouldBeCalledOnce()->willReturn($createContactResponse);
$customer->setActiveCampaignId(3423)->shouldBeCalledOnce();
$customerRepository->add($customer)->shouldBeCalledOnce();
$messageBus->dispatch(Argument::type(ContactTagsAdder::class))->shouldBeCalledOnce()->willReturn(new Envelope(new ContactTagsAdder(12)));
$messageBus->dispatch(Argument::type(ContactListsSubscriber::class))->shouldBeCalledOnce()->willReturn(new Envelope(new ContactListsSubscriber(12)));

$this->__invoke(new ContactCreate(12));
}
Expand Down
15 changes: 3 additions & 12 deletions spec/MessageHandler/Contact/ContactUpdateHandlerSpec.php
Expand Up @@ -7,15 +7,10 @@
use App\Entity\Customer\CustomerInterface;
use InvalidArgumentException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\CustomerInterface as SyliusCustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Mapper\ContactMapperInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactListsSubscriber;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactTagsAdder;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactUpdate;
use Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact\ContactUpdateHandler;
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaign\ContactInterface;
Expand All @@ -29,16 +24,15 @@ public function let(
ContactInterface $contact,
CustomerInterface $customer,
ActiveCampaignResourceClientInterface $activeCampaignContactClient,
CustomerRepositoryInterface $customerRepository,
MessageBusInterface $messageBus
CustomerRepositoryInterface $customerRepository
): void {
$contactMapper->mapFromCustomer($customer)->willReturn($contact);

$customer->getActiveCampaignId()->willReturn('1234');

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

$this->beConstructedWith($contactMapper, $activeCampaignContactClient, $customerRepository, $messageBus);
$this->beConstructedWith($contactMapper, $activeCampaignContactClient, $customerRepository);
}

public function it_is_initializable(): void
Expand Down Expand Up @@ -94,12 +88,9 @@ public function it_throws_if_customer_has_a_different_id_on_active_campaign_than
public function it_updates_contact_on_active_campaign(
ContactInterface $contact,
ActiveCampaignResourceClientInterface $activeCampaignContactClient,
UpdateResourceResponseInterface $updateContactResponse,
MessageBusInterface $messageBus
UpdateResourceResponseInterface $updateContactResponse
): void {
$activeCampaignContactClient->update(1234, $contact)->shouldBeCalledOnce()->willReturn($updateContactResponse);
$messageBus->dispatch(Argument::type(ContactTagsAdder::class))->shouldBeCalledOnce()->willReturn(new Envelope(new ContactTagsAdder(12)));
$messageBus->dispatch(Argument::type(ContactListsSubscriber::class))->shouldBeCalledOnce()->willReturn(new Envelope(new ContactListsSubscriber(12)));

$this->__invoke(new ContactUpdate(12, 1234));
}
Expand Down
9 changes: 1 addition & 8 deletions src/MessageHandler/Contact/ContactCreateHandler.php
Expand Up @@ -7,21 +7,17 @@
use InvalidArgumentException;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Mapper\ContactMapperInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactCreate;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactListsSubscriber;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactTagsAdder;
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaignAwareInterface;

final class ContactCreateHandler
{
public function __construct(
private ContactMapperInterface $contactMapper,
private ActiveCampaignResourceClientInterface $activeCampaignContactClient,
private CustomerRepositoryInterface $customerRepository,
private MessageBusInterface $messageBus
private CustomerRepositoryInterface $customerRepository
) {
}

Expand All @@ -44,8 +40,5 @@ public function __invoke(ContactCreate $message): void
$createContactResponse = $this->activeCampaignContactClient->create($this->contactMapper->mapFromCustomer($customer));
$customer->setActiveCampaignId($createContactResponse->getResourceResponse()->getId());
$this->customerRepository->add($customer);

$this->messageBus->dispatch(new ContactTagsAdder($customerId));
$this->messageBus->dispatch(new ContactListsSubscriber($customerId));
}
}
9 changes: 1 addition & 8 deletions src/MessageHandler/Contact/ContactUpdateHandler.php
Expand Up @@ -7,11 +7,8 @@
use InvalidArgumentException;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Mapper\ContactMapperInterface;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactListsSubscriber;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactTagsAdder;
use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactUpdate;
use Webgriffe\SyliusActiveCampaignPlugin\Model\ActiveCampaignAwareInterface;

Expand All @@ -20,8 +17,7 @@ final class ContactUpdateHandler
public function __construct(
private ContactMapperInterface $contactMapper,
private ActiveCampaignResourceClientInterface $activeCampaignContactClient,
private CustomerRepositoryInterface $customerRepository,
private MessageBusInterface $messageBus
private CustomerRepositoryInterface $customerRepository
) {
}

Expand All @@ -42,8 +38,5 @@ public function __invoke(ContactUpdate $message): void
throw new InvalidArgumentException(sprintf('The Customer with id "%s" has an ActiveCampaign id that does not match. Expected "%s", given "%s".', $customerId, $message->getActiveCampaignId(), (string) $activeCampaignId));
}
$this->activeCampaignContactClient->update($message->getActiveCampaignId(), $this->contactMapper->mapFromCustomer($customer));

$this->messageBus->dispatch(new ContactTagsAdder($customerId));
$this->messageBus->dispatch(new ContactListsSubscriber($customerId));
}
}
2 changes: 0 additions & 2 deletions src/Resources/config/services/message_handler.xml
Expand Up @@ -7,7 +7,6 @@
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.mapper.contact"/>
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.client.active_campaign.contact"/>
<argument type="service" id="sylius.repository.customer"/>
<argument type="service" id="messenger.default_bus"/>
<tag name="messenger.message_handler"/>
</service>

Expand All @@ -16,7 +15,6 @@
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.mapper.contact"/>
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.client.active_campaign.contact"/>
<argument type="service" id="sylius.repository.customer"/>
<argument type="service" id="messenger.default_bus"/>
<tag name="messenger.message_handler"/>
</service>

Expand Down

0 comments on commit 38c25f4

Please sign in to comment.