src/Eccube/EventListener/TransactionListener.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\EventListener;
  13. use Doctrine\DBAL\Connection;
  14. use Doctrine\DBAL\TransactionIsolationLevel;
  15. use Doctrine\ORM\EntityManager;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * トランザクション制御のためのListener
  24.  */
  25. class TransactionListener implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var EntityManagerInterface
  29.      */
  30.     protected $em;
  31.     /**
  32.      * @var bool
  33.      */
  34.     protected $isEnabled true;
  35.     /**
  36.      * TransactionListener constructor.
  37.      *
  38.      * @param EntityManager $em
  39.      * @param bool $isEnabled
  40.      */
  41.     public function __construct(EntityManagerInterface $em$isEnabled true)
  42.     {
  43.         $this->em $em;
  44.         $this->isEnabled $isEnabled;
  45.     }
  46.     /**
  47.      * Disable transaction listener.
  48.      */
  49.     public function disable()
  50.     {
  51.         $this->isEnabled false;
  52.     }
  53.     /**
  54.      * Kernel request listener callback.
  55.      *
  56.      * @param RequestEvent $event
  57.      */
  58.     public function onKernelRequest(RequestEvent $event)
  59.     {
  60.         if (!$this->isEnabled) {
  61.             log_debug('Transaction Listener is disabled.');
  62.             return;
  63.         }
  64.         if (!$event->isMainRequest()) {
  65.             return;
  66.         }
  67.         /** @var Connection $Connection */
  68.         $Connection $this->em->getConnection();
  69.         if (!$Connection->isConnected()) {
  70.             $Connection->connect();
  71.         }
  72.         $Connection->setAutoCommit(false);
  73.         $Connection->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED);
  74.         $this->em->beginTransaction();
  75.         log_debug('Begin Transaction.');
  76.     }
  77.     /**
  78.      * Kernel exception listener callback.
  79.      *
  80.      * @param ExceptionEvent $event
  81.      */
  82.     public function onKernelException(ExceptionEvent $event)
  83.     {
  84.         if (!$this->isEnabled) {
  85.             log_debug('Transaction Listener is disabled.');
  86.             return;
  87.         }
  88.         if (!$event->isMainRequest()) {
  89.             return;
  90.         }
  91.         if ($this->em->getConnection()->getNativeConnection()->inTransaction()) {
  92.             if ($this->em->getConnection()->isRollbackOnly()) {
  93.                 $this->em->rollback();
  94.             }
  95.             log_debug('Rollback executed.');
  96.         } else {
  97.             log_debug('Transaction is not active. Rollback skipped.');
  98.         }
  99.     }
  100.     /**
  101.      *  Kernel terminate listener callback.
  102.      *
  103.      * @param PostResponseEvent $event
  104.      */
  105.     public function onKernelTerminate(TerminateEvent $event)
  106.     {
  107.         if (!$this->isEnabled) {
  108.             log_debug('Transaction Listener is disabled.');
  109.             return;
  110.         }
  111.         if ($this->em->getConnection()->getNativeConnection()->inTransaction()) {
  112.             if ($this->em->getConnection()->isRollbackOnly()) {
  113.                 $this->em->rollback();
  114.                 log_debug('Rollback executed.');
  115.             } else {
  116.                 $this->em->commit();
  117.                 log_debug('Commit executed.');
  118.             }
  119.         } else {
  120.             log_debug('Transaction is not active. Rollback skipped.');
  121.         }
  122.     }
  123.     /**
  124.      * Return the events to subscribe to.
  125.      *
  126.      * @return array
  127.      */
  128.     public static function getSubscribedEvents()
  129.     {
  130.         return [
  131.             KernelEvents::REQUEST => 'onKernelRequest',
  132.             KernelEvents::EXCEPTION => 'onKernelException',
  133.             KernelEvents::TERMINATE => 'onKernelTerminate',
  134.         ];
  135.     }
  136. }