Passed
Push — master ( 676110...9cfebc )
by Daniel
02:12
created

ConfigurationValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Dandelion\Configuration;
4
5
use Dandelion\Exception\ConfigurationNotValidException;
6
use Swaggest\JsonSchema\InvalidValue;
7
use Swaggest\JsonSchema\SchemaContract;
8
9
use function json_decode;
10
11
class ConfigurationValidator implements ConfigurationValidatorInterface
12
{
13
    /**
14
     * @var \Dandelion\Configuration\ConfigurationLoaderInterface
15
     */
16
    protected $configurationLoader;
17
18
    /**
19
     * @var \Swaggest\JsonSchema\SchemaContract
20
     */
21
    protected $jsonSchema;
22
23
    /**
24
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
25
     * @param \Swaggest\JsonSchema\SchemaContract $jsonSchema
26
     */
27
    public function __construct(
28
        ConfigurationLoaderInterface $configurationLoader,
29
        SchemaContract $jsonSchema
30
    ) {
31
        $this->configurationLoader = $configurationLoader;
32
        $this->jsonSchema = $jsonSchema;
33
    }
34
35
    /**
36
     * @return \Dandelion\Configuration\ConfigurationValidatorInterface
37
     *
38
     * @throws \Dandelion\Exception\ConfigurationNotValidException
39
     */
40
    public function validate(): ConfigurationValidatorInterface
41
    {
42
        $configurationFileContent = $this->configurationLoader->loadRaw();
43
44
        try {
45
            $this->jsonSchema->in(json_decode($configurationFileContent));
46
        } catch (InvalidValue $e) {
47
            throw new ConfigurationNotValidException($e->getMessage());
48
        }
49
50
        return $this;
51
    }
52
}
53