Completed
Push — 4.0 ( 103006...239d44 )
by Ryo
05:55
created

SystemService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 48.28%

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
ccs 14
cts 29
cp 0.4828
wmc 11
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDbversion() 0 29 4
A canSetMemoryLimit() 0 10 3
A getMemoryLimit() 0 10 3
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 3
    {
33
        $this->entityManager = $entityManager;
34 3
    }
35
36
    /**
37
     * get DB version
38
     *
39
     * @return string
40
     */
41
    public function getDbversion()
42 3
    {
43
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
44 3
        $rsm->addScalarResult('v', 'v');
45 3
46
        $platform = $this->entityManager->getConnection()->getDatabasePlatform()->getName();
47 3
        switch ($platform) {
48 3
            case 'sqlite':
49
                $prefix = 'SQLite version ';
50 3
                $func = 'sqlite_version()';
51 3
                break;
52 3
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 3
            ->createNativeQuery('select '.$func.' as v', $rsm)
66 3
            ->getSingleScalarResult();
67 3
68
        return $prefix.$version;
69 3
    }
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