Passed
Push — master ( a91cfe...d8387a )
by Sébastien
02:25
created

PlatformNullFile::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Common\IO;
6
7
class PlatformNullFile
8
{
9
    public const PLATFORM_LINUX = 'LINUX';
10
    public const PLATFORM_WIN   = 'WINDOWS';
11
12
    public const SUPPORTED_PLATFORMS = [
13
        self::PLATFORM_LINUX,
14
        self::PLATFORM_WIN,
15
    ];
16
17
    /** @var string $platform if null platform wil be auto detected */
18
    protected $platform;
19
20
    /**
21
     * @param null|string $platform if null platform will be autodetected
22
     *
23
     * @throws \InvalidArgumentException
24
     */
25 2
    public function __construct(?string $platform = null)
26
    {
27 2
        if ($platform === null) {
28 1
            $platform = self::getCurrentPlatform();
29
        } else {
30 1
            if (!in_array(mb_strtoupper($platform), self::SUPPORTED_PLATFORMS, true)) {
31
                throw new \InvalidArgumentException(sprintf(
32
                    'Platform \'%s\' is not supported',
33
                    $platform
34
                ));
35
            }
36
        }
37 2
        $this->platform = mb_strtoupper($platform);
38 2
    }
39
40 1
    public static function getCurrentPlatform(): string
41
    {
42 1
        return defined('PHP_WINDOWS_VERSION_MAJOR')
43 1
                    ? self::PLATFORM_WIN : self::PLATFORM_LINUX;
44
    }
45
46
    /**
47
     * Return /dev/null on linux/unix/mac or NUL on windows.
48
     */
49 2
    public function getNullFile(): string
50
    {
51 2
        switch ($this->platform) {
52 2
            case self::PLATFORM_WIN:
53 1
                return 'NUL';
54
            // All others for now
55 2
            case self::PLATFORM_LINUX:
56
            default:
57 2
                return '/dev/null';
58
        }
59
    }
60
}
61