src/WellCommerce/Bundle/OrderBundle/EventListener/OrderStatusHistorySubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\OrderBundle\EventListener;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Messenger\Stamp\DelayStamp;
  16. use WellCommerce\Bundle\AppBundle\Entity\Client;
  17. use WellCommerce\Bundle\AppBundle\Entity\User;
  18. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  19. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  20. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
  21. use WellCommerce\Bundle\OrderBundle\Service\Messenger\OrderReviewMessage;
  22. /**
  23.  * Class OrderStatusHistorySubscriber
  24.  *
  25.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  26.  */
  27. class OrderStatusHistorySubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  28. {
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             'order_status_history.pre_create' => ['onOrderStatusHistoryPreCreate'0],
  33.         ];
  34.     }
  35.     public function onOrderStatusHistoryPreCreate(EntityEvent $event)
  36.     {
  37.         $history $event->getEntity();
  38.         if ($history instanceof OrderStatusHistory) {
  39.             $order   $history->getOrder();
  40.             $locale  $order->getLocale();
  41.             $shop    $order->getShop();
  42.             $status  $history->getOrderStatus();
  43.             $comment $history->getComment();
  44.             $user    $this->getSecurityHelper()->getCurrentAdmin();
  45.             $this->getEntityManager()->getFilters()->enable('locale')->setParameter('locale'$order->getLocale());
  46.             $this->getTranslatorHelper()->forceLocale($locale);
  47.             if ($user instanceof User) {
  48.                 $history->setAuthor($user->getFirstName() . ' ' $user->getLastName());
  49.             }
  50.             if (empty($comment)) {
  51.                 $history->setComment($status->translate($locale)->getDefaultComment());
  52.             }
  53.             if ($history->isNotify()) {
  54.                 $this->getThemeStorage()->setCurrentTheme($order->getShop()->getTheme());
  55.                 if ($this->getMailerHelper()->isEmailValid($order->getContactDetails()->getEmail())) {
  56.                     $title    $this->getTranslatorHelper()->trans('order_status_history.email.status_changed');
  57.                     $template 'WellCommerceOrderBundle:Email:order_status_change.html.twig';
  58.                     $search   = ['%number%''%id%'];
  59.                     $replace  = [$order->getNumber(), $order->getId()];
  60.                     if (!empty($status->getEmailTitle())) {
  61.                         $title str_replace($search$replace$status->getEmailTitle());
  62.                     }
  63.                     if (!empty($status->translate($locale)->getTranslatedEmailTitle())) {
  64.                         $title str_replace($search$replace$status->translate()->getTranslatedEmailTitle($locale));
  65.                     }
  66.                     if (null !== $status->getEmailTemplate() && '' !== $status->getEmailTemplate()) {
  67.                         $template 'WellCommerceOrderBundle:Email:' $status->getEmailTemplate();
  68.                     }
  69.                     $this->getMailerHelper()->sendEmail([
  70.                         'recipient'     => $order->getContactDetails()->getEmail(),
  71.                         'bcc'           => [],
  72.                         'subject'       => $title,
  73.                         'template'      => $template,
  74.                         'locale'        => $order->getLocale(),
  75.                         'parameters'    => [
  76.                             'history' => $history,
  77.                             'order'   => $order,
  78.                             'status'  => $status,
  79.                         ],
  80.                         'configuration' => $shop->getMailerConfiguration(),
  81.                     ]);
  82.                     if ($shop->isBeamsEnabled() && $order->getClient() instanceof Client) {
  83.                         $user    $order->getClient()->getToken();
  84.                         $users   = [];
  85.                         $users[] = $user;
  86.                         $this->getPusherHelper()->publishToUsers(
  87.                             $users,
  88.                             'Zmiana statusu Twojego zamówienia ' $order->getNumber(),
  89.                             'Nowy status ' $status->translate()->getName()
  90.                         );
  91.                     }
  92.                     if (false === $shop->getOrderReviewStatuses()->isEmpty()) {
  93.                         if ($shop->getOrderReviewStatuses()->contains($status)) {
  94.                             $this->getMessageBus()->dispatch(new OrderReviewMessage($order->getId()), [
  95.                                 new DelayStamp(5000),
  96.                             ]);
  97.                         }
  98.                     }
  99.                 }
  100.             }
  101.             $order->setCurrentStatus($history->getOrderStatus());
  102.         }
  103.     }
  104. }