Passed
Push — master ( d8387a...cbc754 )
by Sébastien
03:10
created

PlatformNullFile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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