Completed
Push — master ( 5635b0...3b50b8 )
by Christian
02:38
created

Config::get()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 10
cp 0
rs 8.439
cc 5
eloc 20
nc 8
nop 2
crap 30
1
<?php
2
3
namespace uuf6429\ElderBrother;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
use uuf6429\ElderBrother\Action\ActionAbstract;
8
9
class Config
10
{
11
    /**
12
     * @var string[]
13
     */
14
    protected $paths;
15
16
    /**
17
     * @var LoggerInterface
18
     */
19
    protected $logger;
20
21
    /**
22
     * @var array
23
     */
24
    protected $config;
25
26
    /**
27
     * @param string[]             $paths
28
     * @param LoggerInterface|null $logger
29
     */
30
    public function __construct($paths, LoggerInterface $logger = null)
31
    {
32
        $this->paths = $paths;
33
        $this->logger = $logger ?: new NullLogger();
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getAll()
40
    {
41
        if (is_null($this->config)) {
42
            $this->load();
43
        }
44
45
        return (array) $this->config;
46
    }
47
48
    /**
49
     * @param string $event
50
     * @param bool   $supportedOnly
51
     *
52
     * @return ActionAbstract[]
53
     */
54
    public function get($event, $supportedOnly = true)
55
    {
56
        if (is_null($this->config)) {
57
            $this->load();
58
        }
59
60
        $config = isset($this->config[$event])
61
            ? array_values($this->config[$event]) : [];
62
63
        if ($supportedOnly) {
64
            $config = array_filter(
65
                $config,
66
                function (ActionAbstract $action) {
67
                    try {
68
                        $action->checkSupport();
69
70
                        return true;
71
                    } catch (\Exception $ex) {
72
                        $this->logger->warning(
73
                            sprintf(
74
                                '%s is not supported: %s.',
75
                                $action->getName(),
76
                                $ex->getMessage()
77
                            )
78
                        );
79
                    }
80
81
                    return false;
82
                }
83
            );
84
        }
85
86
        return $config;
87
    }
88
89
    /**
90
     * @return LoggerInterface
91
     */
92
    public function getLog()
93
    {
94
        return $this->logger;
95
    }
96
97
    /**
98
     * @return self
99
     */
100
    protected function load()
101
    {
102
        $this->config = [];
103
104
        $this->logger->debug('Loading configuration...');
105
106
        foreach ($this->paths as $path) {
107
            $this->loadFile($path);
108
        }
109
110
        $this->initActions();
111
112
        $this->logger->debug('Configuration loaded.');
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $path
119
     */
120
    private function loadFile($path)
121
    {
122
        if (file_exists($path)) {
123
            $this->logger->debug('Loading config file: ' . $path);
124
125
            $config = include $path;
126
127
            foreach ($config as $event => $prioritizedActions) {
128
                // merge config
129
                $this->config[$event] = array_merge(
130
                    isset($this->config[$event]) ? $this->config[$event] : [],
131
                    $prioritizedActions
132
                );
133
134
                // reorder actions
135
                ksort($this->config[$event]);
136
            }
137
        } else {
138
            $this->logger->debug('Config file does not exist: ' . $path);
139
        }
140
    }
141
142
    private function initActions()
143
    {
144
        foreach ($this->config as $actionList) {
145
            foreach ($actionList as $action) {
146
                if ($action instanceof ActionAbstract) {
147
                    $action->setConfig($this);
148
                }
149
            }
150
        }
151
    }
152
}
153