Completed
Push — 4.0 ( 16398e...769a6f )
by chihiro
25s queued 10s
created

SystemService::switchMaintenance()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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