From 9d559786884fae6d930ff15cea4d952bbd526ff1 Mon Sep 17 00:00:00 2001 From: Michal Dabrowski Date: Wed, 13 Nov 2024 14:10:20 +0100 Subject: [PATCH 1/2] log error --- src/WebPushSender/WebPushSender.php | 44 +++++++++++++++++++---------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/WebPushSender/WebPushSender.php b/src/WebPushSender/WebPushSender.php index 7fe7bb9..3685214 100644 --- a/src/WebPushSender/WebPushSender.php +++ b/src/WebPushSender/WebPushSender.php @@ -7,6 +7,7 @@ use BenTools\WebPushBundle\Model\Message\PushNotification; use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerInterface; use BenTools\WebPushBundle\Sender\PushMessageSender; +use Psr\Log\LoggerInterface; use SpearDevs\SyliusPushNotificationsPlugin\Context\ChannelContextInterface; use SpearDevs\SyliusPushNotificationsPlugin\Entity\PushNotificationTemplate\PushNotificationTemplate; use SpearDevs\SyliusPushNotificationsPlugin\Factory\Interfaces\WebPushFactoryInterface; @@ -20,6 +21,8 @@ use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\User\Model\UserInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\Translation\TranslatableMessage; use Traversable; final class WebPushSender implements WebPushSenderInterface @@ -33,16 +36,19 @@ final class WebPushSender implements WebPushSenderInterface public const GROUP_RECEIVER = 'group'; public function __construct( - private UserSubscriptionRepositoryInterface $userSubscriptionRepository, - private UserSubscriptionManagerInterface $userSubscriptionManager, - private PushMessageSender $sender, + private UserSubscriptionRepositoryInterface $userSubscriptionRepository, + private UserSubscriptionManagerInterface $userSubscriptionManager, + private PushMessageSender $sender, private PushNotificationTemplateRepositoryInterface $pushNotificationTemplateRepository, - private PushNotificationConfigurationService $pushNotificationConfigurationService, - private WebPushHistoryCreatorInterface $webPushHistoryCreator, - private ChannelContextInterface $channelContext, - private WebPushFactoryInterface $webPushFactory, - private ParameterMapperInterface $orderParameterMapper, - ) { + private PushNotificationConfigurationService $pushNotificationConfigurationService, + private WebPushHistoryCreatorInterface $webPushHistoryCreator, + private ChannelContextInterface $channelContext, + private WebPushFactoryInterface $webPushFactory, + private ParameterMapperInterface $orderParameterMapper, + private LoggerInterface $logger, + private SessionInterface $session, + ) + { } public function sendWebPush(SendPushNotificationFormModel $sendPushNotificationFormModel): void @@ -93,13 +99,21 @@ public function sendOrderWebPush(OrderInterface $order, string $pushNotification $this->channelContext->setChannelCode($channel->getCode()); $webPush = $this->webPushFactory->create($this->orderParameterMapper, $order, $pushNotificationTemplate); - - $this->sendToUser( - $webPush, - $channel, - $user->getEmail(), - ); + try { + $this->sendToUser( + $webPush, + $channel, + $user->getEmail(), + ); + } catch (\Exception $e) { + $this->session->getFlashBag()->add( + 'error', + new TranslatableMessage('speardevs_sylius_push_notifications_plugin.ui.sent_error') + ); + $this->logger->error('Problem while sending push notifications ' . $e->getMessage()); + } } + } private function send(WebPushInterface $webPush, iterable $subscriptions): void From ec47d729038b868e4bdb8af439c867d8743db94c Mon Sep 17 00:00:00 2001 From: Michal Dabrowski Date: Wed, 20 Nov 2024 14:13:11 +0100 Subject: [PATCH 2/2] cr comments fix --- src/WebPushSender/WebPushSender.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/WebPushSender/WebPushSender.php b/src/WebPushSender/WebPushSender.php index 3685214..ecc0014 100644 --- a/src/WebPushSender/WebPushSender.php +++ b/src/WebPushSender/WebPushSender.php @@ -21,8 +21,6 @@ use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\User\Model\UserInterface; -use Symfony\Component\HttpFoundation\Session\SessionInterface; -use Symfony\Component\Translation\TranslatableMessage; use Traversable; final class WebPushSender implements WebPushSenderInterface @@ -36,19 +34,17 @@ final class WebPushSender implements WebPushSenderInterface public const GROUP_RECEIVER = 'group'; public function __construct( - private UserSubscriptionRepositoryInterface $userSubscriptionRepository, - private UserSubscriptionManagerInterface $userSubscriptionManager, - private PushMessageSender $sender, + private UserSubscriptionRepositoryInterface $userSubscriptionRepository, + private UserSubscriptionManagerInterface $userSubscriptionManager, + private PushMessageSender $sender, private PushNotificationTemplateRepositoryInterface $pushNotificationTemplateRepository, - private PushNotificationConfigurationService $pushNotificationConfigurationService, - private WebPushHistoryCreatorInterface $webPushHistoryCreator, - private ChannelContextInterface $channelContext, - private WebPushFactoryInterface $webPushFactory, - private ParameterMapperInterface $orderParameterMapper, - private LoggerInterface $logger, - private SessionInterface $session, - ) - { + private PushNotificationConfigurationService $pushNotificationConfigurationService, + private WebPushHistoryCreatorInterface $webPushHistoryCreator, + private ChannelContextInterface $channelContext, + private WebPushFactoryInterface $webPushFactory, + private ParameterMapperInterface $orderParameterMapper, + private LoggerInterface $logger, + ) { } public function sendWebPush(SendPushNotificationFormModel $sendPushNotificationFormModel): void @@ -106,14 +102,9 @@ public function sendOrderWebPush(OrderInterface $order, string $pushNotification $user->getEmail(), ); } catch (\Exception $e) { - $this->session->getFlashBag()->add( - 'error', - new TranslatableMessage('speardevs_sylius_push_notifications_plugin.ui.sent_error') - ); $this->logger->error('Problem while sending push notifications ' . $e->getMessage()); } } - } private function send(WebPushInterface $webPush, iterable $subscriptions): void