<?php
/*
* Plugin Name : CustomerRank
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerRank42\Event;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Repository\Master\OrderStatusRepository;
use Plugin\CustomerRank42\Service\CustomerRankService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Workflow\Event\Event;
class OrderStateEvent implements EventSubscriberInterface
{
private $entityManager;
private $requestStack;
private $orderStatusRepository;
private $customerRankService;
public function __construct(
EntityManagerInterface $entityManager,
RequestStack $requestStack,
OrderStatusRepository $orderStatusRepository,
CustomerRankService $customerRankService
)
{
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
$this->orderStatusRepository = $orderStatusRepository;
$this->customerRankService = $customerRankService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'workflow.order.transition.pay' => 'checkRank',
'workflow.order.transition.packing' => 'checkRank',
'workflow.order.transition.cancel' => 'checkRank',
'workflow.order.transition.back_to_in_progress' => 'checkRank',
'workflow.order.transition.return' => 'checkRank',
'workflow.order.transition.cancel_return' => 'checkRank',
'workflow.order.transition.ship' => 'checkRank',
];
}
public function checkRank(Event $event)
{
$request = $this->requestStack->getMainRequest();
$route = $request->get('_route');
$Order = $event->getSubject()->getOrder();
if($route === 'admin_shipping_update_order_status' || $route == 'admin_shipping_expresslink_mail'){
$transition = $event->getTransition();
$to = $transition->getTos()[0];
$OrderStatus = $this->orderStatusRepository->find($to);
$Order->setOrderStatus($OrderStatus);
$this->entityManager->persist($Order);
$this->entityManager->flush();
}
$Customer = $Order->getCustomer();
if ($Customer) {
$this->customerRankService->checkRank($Customer);
}
}
}