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