ConfigurationValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 3

2 Methods

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