Passed
Push — master ( bf86e6...5ea322 )
by Sébastien
07:21
created

PlatformNullFile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentPlatform() 0 4 2
A getNullFile() 0 9 3
A __construct() 0 13 3
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
    public function __construct(?string $platform = null)
26
    {
27
        if ($platform === null) {
28
            $platform = self::getCurrentPlatform();
29
        } else {
30
            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
        $this->platform = mb_strtoupper($platform);
38
    }
39
40
    public static function getCurrentPlatform(): string
41
    {
42
        return defined('PHP_WINDOWS_VERSION_MAJOR')
43
                    ? self::PLATFORM_WIN : self::PLATFORM_LINUX;
44
    }
45
46
    public function getNullFile(): string
47
    {
48
        switch ($this->platform) {
49
            case self::PLATFORM_WIN:
50
                return 'NUL';
51
            // All others for now
52
            case self::PLATFORM_LINUX:
53
            default:
54
                return '/dev/null';
55
        }
56
    }
57
}
58