Completed
Push — master ( 613187...0057b2 )
by Marek
06:22
created

EventListenerConfig   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 0
loc 113
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A event() 0 4 1
A listenerServiceId() 0 4 1
A action() 0 4 1
A priority() 0 4 1
A setEvent() 0 9 2
A setListenerServiceId() 0 9 2
A setAction() 0 9 2
A createFromConfigArray() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pvg\Application\Configuration\ValueObject;
6
7
class EventListenerConfig
8
{
9
    /** @var string */
10
    private $event;
11
12
    /** @var string */
13
    private $listenerServiceId;
14
15
    /** @var string */
16
    private $action;
17
18
    /** @var int */
19
    private $priority;
20
21
    /**
22
     * @throws \ReflectionException
23
     * @throws \InvalidArgumentException
24
     */
25
    public function __construct(
26
        string $event,
27
        string $listenerServiceId,
28
        string $action,
29
        int $priority = 0
30
    ) {
31
        $this
32
            ->setEvent($event)
33
            ->setListenerServiceId($listenerServiceId)
34
            ->setAction($action);
35
        $this->priority = $priority;
36
    }
37
38
    public function event() : string
39
    {
40
        return $this->event;
41
    }
42
43
    public function listenerServiceId() : string
44
    {
45
        return $this->listenerServiceId;
46
    }
47
48
    public function action() : string
49
    {
50
        return $this->action;
51
    }
52
53
    public function priority() : int
54
    {
55
        return $this->priority;
56
    }
57
58
    /**
59
     * @throws \InvalidArgumentException
60
     */
61
    private function setEvent(string $event) : self
62
    {
63
        if (empty($event)) {
64
            throw new \InvalidArgumentException('Event name cannot be empty');
65
        }
66
        $this->event = $event;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @throws \InvalidArgumentException
73
     */
74
    private function setListenerServiceId(string $listenerServiceId) : self
75
    {
76
        if (empty($listenerServiceId)) {
77
            throw new \InvalidArgumentException('Service ID (from service container) cannot be empty!');
78
        }
79
        $this->listenerServiceId = $listenerServiceId;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @throws \InvalidArgumentException
86
     * @throws \ReflectionException
87
     */
88
    private function setAction(string $action) : self
89
    {
90
        if (empty($action)) {
91
            throw new \InvalidArgumentException('Service listener method cannot be empty');
92
        }
93
        $this->action = $action;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return static[]
100
     *
101
     * @throws \ReflectionException
102
     * @throws \InvalidArgumentException
103
     */
104
    public static function createFromConfigArray(array $configArray) : array
105
    {
106
        $result = [];
107
        foreach ($configArray as $event => $listeners) {
108
            if (empty($listeners)) {
109
                continue;
110
            }
111
            foreach ($listeners as $listener) {
112
                $priority = $listener['priority'] ?? 0;
113
                $result[] = new static($event, $listener['service'], $listener['action'], $priority);
114
            }
115
        }
116
117
        return $result;
118
    }
119
}
120