Completed
Pull Request — experimental/3.1 (#2640)
by
unknown
79:35 queued 38:52
created

SystemService::getPHP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service;
25
26
use Doctrine\ORM\EntityManager;
27
use Eccube\Annotation\Inject;
28
use Eccube\Annotation\Service;
29
use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
30
use Symfony\Component\Process\PhpExecutableFinder;
31
32
/**
33
 * @Service
34
 */
35
class SystemService
36
{
37
    /**
38
     * @var EntityManager
39
     * @Inject("orm.em")
40
     */
41 3
    protected $em;
42
43
    /**
44 3
     * @Inject("config")
45 3
     * @var array
46
     */
47 3
    protected $appConfig;
48
49 3
    public function getDbversion()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
50
    {
51
52
        $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
53
        $rsm->addScalarResult('v', 'v');
54 3
55
        $platform = $this->em->getConnection()->getDatabasePlatform()->getName();
56
        switch ($platform) {
57
            case 'sqlite':
58
                $prefix = 'SQLite version ';
59 3
                $func = 'sqlite_version()';
60
                break;
61 3
62 3
            case 'mysql':
63
                $prefix = 'MySQL ';
64
                $func = 'version()';
65 3
                break;
66 3
67 3
            case 'pgsql':
68
            default:
69 3
                $prefix = '';
70
                $func = 'version()';
71
        }
72
73
        $version = $this->em
74
            ->createNativeQuery('select '.$func.' as v', $rsm)
75
            ->getSingleScalarResult();
76
77
        return $prefix.$version;
78
    }
79
80
    /**
81
     * Get environment php command
82
     * @return string
83
     */
84
    public function getPHP()
85
    {
86
        return (new PhpExecutableFinder())->find();
87
    }
88
89
    /**
90
     * Try to set new values memory_limit | return true
91
     * @return bool
92
     */
93
    public function isSetMemoryLimit()
94
    {
95
        $setMemory = $this->appConfig['composer_memory_limit'].'M';
96
97
        try {
98
            ini_set('memory_limit', $setMemory);
99
        } catch (\Exception $exception) {
100
            return false;
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * Get memory_limit | Megabyte
108
     * @return float|int
109
     */
110
    public function getMemoryLimit()
111
    {
112
        // Data type: bytes
113
        $memoryLimit = (new MemoryDataCollector())->getMemoryLimit();
114
        if (-1 == $memoryLimit) {
115
            return -1;
116
        }
117
118
        return ($memoryLimit == 0) ? 0 : ($memoryLimit / 1024) / 1024;
119
    }
120
}
121