Passed
Push — master ( 2e6646...d21bed )
by Alec
02:36
created

System::inContainer()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Helpers\Classes;
4
5
/**
6
 * Class System
7
 *
8
 * @internal
9
 */
10
final class System
11
{
12
    protected const CGROUP_FILE = '/proc/1/cgroup';
13
    protected const CGROUP_PATTERN = '(lxc|docker|kubepods)';
14
15
    /**
16
     * Determine if run in container
17
     *
18
     * @return bool
19
     *
20
     * @link https://stackoverflow.com/a/20012536
21
     */
22 2
    public static function inContainer(): bool
23
    {
24 2
        if (file_exists(self::CGROUP_FILE)
25 2
            && ($content = file_get_contents(self::CGROUP_FILE))
26 2
            && preg_match(self::CGROUP_PATTERN, $content)) {
27
            return true;
28
        }
29 2
        return false;
30
    }
31
}
32