src/WellCommerce/Bundle/OrderBundle/EventListener/OrderStatusSubscriber.php line 97

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\Filesystem\Filesystem;
  16. use Symfony\Component\HttpKernel\KernelInterface;
  17. use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
  18. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  19. use WellCommerce\Bundle\CoreBundle\Helper\Helper;
  20. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
  21. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusGroup;
  22. use WellCommerce\Bundle\OrderBundle\Manager\OrderStatusGroupManager;
  23. use WellCommerce\Bundle\OrderBundle\Manager\OrderStatusManager;
  24. use WellCommerce\Component\Form\Event\FormEvent;
  25. /**
  26.  * Class OrderStatusSubscriber
  27.  *
  28.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  29.  */
  30. class OrderStatusSubscriber implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var OrderStatusGroupManager
  34.      */
  35.     protected $orderStatusGroupManager;
  36.     /**
  37.      * @var OrderStatusManager
  38.      */
  39.     protected $orderStatusManager;
  40.     /**
  41.      * @var KernelInterface
  42.      */
  43.     protected $kernel;
  44.     /**
  45.      * @var ShopStorage
  46.      */
  47.     protected $shopStorage;
  48.     public function __construct(
  49.         OrderStatusGroupManager $orderStatusGroupManager,
  50.         OrderStatusManager $orderStatusManager,
  51.         KernelInterface $kernel,
  52.         ShopStorage $shopStorage
  53.     ) {
  54.         $this->orderStatusGroupManager $orderStatusGroupManager;
  55.         $this->orderStatusManager      $orderStatusManager;
  56.         $this->kernel                  $kernel;
  57.         $this->shopStorage             $shopStorage;
  58.     }
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             'order_status.post_init'           => ['onOrderStatusPostInit'0],
  63.             'admin.order_status.pre_form_init' => ['onOrderStatusFormAdminInit'],
  64.         ];
  65.     }
  66.     public function onOrderStatusFormAdminInit(FormEvent $event)
  67.     {
  68.         $resource $event->getResource();
  69.         if ($resource instanceof OrderStatus) {
  70.             $filesystem    = new Filesystem();
  71.             $rootPath      $this->kernel->getProjectDir();
  72.             $themeFolder   $this->shopStorage->getCurrentShop()->getThemeFolder();
  73.             $templatesPath sprintf('%s/templates/%s/WellCommerceOrderBundle/Email/'$rootPath$themeFolder);
  74.             $statuses $this->orderStatusManager->getCollection();
  75.             $statuses->map(function (OrderStatus $orderStatus) use ($filesystem$templatesPath$rootPath) {
  76.                 $templateName Helper::urlize($orderStatus->translate()->getName()) . '.html.twig';
  77.                 $templateFile $templatesPath $templateName;
  78.                 $originFile   $templatesPath 'order_status_change.html.twig';
  79.                 if (false === $filesystem->exists($templateFile)) {
  80.                     $filesystem->copy($originFile$templateFile);
  81.                 }
  82.             });
  83.         }
  84.     }
  85.     public function onOrderStatusPostInit(EntityEvent $event)
  86.     {
  87.         $orderStatus $event->getEntity();
  88.         if ($orderStatus instanceof OrderStatus) {
  89.             $orderStatus->setOrderStatusGroup($this->getDefaultOrderStatusGroup());
  90.         }
  91.     }
  92.     private function getDefaultOrderStatusGroup(): OrderStatusGroup
  93.     {
  94.         return $this->orderStatusGroupManager->getRepository()->findOneBy([]);
  95.     }
  96. }