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

SchemaConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 74
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 3 1
A getConnection() 0 3 1
A getDirName() 0 3 1
A getValidator() 0 7 1
A getExcludes() 0 3 1
A __construct() 0 7 1
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