Passed
Pull Request — master (#38)
by Teye
05:43
created

LocalInputSource::__construct()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8333
cc 7
nc 3
nop 3
crap 7
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\InputSources;
5
6
use InvalidArgumentException;
7
8
class LocalInputSource implements InputSourceInterface
9
{
10
    protected ?string $baseDir;
11
12
    /**
13
     * @var string[]
14
     */
15
    protected array $files;
16
17
    protected ?string $filter;
18
19
    /**
20
     * @param string[]    $files
21
     * @param string|null $baseDir
22
     * @param string|null $filter
23
     */
24 8
    public function __construct(array $files = [], ?string $baseDir = null, ?string $filter = null)
25
    {
26 8
        if (empty($baseDir) && count($files) == 0) {
27 1
            throw new InvalidArgumentException('You have to specify either $baseDir or $files');
28
        }
29
30
        if (
31 7
            (!empty($baseDir) && empty($filter))
32
            ||
33 7
            (empty($baseDir) && !empty($filter))
34
        ) {
35 2
            throw new InvalidArgumentException('You have to specify both $filter and $baseDir to make use of these!');
36
        }
37
38 5
        $this->baseDir = $baseDir;
39 5
        $this->filter  = $filter;
40 5
        $this->files   = $files;
41
    }
42
43
    /**
44
     * @return array<string,string|string[]>
45
     */
46 4
    public function toArray(): array
47
    {
48 4
        $response = [
49
            'type' => 'local',
50
        ];
51
52 4
        if (count($this->files) > 0) {
53 3
            $response['files'] = $this->files;
54
        }
55
56 4
        if (!empty($this->baseDir)) {
57 2
            $response['baseDir'] = $this->baseDir;
58
        }
59
60 4
        if (!empty($this->filter)) {
61 2
            $response['filter'] = $this->filter;
62
        }
63
64 4
        return $response;
65
    }
66
}