Failed Conditions
Push — master ( 12954f...f40466 )
by Sébastien
02:10
created

PlatformNullFile::getFile()   A

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
namespace Soluble\MediaTools\Common\IO;
6
7
use Soluble\MediaTools\Common\Exception\InvalidArgumentException;
8
9
class PlatformNullFile implements UnescapedFileInterface
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 5
    public function __construct(?string $platform = null)
28
    {
29 5
        if ($platform === null) {
30 3
            $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 4
        $this->platform = mb_strtoupper($platform);
40 4
    }
41
42 3
    public static function getCurrentPlatform(): string
43
    {
44 3
        return defined('PHP_WINDOWS_VERSION_MAJOR')
45 3
                    ? self::PLATFORM_WIN : self::PLATFORM_LINUX;
46
    }
47
48
    /**
49
     * Return /dev/null on linux/unix/mac or NUL on windows.
50
     */
51 4
    public function getNullFile(?string $platform = null): string
52
    {
53 4
        $platform = $platform ?? $this->platform;
54
55
        switch ($platform) {
56 4
            case self::PLATFORM_WIN:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
57 1
                return 'NUL';
58
            // All others for now
59 4
            case self::PLATFORM_LINUX:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
60
            default:
61 4
                return '/dev/null';
62
        }
63
    }
64
65 3
    public function getFile(): string
66
    {
67 3
        return $this->getNullFile();
68
    }
69
}
70