Sources::determineInput()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 8.8333
cc 7
nc 7
nop 2
crap 7
1
<?php
2
3
namespace kalanis\kw_clipr\Clipr;
4
5
6
use kalanis\kw_clipr\Interfaces\ISources;
7
use kalanis\kw_clipr\Output;
8
use kalanis\kw_input\Interfaces\IEntry;
9
10
11
/**
12
 * Class Sources
13
 * @package kalanis\kw_clipr\Clipr
14
 * @todo: idea - split dependency on sourceType - entryTypes and output to extra classes
15
 */
16
class Sources
17
{
18
    protected string $sourceType = '';
19
20 14
    public function determineInput(bool $isWeb = false, bool $noColor = false): self
21
    {
22 14
        if ($noColor) {
23 3
            $this->sourceType = ISources::SOURCE_CLEAR;
24 11
        } elseif ($isWeb) {
25 4
            $this->sourceType = ISources::SOURCE_WEB;
26
        } else {
27 7
            switch ($this->getFamily()) {
28 7
                case 'Windows':
29 1
                    $this->sourceType = ISources::SOURCE_WINDOWS;
30 1
                    break;
31 6
                case 'BSD':
32 5
                case 'Linux':
33 2
                case 'Solaris':
34 4
                    $this->sourceType = ISources::SOURCE_POSIX;
35 4
                    break;
36
                default:
37 2
                    $this->sourceType = ISources::SOURCE_CLEAR;
38
            }
39
        }
40 14
        return $this;
41
    }
42
43
    /**
44
     * nicked from PHPUnit
45
     * @return string
46
     * @codeCoverageIgnore
47
     */
48
    protected function getFamily(): string
49
    {
50
        if (\defined('PHP_OS_FAMILY')) {
51
            return \PHP_OS_FAMILY;
52
        }
53
54
        if (\DIRECTORY_SEPARATOR === '\\') {
55
            return 'Windows';
56
        }
57
58
        switch (\PHP_OS) {
59
            case 'Darwin':
60
                return 'Darwin';
61
62
            case 'DragonFly':
63
            case 'FreeBSD':
64
            case 'NetBSD':
65
            case 'OpenBSD':
66
                return 'BSD';
67
68
            case 'Linux':
69
                return 'Linux';
70
71
            case 'SunOS':
72
                return 'Solaris';
73
74
            default:
75
                return 'Unknown';
76
        }
77
    }
78
79
    /**
80
     * @return string[]
81
     */
82 9
    public function getEntryTypes(): array
83
    {
84 9
        switch ($this->sourceType) {
85 9
            case ISources::SOURCE_WEB:
86 3
                return [IEntry::SOURCE_GET, IEntry::SOURCE_POST, IEntry::SOURCE_FILES];
87 6
            case ISources::SOURCE_POSIX:
88 3
            case ISources::SOURCE_WINDOWS:
89 3
            case ISources::SOURCE_CLEAR:
90
            default:
91 6
                return [IEntry::SOURCE_CLI, IEntry::SOURCE_FILES];
92
        }
93
    }
94
95 6
    public function getOutput(): Output\AOutput
96
    {
97 6
        switch ($this->sourceType) {
98 6
            case ISources::SOURCE_WEB:
99 1
                return new Output\Web();
100 5
            case ISources::SOURCE_POSIX:
101 1
                return new Output\Posix();
102 4
            case ISources::SOURCE_WINDOWS:
103 1
                return new Output\Windows();
104 3
            case ISources::SOURCE_CLEAR:
105
            default:
106 3
                return new Output\Clear();
107
        }
108
    }
109
}
110