vendor/lnb/shopware6-lnb-review-mail/src/Flow/Action/SendReviewMailAction.php line 42

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbReviewMail\Flow\Action;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Event\OrderAware;
  10. class SendReviewMailAction extends FlowAction
  11. {
  12.     private EntityRepository $reviewMailRepository;
  13.     private LoggerInterface $logger;
  14.     public function __construct(EntityRepository $reviewMailRepositoryLoggerInterface $logger)
  15.     {
  16.         $this->reviewMailRepository $reviewMailRepository;
  17.         $this->logger $logger;
  18.     }
  19.     public static function getName(): string
  20.     {
  21.         return 'lnbReviewMail.action.send.review.mail';
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             self::getName() => 'handle',
  27.         ];
  28.     }
  29.     public function requirements(): array
  30.     {
  31.         return [OrderAware::class];
  32.     }
  33.     public function handle(FlowEvent $event): void
  34.     {
  35.         if (! $event->getEvent() instanceof OrderAware) {
  36.             return; //flow is not supported
  37.         }
  38.         $orderId $event->getEvent()->getOrderId();
  39.         $config $event->getConfig();
  40.         if (! isset($config['days'])) {
  41.             $this->logger->critical(sprintf('Missing required parameter "days" for flow "%s" in action sendReviewMailAction'$event->getEvent()->getName()));
  42.             throw new \InvalidArgumentException('Missing required parameter "days"');
  43.         }
  44.         if (! isset($config['mailTemplateId'])) {
  45.             $this->logger->critical(sprintf('Missing required parameter "mailTemplateId" for flow "%s" in action sendReviewMailAction'$event->getEvent()->getName()));
  46.             throw new \InvalidArgumentException('Missing required parameter "mailTemplateId"');
  47.         }
  48.         if ($this->delayedMailAlreadyExists($orderId)) {
  49.             return;
  50.         }
  51.         try {
  52.             $this->reviewMailRepository->create([
  53.                 [
  54.                     'orderId' => $orderId,
  55.                     'mailTemplateId' => $config['mailTemplateId'],
  56.                     'status' => 'pending',
  57.                     'days' => $config['days'],
  58.                     'dueDate' => $this->getDueDate($config['days']),
  59.                 ],
  60.             ], $event->getContext());
  61.         } catch (\Throwable $e) {
  62.             $this->logger->error('Could not create review mail', [
  63.                 'orderId' => $orderId,
  64.                 'mailTemplateId' => $config['mailTemplateId'],
  65.                 'days' => $config['days'],
  66.                 'dueDate' => $this->getDueDate($config['days']),
  67.                 'error' => $e->getMessage(),
  68.                 'trace' => $e->getTraceAsString(),
  69.             ]);
  70.         }
  71.     }
  72.     private function getDueDate(int $days): \DateTimeInterface
  73.     {
  74.         return (new \DateTime())->modify(sprintf('+%d days'$days));
  75.     }
  76.     private function delayedMailAlreadyExists(string $orderId): bool
  77.     {
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(new EqualsFilter('orderId'$orderId));
  80.         $result $this->reviewMailRepository->search($criteria\Shopware\Core\Framework\Context::createDefaultContext());
  81.         return $result->getTotal() > 0;
  82.     }
  83. }