diff --git a/config/services/enqueuer.xml b/config/services/enqueuer.xml index 1d7f8a3..1140801 100644 --- a/config/services/enqueuer.xml +++ b/config/services/enqueuer.xml @@ -7,6 +7,7 @@ + + + + + diff --git a/config/services/message_handler.xml b/config/services/message_handler.xml index adcc0e3..8222ebf 100644 --- a/config/services/message_handler.xml +++ b/config/services/message_handler.xml @@ -7,6 +7,7 @@ + @@ -15,12 +16,14 @@ + + @@ -61,6 +64,7 @@ + @@ -69,12 +73,14 @@ + + @@ -86,6 +92,7 @@ + @@ -95,12 +102,14 @@ + + @@ -109,6 +118,7 @@ + @@ -117,12 +127,14 @@ + + @@ -132,6 +144,7 @@ + diff --git a/src/Enqueuer/ConnectionEnqueuer.php b/src/Enqueuer/ConnectionEnqueuer.php index cc11427..e517bdd 100644 --- a/src/Enqueuer/ConnectionEnqueuer.php +++ b/src/Enqueuer/ConnectionEnqueuer.php @@ -5,6 +5,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\Enqueuer; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\MessageBusInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\Connection\ConnectionCreate; @@ -18,7 +19,15 @@ public function __construct( private MessageBusInterface $messageBus, private ActiveCampaignResourceClientInterface $activeCampaignConnectionClient, private EntityManagerInterface $entityManager, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function enqueue($channel): void @@ -26,8 +35,17 @@ public function enqueue($channel): void /** @var string|int|null $channelId */ $channelId = $channel->getId(); Assert::notNull($channelId, 'The channel id should not be null'); + $this->logger?->debug(sprintf( + 'Starting enqueuing connection for channel "%s".', + $channelId, + )); $activeCampaignConnectionId = $channel->getActiveCampaignId(); if ($activeCampaignConnectionId !== null) { + $this->logger?->debug(sprintf( + 'Channel "%s" has an already valued ActiveCampaign id "%s", so we have to update the connection.', + $channelId, + $activeCampaignConnectionId, + )); $this->messageBus->dispatch(new ConnectionUpdate($channelId, $activeCampaignConnectionId)); return; @@ -44,11 +62,20 @@ public function enqueue($channel): void $activeCampaignConnectionId = $connection->getId(); $channel->setActiveCampaignId($activeCampaignConnectionId); $this->entityManager->flush(); + $this->logger?->debug(sprintf( + 'Found an ActiveCampaign connection with id "%s" for given channel "%s", the id has been stored and we have to update the connection.', + $activeCampaignConnectionId, + $channelId, + )); $this->messageBus->dispatch(new ConnectionUpdate($channelId, $activeCampaignConnectionId)); return; } + $this->logger?->debug(sprintf( + 'No connection found for given channel "%s", we have to create the connection.', + $channelId, + )); $this->messageBus->dispatch(new ConnectionCreate($channelId)); } diff --git a/src/Enqueuer/ContactEnqueuer.php b/src/Enqueuer/ContactEnqueuer.php index 0a0743e..58e3070 100644 --- a/src/Enqueuer/ContactEnqueuer.php +++ b/src/Enqueuer/ContactEnqueuer.php @@ -5,6 +5,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\Enqueuer; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\MessageBusInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactCreate; @@ -18,7 +19,15 @@ public function __construct( private MessageBusInterface $messageBus, private ActiveCampaignResourceClientInterface $activeCampaignContactClient, private EntityManagerInterface $entityManager, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function enqueue($customer): void @@ -26,8 +35,17 @@ public function enqueue($customer): void /** @var string|int|null $customerId */ $customerId = $customer->getId(); Assert::notNull($customerId, 'The customer id should not be null'); + $this->logger?->debug(sprintf( + 'Starting enqueuing contact for customer "%s".', + $customerId, + )); $activeCampaignContactId = $customer->getActiveCampaignId(); if ($activeCampaignContactId !== null) { + $this->logger?->debug(sprintf( + 'Customer "%s" has an already valued ActiveCampaign id "%s", so we have to update the contact.', + $customerId, + $activeCampaignContactId, + )); $this->messageBus->dispatch(new ContactUpdate($customerId, $activeCampaignContactId)); return; @@ -41,11 +59,20 @@ public function enqueue($customer): void $activeCampaignContactId = $contact->getId(); $customer->setActiveCampaignId($activeCampaignContactId); $this->entityManager->flush(); + $this->logger?->debug(sprintf( + 'Customer "%s" has an already valued ActiveCampaign id "%s", so we have to update the contact.', + $customerId, + $activeCampaignContactId, + )); $this->messageBus->dispatch(new ContactUpdate($customerId, $activeCampaignContactId)); return; } + $this->logger?->debug(sprintf( + 'No contact found for given customer "%s", we have to create the contact.', + $customerId, + )); $this->messageBus->dispatch(new ContactCreate($customerId)); } diff --git a/src/Enqueuer/EcommerceCustomerEnqueuer.php b/src/Enqueuer/EcommerceCustomerEnqueuer.php index a69370a..d82250b 100644 --- a/src/Enqueuer/EcommerceCustomerEnqueuer.php +++ b/src/Enqueuer/EcommerceCustomerEnqueuer.php @@ -5,6 +5,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\Enqueuer; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Symfony\Component\Messenger\MessageBusInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -24,7 +25,15 @@ public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignEcommerceCustomerClient, private EntityManagerInterface $entityManager, private FactoryInterface $channelCustomerFactory, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function enqueue($customer, $channel): void @@ -35,9 +44,20 @@ public function enqueue($customer, $channel): void /** @var string|int|null $channelId */ $channelId = $channel->getId(); Assert::notNull($channelId, 'The channel id should not be null'); + $this->logger?->debug(sprintf( + 'Starting enqueuing ecommerce customer for customer "%s" and channel "%s".', + $customerId, + $channelId, + )); $channelCustomer = $customer->getChannelCustomerByChannel($channel); if ($channelCustomer !== null) { + $this->logger?->debug(sprintf( + 'Customer "%s" has an already valued ActiveCampaign id "%s" for channel "%s", so we have to update the ecommerce customer.', + $customerId, + $channelCustomer->getActiveCampaignId(), + $channelId, + )); $this->messageBus->dispatch(new EcommerceCustomerUpdate($customerId, $channelCustomer->getActiveCampaignId(), $channelId)); return; @@ -63,11 +83,22 @@ public function enqueue($customer, $channel): void $this->entityManager->persist($channelCustomer); $customer->addChannelCustomer($channelCustomer); $this->entityManager->flush(); + $this->logger?->debug(sprintf( + 'Found an ActiveCampaign ecommerce customer with id "%s" for given customer "%s" and channel "%s", the id has been stored and we have to update the ecommerce customer.', + $activeCampaignEcommerceCustomerId, + $customerId, + $channelId, + )); $this->messageBus->dispatch(new EcommerceCustomerUpdate($customerId, $activeCampaignEcommerceCustomerId, $channelId)); return; } + $this->logger?->debug(sprintf( + 'No ecommerce customer found for given customer "%s" and channel "%s", we have to create the ecommerce customer.', + $customerId, + $channelId, + )); $this->messageBus->dispatch(new EcommerceCustomerCreate($customerId, $channelId)); } diff --git a/src/Enqueuer/EcommerceOrderEnqueuer.php b/src/Enqueuer/EcommerceOrderEnqueuer.php index a2f063d..3469634 100644 --- a/src/Enqueuer/EcommerceOrderEnqueuer.php +++ b/src/Enqueuer/EcommerceOrderEnqueuer.php @@ -5,6 +5,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\Enqueuer; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\OrderInterface; use Symfony\Component\Messenger\MessageBusInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -21,7 +22,15 @@ public function __construct( private MessageBusInterface $messageBus, private EntityManagerInterface $entityManager, private ActiveCampaignResourceClientInterface $activeCampaignEcommerceOrderClient, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function enqueue($order, bool $isInRealTime = true): void @@ -29,15 +38,29 @@ public function enqueue($order, bool $isInRealTime = true): void /** @var string|int|null $orderId */ $orderId = $order->getId(); Assert::notNull($orderId, 'The order id should not be null'); + $this->logger?->debug(sprintf( + 'Starting enqueuing ecommerce order for order "%s".', + $orderId, + )); $activeCampaignEcommerceOrderId = $order->getActiveCampaignId(); if ($activeCampaignEcommerceOrderId !== null) { if ($order->getState() === OrderInterface::STATE_CANCELLED) { + $this->logger?->debug(sprintf( + 'Order "%s" has been cancelled, so we have to remove the ecommerce order "%s".', + $orderId, + $activeCampaignEcommerceOrderId, + )); $this->messageBus->dispatch(new EcommerceOrderRemove($activeCampaignEcommerceOrderId)); $order->setActiveCampaignId(null); $this->entityManager->flush(); return; } + $this->logger?->debug(sprintf( + 'Order "%s" has an already valued ActiveCampaign id "%s", so we have to update the ecommerce order.', + $orderId, + $activeCampaignEcommerceOrderId, + )); $this->messageBus->dispatch(new EcommerceOrderUpdate($orderId, $activeCampaignEcommerceOrderId, $isInRealTime)); return; @@ -57,10 +80,20 @@ public function enqueue($order, bool $isInRealTime = true): void $activeCampaignEcommerceOrderId = $ecommerceOrder->getId(); if ($order->getState() === OrderInterface::STATE_CANCELLED) { + $this->logger?->debug(sprintf( + 'Found an ActiveCampaign ecommerce order with id "%s" for given canceled order "%s", we have to remove the ecommerce order.', + $activeCampaignEcommerceOrderId, + $orderId, + )); $this->messageBus->dispatch(new EcommerceOrderRemove($activeCampaignEcommerceOrderId)); return; } + $this->logger?->debug(sprintf( + 'Found an ActiveCampaign ecommerce order with id "%s" for given order "%s", the id has been stored and we have to update the ecommerce order.', + $activeCampaignEcommerceOrderId, + $orderId, + )); $order->setActiveCampaignId($activeCampaignEcommerceOrderId); $this->entityManager->flush(); @@ -69,8 +102,17 @@ public function enqueue($order, bool $isInRealTime = true): void return; } if ($order->getState() === OrderInterface::STATE_CANCELLED) { + $this->logger?->debug(sprintf( + 'No ecommerce order found for given canceled order "%s", we can avoid creating the ecommerce order.', + $orderId, + )); + return; } + $this->logger?->debug(sprintf( + 'No ecommerce order found for given order "%s", we have to create the ecommerce order.', + $orderId, + )); $this->messageBus->dispatch(new EcommerceOrderCreate($orderId, $isInRealTime)); } diff --git a/src/Enqueuer/WebhookEnqueuer.php b/src/Enqueuer/WebhookEnqueuer.php index 9290464..90e0a67 100644 --- a/src/Enqueuer/WebhookEnqueuer.php +++ b/src/Enqueuer/WebhookEnqueuer.php @@ -4,6 +4,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\Enqueuer; +use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\MessageBusInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface; @@ -16,7 +17,15 @@ public function __construct( private MessageBusInterface $messageBus, private ActiveCampaignResourceClientInterface $activeCampaignWebhookClient, private ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function enqueue($channel): void @@ -24,6 +33,10 @@ public function enqueue($channel): void /** @var string|int|null $channelId */ $channelId = $channel->getId(); Assert::notNull($channelId, 'The channel id should not be null.'); + $this->logger?->debug(sprintf( + 'Starting enqueuing webhook for channel "%s".', + $channelId, + )); $activeCampaignListId = $channel->getActiveCampaignListId(); Assert::notNull($activeCampaignListId, 'The channel ActiveCampaign list id should not be null.'); $searchWebhooks = $this->activeCampaignWebhookClient->list([ @@ -31,8 +44,19 @@ public function enqueue($channel): void 'filters[listid]' => (string) $activeCampaignListId, ])->getResourceResponseLists(); if (count($searchWebhooks) > 0) { + $this->logger?->debug(sprintf( + 'Channel "%s" has an already valued ActiveCampaign webhook for list "%s", so we can skip the webhook creation.', + $channelId, + $activeCampaignListId, + )); + return; } + $this->logger?->debug(sprintf( + 'Channel "%s" has no ActiveCampaign webhook for list "%s", so we have to create it.', + $channelId, + $activeCampaignListId, + )); $this->messageBus->dispatch(new WebhookCreate($channelId)); } diff --git a/src/EventSubscriber/ChannelSubscriber.php b/src/EventSubscriber/ChannelSubscriber.php index 06a6a93..4c45462 100644 --- a/src/EventSubscriber/ChannelSubscriber.php +++ b/src/EventSubscriber/ChannelSubscriber.php @@ -38,6 +38,10 @@ public function enqueueChannel(GenericEvent $event): void if (!$channel instanceof ChannelInterface || !$channel instanceof ActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked connection enqueuing for channel "%s".', + (string) $channel->getId(), + )); try { $this->connectionEnqueuer->enqueue($channel); @@ -49,9 +53,13 @@ public function enqueueChannel(GenericEvent $event): void public function removeChannel(GenericEvent $event): void { $channel = $event->getSubject(); - if (!$channel instanceof ActiveCampaignAwareInterface) { + if (!$channel instanceof ChannelInterface || !$channel instanceof ActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked connection removal for channel "%s".', + (string) $channel->getId(), + )); $activeCampaignId = $channel->getActiveCampaignId(); if ($activeCampaignId === null) { return; diff --git a/src/EventSubscriber/CustomerSubscriber.php b/src/EventSubscriber/CustomerSubscriber.php index e5f6de2..908d701 100644 --- a/src/EventSubscriber/CustomerSubscriber.php +++ b/src/EventSubscriber/CustomerSubscriber.php @@ -47,6 +47,10 @@ public function enqueueContact(GenericEvent $event): void if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked contact enqueuing for customer "%s".', + (string) $customer->getId(), + )); try { $this->contactEnqueuer->enqueue($customer); @@ -61,6 +65,10 @@ public function enqueueEcommerceCustomer(GenericEvent $event): void if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked ecommerce customer enqueuing for customer "%s".', + (string) $customer->getId(), + )); foreach ($this->customerChannelsResolver->resolve($customer) as $channel) { if (!$channel instanceof ActiveCampaignAwareInterface) { return; @@ -85,6 +93,10 @@ public function addContactTags(GenericEvent $event): void if ($customerId === null) { return; } + $this->logger->debug(sprintf( + 'Invoked adding contact tags for customer "%s".', + $customerId, + )); try { $this->messageBus->dispatch(new ContactTagsAdder($customerId)); @@ -104,6 +116,10 @@ public function subscribeContactToLists(GenericEvent $event): void if ($customerId === null) { return; } + $this->logger->debug(sprintf( + 'Invoked subscribing contact to lists for customer "%s".', + $customerId, + )); try { $this->messageBus->dispatch(new ContactListsSubscriber($customerId)); @@ -115,9 +131,13 @@ public function subscribeContactToLists(GenericEvent $event): void public function removeContact(GenericEvent $event): void { $customer = $event->getSubject(); - if (!$customer instanceof ActiveCampaignAwareInterface) { + if (!$customer instanceof CustomerInterface || !$customer instanceof ActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked remove contact for customer "%s".', + (string) $customer->getId(), + )); $activeCampaignId = $customer->getActiveCampaignId(); if ($activeCampaignId === null) { return; @@ -133,9 +153,13 @@ public function removeContact(GenericEvent $event): void public function removeEcommerceCustomer(GenericEvent $event): void { $customer = $event->getSubject(); - if (!$customer instanceof CustomerActiveCampaignAwareInterface) { + if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked remove ecommerce customer for customer "%s".', + (string) $customer->getId(), + )); $activeCampaignId = $customer->getActiveCampaignId(); if ($activeCampaignId === null) { return; diff --git a/src/EventSubscriber/OrderSubscriber.php b/src/EventSubscriber/OrderSubscriber.php index c43d92d..ebe6119 100644 --- a/src/EventSubscriber/OrderSubscriber.php +++ b/src/EventSubscriber/OrderSubscriber.php @@ -46,10 +46,14 @@ public static function getSubscribedEvents(): array public function enqueueContact(GenericEvent $event): void { $order = $event->getSubject(); - if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface || $order->getCustomer() === null) { + if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface || null === $customer = $order->getCustomer()) { return; } - $customer = $order->getCustomer(); + $this->logger->debug(sprintf( + 'Invoked contact enqueuing for customer "%s" by order "%s".', + (string) $customer->getId(), + (string) $order->getId(), + )); if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } @@ -64,10 +68,14 @@ public function enqueueContact(GenericEvent $event): void public function enqueueEcommerceCustomer(GenericEvent $event): void { $order = $event->getSubject(); - if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface || $order->getCustomer() === null) { + if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface || null === $customer = $order->getCustomer()) { return; } - $customer = $order->getCustomer(); + $this->logger->debug(sprintf( + 'Invoked ecommerce customer enqueuing for customer "%s" by order "%s".', + (string) $customer->getId(), + (string) $order->getId(), + )); if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } @@ -94,6 +102,11 @@ public function addContactTags(GenericEvent $event): void if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked adding contact tags for customer "%s" by order "%s".', + (string) $customer->getId(), + (string) $order->getId(), + )); /** @var int|string|null $customerId */ $customerId = $customer->getId(); if ($customerId === null) { @@ -117,6 +130,11 @@ public function subscribeContactToLists(GenericEvent $event): void if (!$customer instanceof CustomerInterface || !$customer instanceof CustomerActiveCampaignAwareInterface) { return; } + $this->logger->debug(sprintf( + 'Invoked subscribing contact to lists for customer "%s" by order "%s".', + (string) $customer->getId(), + (string) $order->getId(), + )); /** @var int|string|null $customerId */ $customerId = $customer->getId(); if ($customerId === null) { @@ -146,6 +164,10 @@ private function enqueueOrder(GenericEvent $event, bool $isInRealTime): void if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface || $order->getCustomer() === null) { return; } + $this->logger->debug(sprintf( + 'Invoked ecommerce order enqueuing for order "%s".', + (string) $order->getId(), + ), ['is_in_real_time' => $isInRealTime]); try { $this->ecommerceOrderEnqueuer->enqueue($order, $isInRealTime); @@ -156,14 +178,18 @@ private function enqueueOrder(GenericEvent $event, bool $isInRealTime): void public function removeOrder(GenericEvent $event): void { - $customer = $event->getSubject(); - if (!$customer instanceof ActiveCampaignAwareInterface) { + $order = $event->getSubject(); + if (!$order instanceof OrderInterface || !$order instanceof ActiveCampaignAwareInterface) { return; } - $activeCampaignId = $customer->getActiveCampaignId(); + $activeCampaignId = $order->getActiveCampaignId(); if ($activeCampaignId === null) { return; } + $this->logger->debug(sprintf( + 'Invoked remove ecommerce order for order "%s".', + (string) $order->getId(), + )); try { $this->messageBus->dispatch(new EcommerceOrderRemove($activeCampaignId)); diff --git a/src/MessageHandler/Connection/ConnectionCreateHandler.php b/src/MessageHandler/Connection/ConnectionCreateHandler.php index 2e7e3a9..d760638 100644 --- a/src/MessageHandler/Connection/ConnectionCreateHandler.php +++ b/src/MessageHandler/Connection/ConnectionCreateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Connection; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -18,9 +20,22 @@ public function __construct( private ConnectionMapperInterface $connectionMapper, private ActiveCampaignResourceClientInterface $activeCampaignConnectionClient, private ChannelRepositoryInterface $channelRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(ConnectionCreate $message): void { $channelId = $message->getChannelId(); @@ -37,7 +52,13 @@ public function __invoke(ConnectionCreate $message): void if ($activeCampaignId !== null) { throw new InvalidArgumentException(sprintf('The Channel with id "%s" has been already created on ActiveCampaign on the connection with id "%s"', $channelId, $activeCampaignId)); } - $createConnectionResponse = $this->activeCampaignConnectionClient->create($this->connectionMapper->mapFromChannel($channel)); + try{ + $createConnectionResponse = $this->activeCampaignConnectionClient->create($this->connectionMapper->mapFromChannel($channel)); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } $channel->setActiveCampaignId($createConnectionResponse->getResourceResponse()->getId()); $this->channelRepository->add($channel); } diff --git a/src/MessageHandler/Connection/ConnectionRemoveHandler.php b/src/MessageHandler/Connection/ConnectionRemoveHandler.php index 2eb5c19..6906941 100644 --- a/src/MessageHandler/Connection/ConnectionRemoveHandler.php +++ b/src/MessageHandler/Connection/ConnectionRemoveHandler.php @@ -4,6 +4,8 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Connection; +use GuzzleHttp\Exception\GuzzleException; +use Psr\Log\LoggerInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\Connection\ConnectionRemove; @@ -11,11 +13,30 @@ final class ConnectionRemoveHandler { public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignConnectionClient, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(ConnectionRemove $message): void { - $this->activeCampaignConnectionClient->remove($message->getActiveCampaignId()); + try { + $this->activeCampaignConnectionClient->remove($message->getActiveCampaignId()); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/Connection/ConnectionUpdateHandler.php b/src/MessageHandler/Connection/ConnectionUpdateHandler.php index 4c744a7..60f9c6b 100644 --- a/src/MessageHandler/Connection/ConnectionUpdateHandler.php +++ b/src/MessageHandler/Connection/ConnectionUpdateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Connection; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -18,9 +20,22 @@ public function __construct( private ConnectionMapperInterface $connectionMapper, private ActiveCampaignResourceClientInterface $activeCampaignConnectionClient, private ChannelRepositoryInterface $channelRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(ConnectionUpdate $message): void { $channelId = $message->getChannelId(); @@ -37,6 +52,12 @@ public function __invoke(ConnectionUpdate $message): void if ($activeCampaignId !== $message->getActiveCampaignId()) { throw new InvalidArgumentException(sprintf('The Channel with id "%s" has an ActiveCampaign id that does not match. Expected "%s", given "%s".', $channelId, $message->getActiveCampaignId(), (string) $activeCampaignId)); } - $this->activeCampaignConnectionClient->update($message->getActiveCampaignId(), $this->connectionMapper->mapFromChannel($channel)); + try { + $this->activeCampaignConnectionClient->update($message->getActiveCampaignId(), $this->connectionMapper->mapFromChannel($channel)); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/Contact/ContactCreateHandler.php b/src/MessageHandler/Contact/ContactCreateHandler.php index 245793a..6ab08e8 100644 --- a/src/MessageHandler/Contact/ContactCreateHandler.php +++ b/src/MessageHandler/Contact/ContactCreateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; @@ -20,9 +22,22 @@ public function __construct( private ContactMapperInterface $contactMapper, private ActiveCampaignResourceClientInterface $activeCampaignContactClient, private CustomerRepositoryInterface $customerRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(ContactCreate $message): void { $customerId = $message->getCustomerId(); @@ -51,6 +66,15 @@ public function __invoke(ContactCreate $message): void /** @var ContactResponse $contact */ $contact = reset($searchContactsForEmail); $activeCampaignContactId = $contact->getId(); + $this->logger?->warning(sprintf( + 'Contact with email "%s" already exists on ActiveCampaign with id "%s". Why it has not been found before?', + (string) $customer->getEmail(), + $activeCampaignContactId, + )); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; } $customer->setActiveCampaignId($activeCampaignContactId); $this->customerRepository->add($customer); diff --git a/src/MessageHandler/Contact/ContactListsSubscriberHandler.php b/src/MessageHandler/Contact/ContactListsSubscriberHandler.php index 51a09c4..55b6c7f 100644 --- a/src/MessageHandler/Contact/ContactListsSubscriberHandler.php +++ b/src/MessageHandler/Contact/ContactListsSubscriberHandler.php @@ -4,6 +4,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\CustomerInterface; @@ -30,6 +31,11 @@ public function __construct( ) { } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(ContactListsSubscriber $message): void { $customerId = $message->getCustomerId(); @@ -59,7 +65,12 @@ public function __invoke(ContactListsSubscriber $message): void try { $listSubscriptionStatus = $this->listSubscriptionStatusResolver->resolve($customer, $channel); } catch (ListSubscriptionStatusResolverExceptionInterface $exception) { - $this->logger->info(sprintf('Unable to resolve for the customer "%s" the subscription status for the list "%s" of channel "%s".', (string) $customer->getEmail(), $activeCampaignListId, (string) $channel->getCode())); + $this->logger->info(sprintf( + 'Unable to resolve for the customer "%s" the subscription status for the list "%s" of channel "%s".', + (string) $customer->getEmail(), + $activeCampaignListId, + (string) $channel->getCode(), + )); continue; } @@ -77,6 +88,10 @@ public function __invoke(ContactListsSubscriber $message): void $this->logger->info(sprintf('The association with the list with id "%s" already exists for the contact with id "%s".', $activeCampaignListId, $activeCampaignContactId)); continue; + } catch (\Throwable $e) { + $this->logger->error($e->getMessage(), $e->getTrace()); + + throw $e; } } } diff --git a/src/MessageHandler/Contact/ContactRemoveHandler.php b/src/MessageHandler/Contact/ContactRemoveHandler.php index 47fdbd6..a01ae62 100644 --- a/src/MessageHandler/Contact/ContactRemoveHandler.php +++ b/src/MessageHandler/Contact/ContactRemoveHandler.php @@ -4,6 +4,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact; +use Psr\Log\LoggerInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\Contact\ContactRemove; @@ -11,11 +12,23 @@ final class ContactRemoveHandler { public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignContactClient, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } public function __invoke(ContactRemove $message): void { - $this->activeCampaignContactClient->remove($message->getActiveCampaignId()); + try { + $this->activeCampaignContactClient->remove($message->getActiveCampaignId()); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + } } } diff --git a/src/MessageHandler/Contact/ContactTagsAdderHandler.php b/src/MessageHandler/Contact/ContactTagsAdderHandler.php index cde5d86..9e7e11f 100644 --- a/src/MessageHandler/Contact/ContactTagsAdderHandler.php +++ b/src/MessageHandler/Contact/ContactTagsAdderHandler.php @@ -4,6 +4,7 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\CustomerInterface; @@ -30,6 +31,11 @@ public function __construct( ) { } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(ContactTagsAdder $message): void { $customerId = $message->getCustomerId(); @@ -62,6 +68,10 @@ public function __invoke(ContactTagsAdder $message): void $this->logger->info(sprintf('The tag with id "%s" already exists for the contact with id "%s".', $activeCampaignTagId, $activeCampaignContactId)); continue; + } catch (\Throwable $e) { + $this->logger->error($e->getMessage(), $e->getTrace()); + + throw $e; } } } diff --git a/src/MessageHandler/Contact/ContactUpdateHandler.php b/src/MessageHandler/Contact/ContactUpdateHandler.php index bc5ec4f..0472616 100644 --- a/src/MessageHandler/Contact/ContactUpdateHandler.php +++ b/src/MessageHandler/Contact/ContactUpdateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Contact; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Repository\CustomerRepositoryInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -18,9 +20,22 @@ public function __construct( private ContactMapperInterface $contactMapper, private ActiveCampaignResourceClientInterface $activeCampaignContactClient, private CustomerRepositoryInterface $customerRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(ContactUpdate $message): void { $customerId = $message->getCustomerId(); @@ -37,6 +52,12 @@ public function __invoke(ContactUpdate $message): void if ($activeCampaignId !== $message->getActiveCampaignId()) { 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)); + try{ + $this->activeCampaignContactClient->update($message->getActiveCampaignId(), $this->contactMapper->mapFromCustomer($customer)); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerCreateHandler.php b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerCreateHandler.php index f56f09f..4fc8319 100644 --- a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerCreateHandler.php +++ b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerCreateHandler.php @@ -5,7 +5,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceCustomer; use Doctrine\ORM\EntityManagerInterface; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; @@ -30,9 +32,22 @@ public function __construct( private ChannelRepositoryInterface $channelRepository, private FactoryInterface $channelCustomerFactory, private EntityManagerInterface $entityManager, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceCustomerCreate $message): void { $channelId = $message->getChannelId(); @@ -61,7 +76,13 @@ public function __invoke(EcommerceCustomerCreate $message): void throw new InvalidArgumentException(sprintf('The Customer with id "%s" has been already created on ActiveCampaign on the EcommerceCustomer with id "%s"', $customerId, $activeCampaignId)); } - $response = $this->activeCampaignClient->create($this->ecommerceCustomerMapper->mapFromCustomerAndChannel($customer, $channel)); + try { + $response = $this->activeCampaignClient->create($this->ecommerceCustomerMapper->mapFromCustomerAndChannel($customer, $channel)); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } $channelCustomer = $this->channelCustomerFactory->createNew(); $channelCustomer->setCustomer($customer); $channelCustomer->setActiveCampaignId($response->getResourceResponse()->getId()); diff --git a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerRemoveHandler.php b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerRemoveHandler.php index fd3a365..474a216 100644 --- a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerRemoveHandler.php +++ b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerRemoveHandler.php @@ -4,6 +4,8 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceCustomer; +use GuzzleHttp\Exception\GuzzleException; +use Psr\Log\LoggerInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\EcommerceCustomer\EcommerceCustomerRemove; @@ -11,11 +13,30 @@ final class EcommerceCustomerRemoveHandler { public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignContactClient, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceCustomerRemove $message): void { - $this->activeCampaignContactClient->remove($message->getActiveCampaignId()); + try { + $this->activeCampaignContactClient->remove($message->getActiveCampaignId()); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerUpdateHandler.php b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerUpdateHandler.php index 0ec25f6..4b4e3bd 100644 --- a/src/MessageHandler/EcommerceCustomer/EcommerceCustomerUpdateHandler.php +++ b/src/MessageHandler/EcommerceCustomer/EcommerceCustomerUpdateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceCustomer; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; @@ -22,9 +24,22 @@ public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignClient, private CustomerRepositoryInterface $customerRepository, private ChannelRepositoryInterface $channelRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceCustomerUpdate $message): void { $channelId = $message->getChannelId(); @@ -55,6 +70,12 @@ public function __invoke(EcommerceCustomerUpdate $message): void if ($activeCampaignId !== $message->getActiveCampaignId()) { 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->activeCampaignClient->update($message->getActiveCampaignId(), $this->ecommerceCustomerMapper->mapFromCustomerAndChannel($customer, $channel)); + try { + $this->activeCampaignClient->update($message->getActiveCampaignId(), $this->ecommerceCustomerMapper->mapFromCustomerAndChannel($customer, $channel)); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/EcommerceOrder/EcommerceOrderCreateHandler.php b/src/MessageHandler/EcommerceOrder/EcommerceOrderCreateHandler.php index 54078b0..6770ce2 100644 --- a/src/MessageHandler/EcommerceOrder/EcommerceOrderCreateHandler.php +++ b/src/MessageHandler/EcommerceOrder/EcommerceOrderCreateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceOrder; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -18,9 +20,22 @@ public function __construct( private EcommerceOrderMapperInterface $ecommerceOrderMapper, private ActiveCampaignResourceClientInterface $activeCampaignEcommerceOrderClient, private OrderRepositoryInterface $orderRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceOrderCreate $message): void { $orderId = $message->getOrderId(); @@ -37,7 +52,13 @@ public function __invoke(EcommerceOrderCreate $message): void if ($activeCampaignId !== null) { throw new InvalidArgumentException(sprintf('The Order with id "%s" has been already created on ActiveCampaign on the ecommerce order with id "%s"', $orderId, $activeCampaignId)); } - $response = $this->activeCampaignEcommerceOrderClient->create($this->ecommerceOrderMapper->mapFromOrder($order, $message->isInRealTime())); + try { + $response = $this->activeCampaignEcommerceOrderClient->create($this->ecommerceOrderMapper->mapFromOrder($order, $message->isInRealTime())); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } $order->setActiveCampaignId($response->getResourceResponse()->getId()); $this->orderRepository->add($order); } diff --git a/src/MessageHandler/EcommerceOrder/EcommerceOrderRemoveHandler.php b/src/MessageHandler/EcommerceOrder/EcommerceOrderRemoveHandler.php index 52cb8b5..3d3fb33 100644 --- a/src/MessageHandler/EcommerceOrder/EcommerceOrderRemoveHandler.php +++ b/src/MessageHandler/EcommerceOrder/EcommerceOrderRemoveHandler.php @@ -4,6 +4,8 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceOrder; +use GuzzleHttp\Exception\GuzzleException; +use Psr\Log\LoggerInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; use Webgriffe\SyliusActiveCampaignPlugin\Message\EcommerceOrder\EcommerceOrderRemove; @@ -11,11 +13,30 @@ final class EcommerceOrderRemoveHandler { public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignConnectionClient, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceOrderRemove $message): void { - $this->activeCampaignConnectionClient->remove($message->getActiveCampaignId()); + try { + $this->activeCampaignConnectionClient->remove($message->getActiveCampaignId()); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/EcommerceOrder/EcommerceOrderUpdateHandler.php b/src/MessageHandler/EcommerceOrder/EcommerceOrderUpdateHandler.php index e4c6e20..9c37b9f 100644 --- a/src/MessageHandler/EcommerceOrder/EcommerceOrderUpdateHandler.php +++ b/src/MessageHandler/EcommerceOrder/EcommerceOrderUpdateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\EcommerceOrder; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -18,9 +20,22 @@ public function __construct( private EcommerceOrderMapperInterface $ecommerceOrderMapper, private ActiveCampaignResourceClientInterface $activeCampaignEcommerceOrderClient, private OrderRepositoryInterface $orderRepository, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws GuzzleException + * @throws \Throwable + * @throws \JsonException + */ public function __invoke(EcommerceOrderUpdate $message): void { $orderId = $message->getOrderId(); @@ -37,6 +52,12 @@ public function __invoke(EcommerceOrderUpdate $message): void if ($activeCampaignId !== $message->getActiveCampaignId()) { throw new InvalidArgumentException(sprintf('The Order with id "%s" has an ActiveCampaign id that does not match. Expected "%s", given "%s".', $orderId, $message->getActiveCampaignId(), (string) $activeCampaignId)); } - $this->activeCampaignEcommerceOrderClient->update($message->getActiveCampaignId(), $this->ecommerceOrderMapper->mapFromOrder($order, $message->isInRealTime())); + try { + $this->activeCampaignEcommerceOrderClient->update($message->getActiveCampaignId(), $this->ecommerceOrderMapper->mapFromOrder($order, $message->isInRealTime())); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/src/MessageHandler/Webhook/WebhookCreateHandler.php b/src/MessageHandler/Webhook/WebhookCreateHandler.php index cd23073..d90c0ec 100644 --- a/src/MessageHandler/Webhook/WebhookCreateHandler.php +++ b/src/MessageHandler/Webhook/WebhookCreateHandler.php @@ -4,7 +4,9 @@ namespace Webgriffe\SyliusActiveCampaignPlugin\MessageHandler\Webhook; +use GuzzleHttp\Exception\GuzzleException; use InvalidArgumentException; +use Psr\Log\LoggerInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Webgriffe\SyliusActiveCampaignPlugin\Client\ActiveCampaignResourceClientInterface; @@ -20,9 +22,22 @@ public function __construct( private ActiveCampaignResourceClientInterface $activeCampaignWebhookClient, private ChannelRepositoryInterface $channelRepository, private ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator, + private ?LoggerInterface $logger = null, ) { + if ($this->logger === null) { + trigger_deprecation( + 'webgriffe/sylius-active-campaign-plugin', + 'v0.12.2', + 'The logger argument is mandatory.', + ); + } } + /** + * @throws \Throwable + * @throws GuzzleException + * @throws \JsonException + */ public function __invoke(WebhookCreate $message): void { $channelId = $message->getChannelId(); @@ -38,12 +53,18 @@ public function __invoke(WebhookCreate $message): void if ($activeCampaignListId === null) { throw new InvalidArgumentException(sprintf('The Channel with id "%s" does not have an ActiveCampaign list id.', $channelId)); } - $this->activeCampaignWebhookClient->create($this->webhookMapper->map( - sprintf('Update Sylius newsletter subscription to list "%s"', $activeCampaignListId), - $this->channelHostnameUrlGenerator->generateForRoute($channel, 'webgriffe_sylius_active_campaign_list_status_webhook'), - ['subscribe', 'unsubscribe'], - ['public', 'admin', 'system'], - $activeCampaignListId, - )); + try { + $this->activeCampaignWebhookClient->create($this->webhookMapper->map( + sprintf('Update Sylius newsletter subscription to list "%s"', $activeCampaignListId), + $this->channelHostnameUrlGenerator->generateForRoute($channel, 'webgriffe_sylius_active_campaign_list_status_webhook'), + ['subscribe', 'unsubscribe'], + ['public', 'admin', 'system'], + $activeCampaignListId, + )); + } catch (\Throwable $e) { + $this->logger?->error($e->getMessage(), $e->getTrace()); + + throw $e; + } } } diff --git a/tests/Application/config/packages/monolog.yaml b/tests/Application/config/packages/monolog.yaml new file mode 100644 index 0000000..9ac8a37 --- /dev/null +++ b/tests/Application/config/packages/monolog.yaml @@ -0,0 +1,7 @@ +monolog: + handlers: + active_campaign: + type: stream + path: "%kernel.logs_dir%/active_campaign_%kernel.environment%.log" + level: debug + channels: ['webgriffe_sylius_active_campaign_plugin']