Completed
Pull Request — 4.0 (#4023)
by Kentaro
06:42
created

SystemService::disableMaintenance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
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
    private $disableMaintenanceAfterResponse = false;
29
    private $maintenanceMode = null;
30
31
    /**
32 3
     * @var EntityManagerInterface
33
     */
34 3
    protected $entityManager;
35
36
    /**
37
     * @var ContainerInterface
38
     */
39
    protected $container;
40
41
    /**
42 3
     * SystemService constructor.
43
     *
44 3
     * @param EntityManagerInterface $entityManager
45 3
     * @param ContainerInterface $container
46
     */
47 3
    public function __construct(
48 3
        EntityManagerInterface $entityManager,
49
        ContainerInterface $container
50 3
    ) {
51 3
        $this->entityManager = $entityManager;
52 3
        $this->container = $container;
53
    }
54
55
    /**
56
     * get DB version
57
     *
58
     * @return string
59
     */
60
    public function getDbversion()
61
    {
62
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
63
        $rsm->addScalarResult('v', 'v');
64
65 3
        $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName();
66 3
        switch ($platform) {
67 3
            case 'sqlite':
68
                $prefix = 'SQLite version ';
69 3
                $func = 'sqlite_version()';
70
                break;
71
72
            case 'mysql':
73
                $prefix = 'MySQL ';
74
                $func = 'version()';
75
                break;
76
77
            case 'pgsql':
78
            default:
79
                $prefix = '';
80
                $func = 'version()';
81
        }
82
83
        $version = $this->entityManager
84
            ->createNativeQuery('select '.$func.' as v', $rsm)
85
            ->getSingleScalarResult();
86
87
        return $prefix.$version;
88
    }
89
90
    /**
91
     * Try to set new values memory_limit | return true
92
     *
93
     * @param string $memory | EX: 1536M
94
     *
95
     * @return bool
96
     */
97
    public function canSetMemoryLimit($memory)
98
    {
99
        try {
100
            $ret = ini_set('memory_limit', $memory);
101
        } catch (\Exception $exception) {
102
            return false;
103
        }
104
105
        return ($ret === false) ? false : true;
106
    }
107
108
    /**
109
     * Get memory_limit | Megabyte
110
     *
111
     * @return float|int
112
     */
113
    public function getMemoryLimit()
114
    {
115
        // Data type: bytes
116
        $memoryLimit = (new MemoryDataCollector())->getMemoryLimit();
117
        if (-1 == $memoryLimit) {
118
            return -1;
119
        }
120
121
        return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024;
122
    }
123
124
    /**
125
     * メンテナンスモードを切り替える
126
     *
127
     * - $isEnable = true の場合, $mode の文字列が記載された .maintenance ファイルを生成する
128
     * - $isEnable = false の場合, $mode の文字列が記載された .maintenance ファイルを削除する
129
     *
130
     * @param bool $isEnable
131
     * @param string $mode
132
     */
133
    public function switchMaintenance($isEnable = false, $mode = self::AUTO_MAINTENANCE)
134
    {
135
        $isMaintenanceMode = $this->isMaintenanceMode();
136
        $path = $this->container->getParameter('eccube_content_maintenance_file_path');
137
138
        if ($isEnable && $isMaintenanceMode === false) {
139
            file_put_contents($path, $mode);
140
        } elseif ($isEnable === false && $isMaintenanceMode) {
141
            $contents = file_get_contents($path);
142
            if ($contents == $mode) {
143
                unlink($path);
144
            }
145
        }
146
    }
147
148
    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...
149
    {
150
        if ($this->disableMaintenanceAfterResponse) {
151
            $this->switchMaintenance(false, $this->maintenanceMode);
152
        }
153
    }
154
155
    public function disableMaintenance($mode = self::AUTO_MAINTENANCE)
156
    {
157
        $this->disableMaintenanceAfterResponse = true;
158
        $this->maintenanceMode = $mode;
159
    }
160
161
    /**
162
     * メンテナンスモードの状態を判定する
163
     *
164
     * @return Bool
165
     */
166
    public function isMaintenanceMode()
167
    {
168
        // .maintenanceが存在しているかチェック
169
        return file_exists($this->container->getParameter('eccube_content_maintenance_file_path'));
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public static function getSubscribedEvents()
176
    {
177
        return [KernelEvents::TERMINATE => 'disableMaintenanceEvent'];
178
    }
179
}
180