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

LocalInputSource   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
c 0
b 0
f 0
dl 0
loc 57
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 7
A toArray() 0 19 4
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
}