Completed
Push — dev/store-tests ( b538ec...90db53 )
by Kiyotaka
05:40
created

SystemService::getPHP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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\HttpKernel\DataCollector\MemoryDataCollector;
18
19
class SystemService
20
{
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    protected $entityManager;
25
26
    /**
27
     * SystemService constructor.
28
     *
29
     * @param EntityManagerInterface $entityManager
30
     */
31
    public function __construct(EntityManagerInterface $entityManager)
32
    {
33
        $this->entityManager = $entityManager;
34
    }
35
36
    /**
37
     * get DB version
38
     *
39
     * @return string
40
     */
41
    public function getDbversion()
42
    {
43
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
44
        $rsm->addScalarResult('v', 'v');
45
46
        $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName();
47
        switch ($platform) {
48
            case 'sqlite':
49
                $prefix = 'SQLite version ';
50
                $func = 'sqlite_version()';
51
                break;
52
53
            case 'mysql':
54
                $prefix = 'MySQL ';
55
                $func = 'version()';
56
                break;
57
58
            case 'pgsql':
59
            default:
60
                $prefix = '';
61
                $func = 'version()';
62
        }
63
64
        $version = $this->entityManager
65
            ->createNativeQuery('select '.$func.' as v', $rsm)
66
            ->getSingleScalarResult();
67
68
        return $prefix.$version;
69
    }
70
71
    /**
72
     * Try to set new values memory_limit | return true
73
     *
74
     * @param string $memory | EX: 1536M
75
     *
76
     * @return bool
77
     */
78
    public function canSetMemoryLimit($memory)
79
    {
80
        try {
81
            $ret = ini_set('memory_limit', $memory);
82
        } catch (\Exception $exception) {
83
            return false;
84
        }
85
86
        return ($ret === false) ? false : true;
87
    }
88
89
    /**
90
     * Get memory_limit | Megabyte
91
     *
92
     * @return float|int
93
     */
94
    public function getMemoryLimit()
95
    {
96
        // Data type: bytes
97
        $memoryLimit = (new MemoryDataCollector())->getMemoryLimit();
98
        if (-1 == $memoryLimit) {
99
            return -1;
100
        }
101
102
        return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024;
103
    }
104
}
105