<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\OrderBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\CoreBundle\Helper\Helper;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusGroup;
use WellCommerce\Bundle\OrderBundle\Manager\OrderStatusGroupManager;
use WellCommerce\Bundle\OrderBundle\Manager\OrderStatusManager;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class OrderStatusSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class OrderStatusSubscriber implements EventSubscriberInterface
{
/**
* @var OrderStatusGroupManager
*/
protected $orderStatusGroupManager;
/**
* @var OrderStatusManager
*/
protected $orderStatusManager;
/**
* @var KernelInterface
*/
protected $kernel;
/**
* @var ShopStorage
*/
protected $shopStorage;
public function __construct(
OrderStatusGroupManager $orderStatusGroupManager,
OrderStatusManager $orderStatusManager,
KernelInterface $kernel,
ShopStorage $shopStorage
) {
$this->orderStatusGroupManager = $orderStatusGroupManager;
$this->orderStatusManager = $orderStatusManager;
$this->kernel = $kernel;
$this->shopStorage = $shopStorage;
}
public static function getSubscribedEvents()
{
return [
'order_status.post_init' => ['onOrderStatusPostInit', 0],
'admin.order_status.pre_form_init' => ['onOrderStatusFormAdminInit'],
];
}
public function onOrderStatusFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof OrderStatus) {
$filesystem = new Filesystem();
$rootPath = $this->kernel->getProjectDir();
$themeFolder = $this->shopStorage->getCurrentShop()->getThemeFolder();
$templatesPath = sprintf('%s/templates/%s/WellCommerceOrderBundle/Email/', $rootPath, $themeFolder);
$statuses = $this->orderStatusManager->getCollection();
$statuses->map(function (OrderStatus $orderStatus) use ($filesystem, $templatesPath, $rootPath) {
$templateName = Helper::urlize($orderStatus->translate()->getName()) . '.html.twig';
$templateFile = $templatesPath . $templateName;
$originFile = $templatesPath . 'order_status_change.html.twig';
if (false === $filesystem->exists($templateFile)) {
$filesystem->copy($originFile, $templateFile);
}
});
}
}
public function onOrderStatusPostInit(EntityEvent $event)
{
$orderStatus = $event->getEntity();
if ($orderStatus instanceof OrderStatus) {
$orderStatus->setOrderStatusGroup($this->getDefaultOrderStatusGroup());
}
}
private function getDefaultOrderStatusGroup(): OrderStatusGroup
{
return $this->orderStatusGroupManager->getRepository()->findOneBy([]);
}
}