Completed
Push — master ( b5231e...2792e5 )
by Christian
02:36
created

Config::get()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
3
namespace uuf6429\ElderBrother;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
8
class Config
9
{
10
    /**
11
     * @var string[]
12
     */
13
    protected $paths;
14
15
    /**
16
     * @var LoggerInterface
17
     */
18
    protected $logger;
19
20
    /**
21
     * @var array
22
     */
23
    protected $config;
24
25
    /**
26
     * @param string[]        $paths
27
     * @param LoggerInterface $logger
0 ignored issues
show
Documentation introduced by
Should the type for parameter $logger not be null|LoggerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
28
     */
29
    public function __construct($paths, LoggerInterface $logger = null)
30
    {
31
        $this->paths = $paths;
32
        $this->logger = $logger ?: new NullLogger();
33
    }
34
35
    /**
36
     * @return self
37
     */
38
    public function load()
39
    {
40
        $this->config = [];
41
42
        foreach ($this->paths as $path) {
43
            if (file_exists($path)) {
44
                $this->logger->debug('Loading config file: ' . $path);
45
46
                $config = include $path;
47
48
                foreach ($config as $event => $prioritizedActions) {
49
                    // merge config
50
                    $this->config[$event] = array_merge(
51
                        isset($this->config[$event]) ? $this->config[$event] : [],
52
                        $prioritizedActions
53
                    );
54
55
                    // reorder actions
56
                    ksort($this->config[$event]);
57
                }
58
            } else {
59
                $this->logger->debug('Config file does not exist: ' . $path);
60
            }
61
        }
62
63
        $this->logger->debug('Configuration loaded.');
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getAll()
72
    {
73
        return $this->config;
74
    }
75
76
    /**
77
     * @param string $event
78
     * @param bool   $supportedOnly
79
     *
80
     * @return ActionInterface[]
81
     */
82
    public function get($event, $supportedOnly = true)
83
    {
84
        $config = isset($this->config[$event])
85
            ? array_values($this->config[$event]) : [];
86
87
        if ($supportedOnly) {
88
            $config = array_filter(
89
                $config,
90
                function (ActionInterface $action) {
91
                    try {
92
                        $action->checkSupport();
93
94
                        return true;
95
                    } catch (\Exception $ex) {
96
                        $this->logger->warning(
97
                            sprintf(
98
                                '%s is not supported: %s.',
99
                                $action->getName(),
100
                                $ex->getMessage()
101
                            )
102
                        );
103
                    }
104
                }
105
            );
106
        }
107
108
        return $config;
109
    }
110
}
111