<?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\Messenger\Stamp\DelayStamp;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Entity\User;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
use WellCommerce\Bundle\OrderBundle\Service\Messenger\OrderReviewMessage;
/**
* Class OrderStatusHistorySubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class OrderStatusHistorySubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'order_status_history.pre_create' => ['onOrderStatusHistoryPreCreate', 0],
];
}
public function onOrderStatusHistoryPreCreate(EntityEvent $event)
{
$history = $event->getEntity();
if ($history instanceof OrderStatusHistory) {
$order = $history->getOrder();
$locale = $order->getLocale();
$shop = $order->getShop();
$status = $history->getOrderStatus();
$comment = $history->getComment();
$user = $this->getSecurityHelper()->getCurrentAdmin();
$this->getEntityManager()->getFilters()->enable('locale')->setParameter('locale', $order->getLocale());
$this->getTranslatorHelper()->forceLocale($locale);
if ($user instanceof User) {
$history->setAuthor($user->getFirstName() . ' ' . $user->getLastName());
}
if (empty($comment)) {
$history->setComment($status->translate($locale)->getDefaultComment());
}
if ($history->isNotify()) {
$this->getThemeStorage()->setCurrentTheme($order->getShop()->getTheme());
if ($this->getMailerHelper()->isEmailValid($order->getContactDetails()->getEmail())) {
$title = $this->getTranslatorHelper()->trans('order_status_history.email.status_changed');
$template = 'WellCommerceOrderBundle:Email:order_status_change.html.twig';
$search = ['%number%', '%id%'];
$replace = [$order->getNumber(), $order->getId()];
if (!empty($status->getEmailTitle())) {
$title = str_replace($search, $replace, $status->getEmailTitle());
}
if (!empty($status->translate($locale)->getTranslatedEmailTitle())) {
$title = str_replace($search, $replace, $status->translate()->getTranslatedEmailTitle($locale));
}
if (null !== $status->getEmailTemplate() && '' !== $status->getEmailTemplate()) {
$template = 'WellCommerceOrderBundle:Email:' . $status->getEmailTemplate();
}
$this->getMailerHelper()->sendEmail([
'recipient' => $order->getContactDetails()->getEmail(),
'bcc' => [],
'subject' => $title,
'template' => $template,
'locale' => $order->getLocale(),
'parameters' => [
'history' => $history,
'order' => $order,
'status' => $status,
],
'configuration' => $shop->getMailerConfiguration(),
]);
if ($shop->isBeamsEnabled() && $order->getClient() instanceof Client) {
$user = $order->getClient()->getToken();
$users = [];
$users[] = $user;
$this->getPusherHelper()->publishToUsers(
$users,
'Zmiana statusu Twojego zamówienia ' . $order->getNumber(),
'Nowy status ' . $status->translate()->getName()
);
}
if (false === $shop->getOrderReviewStatuses()->isEmpty()) {
if ($shop->getOrderReviewStatuses()->contains($status)) {
$this->getMessageBus()->dispatch(new OrderReviewMessage($order->getId()), [
new DelayStamp(5000),
]);
}
}
}
}
$order->setCurrentStatus($history->getOrderStatus());
}
}
}