Failed Conditions
Push — master ( dbea68...9459a9 )
by Kentaro
01:14
created

TransactionListener::onKernelTerminate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4.0312
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\EventListener;
25
26
use Eccube\Application;
27
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
28
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
29
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
30
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
31
use Symfony\Component\HttpKernel\KernelEvents;
32
33
/**
34
 * トランザクション制御のためのListener
35
 *
36
 * @package Eccube\EventListener
37
 */
38
class TransactionListener implements EventSubscriberInterface
39
{
40
    private $app;
41
42
    /**
43
     * Constructor function.
44
     *
45
     * @param Application $app
46
     */
47 936
    public function __construct(Application $app)
48
    {
49 936
        $this->app = $app;
50
    }
51
52
    /**
53
     * Kernel request listener callback.
54
     *
55
     * @param GetResponseEvent $event
56
     */
57 352
    public function onKernelRequest(GetResponseEvent $event)
58
    {
59 352
        if (!$event->isMasterRequest()) {
60 98
            return;
61
        }
62
63 350
        $app = $this->app;
64 350
        if (!$app->isTestMode()) {
65 9
            $app['orm.em']->getConnection()->setAutoCommit(false);
66 9
            $app['orm.em']->beginTransaction();
67
        } else {
68 341
            $this->app->log('TestCase to onKernelRequest of beginTransaction');
69
        }
70
    }
71
72
    /**
73
     * Kernel exception listener callback.
74
     *
75
     * @param GetResponseForExceptionEvent $event
76
     */
77 12
    public function onKernelException(GetResponseForExceptionEvent $event)
78
    {
79 12
        if (!$event->isMasterRequest()) {
80
            return;
81
        }
82
83 12
        $app = $this->app;
84 12
        if (!$app->isTestMode()) {
85 4
            if ($app['orm.em']->getConnection()->isTransactionActive()) {
86 4
                $app['orm.em']->rollback();
87
            }
88
        } else {
89 8
            $this->app->log('TestCase to onKernelException of rollback');
90
        }
91
    }
92
93
    /**
94
     *  Kernel terminate listener callback.
95
     *
96
     * @param PostResponseEvent $event
97
     */
98 347
    public function onKernelTerminate(PostResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100 347
        $app = $this->app;
101
102 347
        if (!$app->isTestMode()) {
103 9
            if ($app['orm.em']->getConnection()->isTransactionActive()) {
104 9
                if ($app['orm.em']->getConnection()->isRollbackOnly()) {
105
                    $app['orm.em']->rollback();
106
                } else {
107 9
                    $app['orm.em']->commit();
108
                }
109
            }
110
        } else {
111 338
            $this->app->log('TestCase to onKernelTerminate of commit.');
112
        }
113
    }
114
115
    /**
116
     * Return the events to subscribe to.
117
     *
118
     * @return array
119
     */
120 936
    public static function getSubscribedEvents()
121
    {
122
        return array(
123 936
            KernelEvents::REQUEST => 'onKernelRequest',
124
            KernelEvents::EXCEPTION => 'onKernelException',
125
            KernelEvents::TERMINATE => 'onKernelTerminate',
126
        );
127
    }
128
}
129