Test Failed
Pull Request — master (#18)
by Alex
16:59 queued 14:19
created

ListenerConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 22
c 2
b 0
f 0
dl 0
loc 100
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriority() 0 3 1
A getEventName() 0 3 1
A toArray() 0 6 1
A setPriority() 0 3 1
A comparePriority() 0 3 1
A __construct() 0 5 1
A getListener() 0 3 1
A fromArray() 0 12 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Listener;
6
7
/**
8
 * @author  Alex Patterson <[email protected]>
9
 * @package Arp\EventDispatcher\Listener
10
 */
11
class ListenerConfig
12
{
13
    /**
14
     * @var callable
15
     */
16
    protected $listener;
17
18
    /**
19
     * @var string
20
     */
21
    protected $eventName;
22
23
    /**
24
     * @var int
25
     */
26
    protected $priority;
27
28
    /**
29
     * @param string   $eventName
30
     * @param callable $listener
31
     * @param int      $priority
32
     */
33
    public function __construct(string $eventName, callable $listener, int $priority = 1)
34
    {
35
        $this->eventName = $eventName;
36
        $this->listener = $listener;
37
        $this->priority = $priority;
38
    }
39
40
    /**
41
     * @return callable
42
     */
43
    public function getListener(): callable
44
    {
45
        return $this->listener;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getEventName(): string
52
    {
53
        return $this->eventName;
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function getPriority(): int
60
    {
61
        return $this->priority;
62
    }
63
64
    /**
65
     * @param int $priority
66
     */
67
    public function setPriority(int $priority): void
68
    {
69
        $this->priority = $priority;
70
    }
71
72
    /**
73
     * Compare the priority with another listener config instance.
74
     *
75
     * @param ListenerConfig $config
76
     *
77
     * @return int
78
     */
79
    public function comparePriority(ListenerConfig $config): int
80
    {
81
        return ($this->priority <=> $config->getPriority());
82
    }
83
84
    /**
85
     * @param array $data
86
     */
87
    public function fromArray(array $data): void
88
    {
89
        if (isset($data['listener']) && is_callable($data['listener'])) {
90
            $this->listener = $data['listener'];
91
        }
92
93
        if (isset($data['event_name']) && is_string($data['event_name'])) {
94
            $this->eventName = $data['event_name'];
95
        }
96
97
        if (isset($data['priority'])) {
98
            $this->setPriority($data['priority']);
99
        }
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function toArray(): array
106
    {
107
        return [
108
            'listener' => $this->listener,
109
            'event_name' => $this->eventName,
110
            'priority' => $this->priority,
111
        ];
112
    }
113
}
114