Completed
Push — 4.0 ( b212b0...d8f189 )
by chihiro
06:17 queued 10s
created

SystemService::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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\Service;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Symfony\Component\HttpKernel\KernelEvents;
21
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
22
23
class SystemService implements EventSubscriberInterface
24
{
25
    const AUTO_MAINTENANCE = 'auto_maintenance';
26
    const AUTO_MAINTENANCE_UPDATE = 'auto_maintenance_update';
27
28
    /**
29
     * メンテナンスモードを無効にする場合はtrue
30
     * @var bool
31
     */
32 3
    private $disableMaintenanceAfterResponse = false;
33
34 3
    /**
35
     * メンテナンスモードの識別子
36
     * @var string
37
     */
38
    private $maintenanceMode = null;
39
40
    /**
41
     * @var EntityManagerInterface
42 3
     */
43
    protected $entityManager;
44 3
45 3
    /**
46
     * @var ContainerInterface
47 3
     */
48 3
    protected $container;
49
50 3
    /**
51 3
     * SystemService constructor.
52 3
     *
53
     * @param EntityManagerInterface $entityManager
54
     * @param ContainerInterface $container
55
     */
56
    public function __construct(
57
        EntityManagerInterface $entityManager,
58
        ContainerInterface $container
59
    ) {
60
        $this->entityManager = $entityManager;
61
        $this->container = $container;
62
    }
63
64
    /**
65 3
     * get DB version
66 3
     *
67 3
     * @return string
68
     */
69 3
    public function getDbversion()
70
    {
71
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
72
        $rsm->addScalarResult('v', 'v');
73
74
        $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName();
75
        switch ($platform) {
76
            case 'sqlite':
77
                $prefix = 'SQLite version ';
78
                $func = 'sqlite_version()';
79
                break;
80
81
            case 'mysql':
82
                $prefix = 'MySQL ';
83
                $func = 'version()';
84
                break;
85
86
            case 'pgsql':
87
            default:
88
                $prefix = '';
89
                $func = 'version()';
90
        }
91
92
        $version = $this->entityManager
93
            ->createNativeQuery('select '.$func.' as v', $rsm)
94
            ->getSingleScalarResult();
95
96
        return $prefix.$version;
97
    }
98
99
    /**
100
     * Try to set new values memory_limit | return true
101
     *
102
     * @param string $memory | EX: 1536M
103
     *
104
     * @return bool
105
     */
106
    public function canSetMemoryLimit($memory)
107
    {
108
        try {
109
            $ret = ini_set('memory_limit', $memory);
110
        } catch (\Exception $exception) {
111
            return false;
112
        }
113
114
        return ($ret === false) ? false : true;
115
    }
116
117
    /**
118
     * Get memory_limit | Megabyte
119
     *
120
     * @return float|int
121
     */
122
    public function getMemoryLimit()
123
    {
124
        // Data type: bytes
125
        $memoryLimit = (new MemoryDataCollector())->getMemoryLimit();
126
        if (-1 == $memoryLimit) {
127
            return -1;
128
        }
129
130
        return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024;
131
    }
132
133
    /**
134
     * メンテナンスモードを切り替える
135
     *
136
     * - $isEnable = true の場合, $mode の文字列が記載された .maintenance ファイルを生成する
137
     * - $isEnable = false の場合, $mode の文字列が記載された .maintenance ファイルを削除する
138
     *
139
     * @param bool $isEnable
140
     * @param string $mode
141
     */
142
    public function switchMaintenance($isEnable = false, $mode = self::AUTO_MAINTENANCE)
143
    {
144
        $isMaintenanceMode = $this->isMaintenanceMode();
145
        $path = $this->container->getParameter('eccube_content_maintenance_file_path');
146
147
        if ($isEnable && $isMaintenanceMode === false) {
148
            file_put_contents($path, $mode);
149
        } elseif ($isEnable === false && $isMaintenanceMode) {
150
            $contents = file_get_contents($path);
151
            if ($contents == $mode) {
152
                unlink($path);
153
            }
154
        }
155
    }
156
157
    /**
158
     * KernelEvents::TERMINATE で設定されるEvent
159
     *
160
     * @param PostResponseEvent $event
161
     */
162
    public function disableMaintenanceEvent(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...
163
    {
164
        if ($this->disableMaintenanceAfterResponse) {
165
            $this->switchMaintenance(false, $this->maintenanceMode);
166
        }
167
    }
168
169
    /**
170
     * メンテナンスモードを解除する
171
     *
172
     * KernelEvents::TERMINATE で解除のEventを設定し、メンテナンスモードを解除する
173
     * @param string $mode
174
     */
175
    public function disableMaintenance($mode = self::AUTO_MAINTENANCE)
176
    {
177
        $this->disableMaintenanceAfterResponse = true;
178
        $this->maintenanceMode = $mode;
179
    }
180
181
    /**
182
     * メンテナンスモードの状態を判定する
183
     *
184
     * @return Bool
185
     */
186
    public function isMaintenanceMode()
187
    {
188
        // .maintenanceが存在しているかチェック
189
        return file_exists($this->container->getParameter('eccube_content_maintenance_file_path'));
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public static function getSubscribedEvents()
196
    {
197
        return [KernelEvents::TERMINATE => 'disableMaintenanceEvent'];
198
    }
199
}
200