custom/plugins/ZeobvGetNotified/src/Storefront/Subscriber/NotificationSubscriber.php line 65

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace Zeobv\GetNotified\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  7. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  8. use Zeobv\GetNotified\Service\MailService;
  9. use Zeobv\GetNotified\Service\ConfigService;
  10. /**
  11.  * Class NotificationSubscriber
  12.  *
  13.  * @package Zeobv\GetNotified\Storefront\Subscriber
  14.  */
  15. class NotificationSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var ConfigService
  19.      */
  20.     protected $configService;
  21.     /**
  22.      * @var MailService
  23.      */
  24.     protected $mailService;
  25.     /**
  26.      * @var LoggerInterface
  27.      */
  28.     protected $logger;
  29.     /**
  30.      * NotificationSubscriber constructor.
  31.      *
  32.      * @param ConfigService   $configService
  33.      * @param MailService     $mailService
  34.      * @param LoggerInterface $logger
  35.      */
  36.     public function __construct(
  37.         ConfigService $configService,
  38.         MailService $mailService,
  39.         LoggerInterface $logger
  40.     ) {
  41.         $this->configService $configService;
  42.         $this->mailService $mailService;
  43.         $this->logger $logger;
  44.     }
  45.     /**
  46.      * @return array
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             'zeo_stock_subscriber_product.written' => 'onNewStockSubscriber',
  52.         ];
  53.     }
  54.     public function onNewStockSubscriber(EntityWrittenEvent $event): void
  55.     {
  56.         /** @var SalesChannelApiSource|mixed $source */
  57.         $source $event->getContext()->getSource();
  58.         if (!$event->getContext()->getSource() instanceof SalesChannelApiSource) {
  59.             return;
  60.         }
  61.         $scId $source->getSalesChannelId();
  62.         if (!$this->configService->getReceiveNotificationEmailOnNewSubscription($scId)) {
  63.             return;
  64.         }
  65.         $notificationEmail $this->configService->getNotificationEmail($scId);
  66.         if (empty($notificationEmail)) {
  67.             return;
  68.         }
  69.         /** @var EntityWriteResult $writeResult */
  70.         foreach ($event->getWriteResults() as $writeResult) {
  71.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  72.                 continue;
  73.             }
  74.             $payload $writeResult->getPayload();
  75.             if (!is_array($payload) || !key_exists('stockSubscriberId'$payload)) {
  76.                 continue;
  77.             }
  78.             try {
  79.                 $this->mailService->sendNewSubscriptionMail($notificationEmail$payload$event->getContext());
  80.             } catch (\Throwable $e) {
  81.                 $this->logger->critical($e->getMessage(), ['trace' => $e->getTrace()]);
  82.             }
  83.         }
  84.     }
  85. }