Passed
Pull Request — master (#1)
by Harry
02:40
created

SchemaConfig::getDirName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Sprout\Config;
4
5
use Graze\ConfigValidation\ConfigValidatorInterface;
6
use Graze\ConfigValidation\Validate;
7
use Respect\Validation\Validator as v;
8
9
class SchemaConfig implements SchemaConfigInterface
10
{
11
    const CONFIG_SCHEMA     = 'schema';
12
    const CONFIG_EXCLUDE    = 'exclude';
13
    const CONFIG_CONNECTION = 'connection';
14
    const CONFIG_DIR_NAME   = 'dirName';
15
16
    /** @var string */
17
    private $schema;
18
    /** @var string[] */
19
    private $exclude;
20
    /** @var ConnectionConfigInterface */
21
    private $connection;
22
    /** @var string */
23
    private $dirName;
24
25
    /**
26
     * SchemaConfig constructor.
27
     *
28
     * @param array $options Array of options
29
     *
30
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
31
     */
32
    public function __construct(array $options = [])
33
    {
34
        $options = static::getValidator()->validate($options);
35
        $this->schema = $options[static::CONFIG_SCHEMA];
36
        $this->exclude = $options[static::CONFIG_EXCLUDE];
37
        $this->connection = new ConnectionConfig($options[static::CONFIG_CONNECTION]);
38
        $this->dirName = $options[static::CONFIG_DIR_NAME] ?? $this->schema;
0 ignored issues
show
introduced by
Nested ternary operator is not allowed
Loading history...
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getSchema(): string
45
    {
46
        return $this->schema;
47
    }
48
49
    /**
50
     * @return ConnectionConfigInterface
51
     */
52
    public function getConnection(): ConnectionConfigInterface
53
    {
54
        return $this->connection;
55
    }
56
57
    /**
58
     * @return ConfigValidatorInterface
59
     */
60
    public static function getValidator(): ConfigValidatorInterface
61
    {
62
        return Validate::arr(false)
63
                       ->addChild(static::CONFIG_CONNECTION, ConnectionConfig::getValidator())
64
                       ->optional(static::CONFIG_SCHEMA, v::stringType())
65
                       ->optional(static::CONFIG_EXCLUDE, v::arrayVal()->each(v::stringType()), [])
66
                       ->optional(static::CONFIG_DIR_NAME, v::stringType());
67
    }
68
69
    /**
70
     * @return string[]
71
     */
72
    public function getExcludes(): array
73
    {
74
        return $this->exclude;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getDirName(): string
81
    {
82
        return $this->dirName;
83
    }
84
}
85