custom/plugins/CioCustomerMailbox/src/Subscriber/ShowUnreadMessagesModalSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. namespace CioCustomerMailbox\Subscriber;
  3. use CioCustomerMailbox\Service\MailboxConfigService;
  4. use CioImports\Event\ImportStockMissingProductNotificationEvent;
  5. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerDeletedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  14. use Shopware\Core\Framework\Event\BusinessEvent;
  15. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  16. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Shopware\Storefront\Controller\AuthController;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  22. use Symfony\Component\HttpKernel\Event\RequestEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. use Shopware\Storefront\Event\StorefrontRenderEvent;
  25. class ShowUnreadMessagesModalSubscriber implements EventSubscriberInterface
  26. {
  27.     CONST WHITELIST_ROUTES = [
  28.         '/account/login',
  29.         '/widgets/checkout/info',
  30.         '/account/recover',
  31.         '/account/recover/password',
  32.         '/checkout/offcanvas',
  33.         '/cookie/offcanvas',
  34.         '/impressum',
  35.         '/account/mailbox'
  36.     ];
  37.     protected EntityRepository $messagesRepository;
  38.     protected MailboxConfigService $mailboxConfigService;
  39.     public function __construct(EntityRepository $messagesRepositoryMailboxConfigService $mailboxConfigService)
  40.     {
  41.         $this->messagesRepository $messagesRepository;
  42.         $this->mailboxConfigService $mailboxConfigService;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
  48.         ];
  49.     }
  50.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  51.     {
  52.         if($event->getSalesChannelContext()->getSalesChannel()->isMaintenance()) {
  53.             return;
  54.         }
  55.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  56.             $criteria = new Criteria();
  57.             // only messages for the current customer
  58.             $criteria->addFilter(new EqualsFilter('customer_id'$event->getSalesChannelContext()->getCustomer()->getId()));
  59.             // only messages that are not read
  60.             $criteria->addFilter(new EqualsFilter('read_at'null));
  61.             $count $this->messagesRepository->search($criteria$event->getSalesChannelContext()->getContext())->count();
  62.             if ($count 0) {
  63.                 $event->setParameter('unreadMessagesCount'$count);
  64.             }
  65.         }
  66.         if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
  67.             return;
  68.         }
  69.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  70.             $criteria = new Criteria();
  71.             // only messages for the current customer
  72.             $criteria->addFilter(new EqualsFilter('customer_id'$event->getSalesChannelContext()->getCustomer()->getId()));
  73.             // only messages that are published at the moment and older than x days
  74.             $criteria->addFilter(
  75.                 new RangeFilter(
  76.                     'message_content.visible_from',
  77.                     [
  78.                         'lte' => (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Berlin'))->modify('-' $this->mailboxConfigService->getShowModalAfterDays() . ' days')->format('Y-m-d H:i:s')
  79.                     ]
  80.                 )
  81.             );
  82.             // only messages that are not read
  83.             $criteria->addFilter(new EqualsFilter('read_at'null));
  84.             // only messages that are important
  85.             $criteria->addFilter(new EqualsFilter('message_content.important'true));
  86.             $count $this->messagesRepository->search($criteria$event->getSalesChannelContext()->getContext())->count();
  87.             if ($count 0) {
  88.                 $event->setParameter('showUnreadMessagesModal'true);
  89.             }
  90.         }
  91.     }
  92. }