PlatformNullFile   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 25
c 1
b 0
f 1
dl 0
loc 59
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentPlatform() 0 4 2
A getNullFile() 0 11 3
A getFile() 0 3 1
A __construct() 0 13 3
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