app/Plugin/CustomerRank42/Event/OrderStateEvent.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Plugin\CustomerRank42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Repository\Master\OrderStatusRepository;
  14. use Plugin\CustomerRank42\Service\CustomerRankService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Workflow\Event\Event;
  18. class OrderStateEvent implements EventSubscriberInterface
  19. {
  20.     private $entityManager;
  21.     private $requestStack;
  22.     private $orderStatusRepository;
  23.     private $customerRankService;
  24.     public function __construct(
  25.             EntityManagerInterface $entityManager,
  26.             RequestStack $requestStack,
  27.             OrderStatusRepository $orderStatusRepository,
  28.             CustomerRankService $customerRankService
  29.             )
  30.     {
  31.         $this->entityManager $entityManager;
  32.         $this->requestStack $requestStack;
  33.         $this->orderStatusRepository $orderStatusRepository;
  34.         $this->customerRankService $customerRankService;
  35.     }
  36.     /**
  37.      * @return array
  38.      */
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             'workflow.order.transition.pay' => 'checkRank',
  43.             'workflow.order.transition.packing' => 'checkRank',
  44.             'workflow.order.transition.cancel' => 'checkRank',
  45.             'workflow.order.transition.back_to_in_progress' => 'checkRank',
  46.             'workflow.order.transition.return' => 'checkRank',
  47.             'workflow.order.transition.cancel_return' => 'checkRank',
  48.             'workflow.order.transition.ship' => 'checkRank',
  49.         ];
  50.     }
  51.     public function checkRank(Event $event)
  52.     {
  53.         $request $this->requestStack->getMainRequest();
  54.         $route $request->get('_route');
  55.         $Order $event->getSubject()->getOrder();
  56.         if($route === 'admin_shipping_update_order_status' || $route == 'admin_shipping_expresslink_mail'){
  57.             $transition $event->getTransition();
  58.             $to $transition->getTos()[0];
  59.             $OrderStatus $this->orderStatusRepository->find($to);
  60.             $Order->setOrderStatus($OrderStatus);
  61.             $this->entityManager->persist($Order);
  62.             $this->entityManager->flush();
  63.         }
  64.         $Customer $Order->getCustomer();
  65.         if ($Customer) {
  66.             $this->customerRankService->checkRank($Customer);
  67.         }
  68.     }
  69. }