Completed
Pull Request — 4.0 (#4000)
by
unknown
06:23
created

SystemService::canSetMemoryLimit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 12
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
    /**
23
     * @var EntityManagerInterface
24
     */
25
    protected $entityManager;
26
27
    /**
28
     * @var ContainerInterface
29
     */
30
    protected $container;
31
32 3
    /**
33
     * SystemService constructor.
34 3
     *
35
     * @param EntityManagerInterface $entityManager
36
     * @param ContainerInterface $container
37
     */
38
    public function __construct(
39
        EntityManagerInterface $entityManager,
40
        ContainerInterface $container
41
    ) {
42 3
        $this->entityManager = $entityManager;
43
        $this->container = $container;
44 3
    }
45 3
46
    /**
47 3
     * get DB version
48 3
     *
49
     * @return string
50 3
     */
51 3
    public function getDbversion()
52 3
    {
53
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
54
        $rsm->addScalarResult('v', 'v');
55
56
        $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName();
57
        switch ($platform) {
58
            case 'sqlite':
59
                $prefix = 'SQLite version ';
60
                $func = 'sqlite_version()';
61
                break;
62
63
            case 'mysql':
64
                $prefix = 'MySQL ';
65 3
                $func = 'version()';
66 3
                break;
67 3
68
            case 'pgsql':
69 3
            default:
70
                $prefix = '';
71
                $func = 'version()';
72
        }
73
74
        $version = $this->entityManager
75
            ->createNativeQuery('select '.$func.' as v', $rsm)
76
            ->getSingleScalarResult();
77
78
        return $prefix.$version;
79
    }
80
81
    /**
82
     * Try to set new values memory_limit | return true
83
     *
84
     * @param string $memory | EX: 1536M
85
     *
86
     * @return bool
87
     */
88
    public function canSetMemoryLimit($memory)
89
    {
90
        try {
91
            $ret = ini_set('memory_limit', $memory);
92
        } catch (\Exception $exception) {
93
            return false;
94
        }
95
96
        return ($ret === false) ? false : true;
97
    }
98
99
    /**
100
     * Get memory_limit | Megabyte
101
     *
102
     * @return float|int
103
     */
104
    public function getMemoryLimit()
105
    {
106
        // Data type: bytes
107
        $memoryLimit = (new MemoryDataCollector())->getMemoryLimit();
108
        if (-1 == $memoryLimit) {
109
            return -1;
110
        }
111
112
        return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024;
113
    }
114
115
    /**
116
     * メンテナンスモードを切り替える
117
     *
118
     * @param Bool $isEnable
119
     */
120
    public function switchMaintenance($isEnable = false)
121
    {
122
        $isMaintenanceMode = $this->isMaintenanceMode();
123
        $path = $this->container->getParameter('eccube_content_maintenance_file_path');
124
125
        if ($isEnable && $isMaintenanceMode == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
126
            touch($path);
127
        } elseif ($isEnable == false && $isMaintenanceMode) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
128
            unlink($path);
129
        }
130
131
    }
132
133
    /**
134
     * メンテナンスモードの状態を判定する
135
     *
136
     * @return Bool
137
     */
138
    public function isMaintenanceMode()
139
    {
140
        // .maintenanceが存在しているかチェック
141
        return file_exists($this->container->getParameter('eccube_content_maintenance_file_path'));
142
    }
143
144
}
145