ConfigReader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 88
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A init() 0 9 2
A get() 0 8 2
A getYamlFileContent() 0 7 1
A initEventDispatcher() 0 13 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Configuration;
6
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
11
use Symfony\Component\Yaml\Yaml;
12
13
class ConfigReader
14
{
15
    /** @var FileLocator */
16
    private $fileLocator;
17
18
    /** @var string */
19
    private $configPath;
20
21
    /** @var array */
22
    private $configArray;
23
24
    /** @var LoggerInterface */
25
    private $logger;
26
27
    /** @var string */
28
    private $eventListenerConfigPath;
29
30
    /** @var ContainerAwareEventDispatcher */
31
    private $dispatcher;
32
33
    /**
34
     * Create an global application config from given file.
35
     */
36
    public function __construct(
37
        FileLocator $fileLocator,
38
        string $applicationConfigPath,
39
        string $eventListenerConfigPath,
40
        ContainerAwareEventDispatcher $dispatcher,
41
        LoggerInterface $logger
42
    ) {
43
        $this->fileLocator             = $fileLocator;
44
        $this->configPath              = $applicationConfigPath;
45
        $this->logger                  = $logger;
46
        $this->eventListenerConfigPath = $eventListenerConfigPath;
47
        $this->dispatcher              = $dispatcher;
48
    }
49
50
    public function init() : void
51
    {
52
        if (empty($this->configArray)) {
53
            $this->configArray = $this->getYamlFileContent($this->configPath);
54
        }
55
56
        $eventListenerConfig = $this->getYamlFileContent($this->eventListenerConfigPath);
57
        $this->initEventDispatcher(EventListenerConfig::createFromConfigArray($eventListenerConfig));
58
    }
59
60
    public function get() : array
61
    {
62
        if (empty($this->configArray)) {
63
            throw new InvalidConfigurationException('Invalid configuration: empty');
64
        }
65
66
        return $this->configArray;
67
    }
68
69
    /**
70
     * @throws \Symfony\Component\Config\Exception\FileLocatorFileNotFoundException
71
     * @throws \Symfony\Component\Yaml\Exception\ParseException
72
     * @throws \InvalidArgumentException
73
     */
74
    private function getYamlFileContent(string $path) : array
75
    {
76
        $fullPath = $this->fileLocator->locate($path);
77
        $this->logger->info('Reading config from {path}', ['path' => $fullPath]);
78
79
        return Yaml::parse(file_get_contents($fullPath));
80
    }
81
82
    /**
83
     * @param EventListenerConfig[] $eventListenerConfig
84
     *
85
     * @throws \InvalidArgumentException
86
     */
87
    private function initEventDispatcher(array $eventListenerConfig) : void
88
    {
89
        /** @var EventListenerConfig $singleListenerConfig */
90
        foreach ($eventListenerConfig as $singleListenerConfig) {
91
            $this->dispatcher->addListenerService(
92
                $singleListenerConfig->event(),
93
                [
94
                    $singleListenerConfig->listenerServiceId(),
95
                    $singleListenerConfig->action(),
96
                ]
97
            );
98
        }
99
    }
100
}
101