PlatformNullFile::getFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Common\IO;
13
14
use Soluble\MediaTools\Common\Exception\InvalidArgumentException;
15
16
final class PlatformNullFile implements UnescapedFileInterface
17
{
18
    public const PLATFORM_LINUX = 'LINUX';
19
    public const PLATFORM_WIN   = 'WINDOWS';
20
21
    public const SUPPORTED_PLATFORMS = [
22
        self::PLATFORM_LINUX,
23
        self::PLATFORM_WIN,
24
    ];
25
26
    /** @var string $platform if null platform wil be auto detected */
27
    private $platform;
28
29
    /**
30
     * @param null|string $platform if null platform will be autodetected
31
     *
32
     * @throws InvalidArgumentException
33
     */
34 7
    public function __construct(?string $platform = null)
35
    {
36 7
        if ($platform === null) {
37 5
            $platform = self::getCurrentPlatform();
38
        } else {
39 2
            if (!in_array(mb_strtoupper($platform), self::SUPPORTED_PLATFORMS, true)) {
40 1
                throw new InvalidArgumentException(sprintf(
41 1
                    'Platform \'%s\' is not supported',
42
                    $platform
43
                ));
44
            }
45
        }
46 6
        $this->platform = mb_strtoupper($platform);
47 6
    }
48
49 5
    public static function getCurrentPlatform(): string
50
    {
51 5
        return defined('PHP_WINDOWS_VERSION_MAJOR')
52 5
                    ? self::PLATFORM_WIN : self::PLATFORM_LINUX;
53
    }
54
55
    /**
56
     * Return /dev/null on linux/unix/mac or NUL on windows.
57
     */
58 6
    public function getNullFile(?string $platform = null): string
59
    {
60 6
        $platform = $platform ?? $this->platform;
61
62
        switch ($platform) {
63 6
            case self::PLATFORM_WIN:
64 1
                return 'NUL';
65
            // All others for now
66 6
            case self::PLATFORM_LINUX:
67
            default:
68 6
                return '/dev/null';
69
        }
70
    }
71
72 5
    public function getFile(): string
73
    {
74 5
        return $this->getNullFile();
75
    }
76
}
77