Reader::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 3
crap 3
1
<?php
2
3
namespace SyncFS\Configuration;
4
5
use Symfony\Component\Config\Definition\ConfigurationInterface;
6
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
7
use Symfony\Component\Config\Definition\Processor;
8
use Symfony\Component\Yaml\Parser;
9
10
/**
11
 * Class Reader
12
 *
13
 * @package SyncFS\Configuration
14
 * @author  Matej Velikonja <[email protected]>
15
 */
16
class Reader
17
{
18
    /**
19
     * @var \Symfony\Component\Config\Definition\ConfigurationInterface
20
     */
21
    private $configuration;
22
23
    /**
24
     * @var \Symfony\Component\Yaml\Parser
25
     */
26
    private $parser;
27
28
    /**
29
     * @var \Symfony\Component\Config\Definition\Processor
30
     */
31
    private $processor;
32
33
    /**
34
     * @param ConfigurationInterface $configuration
35
     * @param Parser                 $parser
36
     * @param Processor              $processor
37
     */
38 5
    public function __construct(ConfigurationInterface $configuration, $parser = null, $processor = null)
39
    {
40 5
        if (! $parser) {
41 5
            $parser = new Parser();
42 5
        }
43
44 5
        if (! $processor) {
45 5
            $processor = new Processor();
46 5
        }
47
48 5
        $this->configuration = $configuration;
49 5
        $this->parser        = $parser;
50 5
        $this->processor     = $processor;
51 5
    }
52
53
    /**
54
     * @param string $configPath
55
     *
56
     * @throws ReaderException
57
     *
58
     * @return array
59
     */
60 5
    public function read($configPath)
61
    {
62 5
        if (!is_readable($configPath)) {
63 1
            throw new ReaderException(sprintf('Configuration file `%s` cannot be read.', $configPath));
64
        }
65
66 4
        $content = file_get_contents($configPath);
67 4
        $data    = $this->parser->parse($content);
68
69 4
        if (! is_array($data)) {
70
            throw new ReaderException('Parsing error.');
71
        }
72
73
        try {
74 4
            $config  = $this->processor->processConfiguration(
75 4
                $this->configuration,
76
                $data
77 4
            );
78 4
        } catch (InvalidConfigurationException $e) {
79 1
            throw new ReaderException($e->getMessage());
80
        }
81
82 3
        return $config;
83
    }
84
}
85