Completed
Push — master ( d372d6...3eec25 )
by Christian
02:37
created

Config::setUpAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
2
3
namespace uuf6429\ElderBrother\Config;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerInterface;
7
use uuf6429\ElderBrother\Action\ActionAbstract;
8
9
class Config implements ConfigInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $config = [];
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function loadFromFile($fileName, LoggerInterface $logger)
20
    {
21 1
        $eventActions = include $fileName;
22 1
        $this->loadFromArray($eventActions, $logger);
23 1
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 4
    public function loadFromArray($array, LoggerInterface $logger)
29
    {
30
        // merge config and set up actions
31 4
        foreach ($array as $event => $prioritizedActions) {
32 4
            foreach ($prioritizedActions as $key => $action) {
33 3
                $this->setUpAction($action, $logger);
34 4
                $this->config[$event][$key] = $action;
35
            }
36
        }
37
38
        // reorder actions by index
39 4
        array_map('ksort', $this->config);
40 4
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function getAllEventActions()
46
    {
47 1
        return (array) $this->config;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4
    public function getActionsForEvent($event, $supportedOnly = true)
54
    {
55 4
        $config = isset($this->config[$event])
56 4
            ? array_values($this->config[$event]) : [];
57
58 4
        if ($supportedOnly) {
59 4
            $config = array_filter(
60
                $config,
61 4
                function (ActionAbstract $action) {
62 3
                    return $action->isSupported();
63 4
                }
64
            );
65
        }
66
67 4
        return $config;
68
    }
69
70
    /**
71
     * @param ActionAbstract  $action
72
     * @param LoggerInterface $logger
73
     */
74 3
    private function setUpAction(ActionAbstract $action, LoggerInterface $logger)
75
    {
76 3
        if ($action instanceof ConfigAwareInterface) {
77 3
            $action->setConfig($this);
78
        }
79
80 3
        if ($action instanceof LoggerAwareInterface) {
81 3
            $action->setLogger($logger);
82
        }
83 3
    }
84
}
85