CallbacksListenerAggregate   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C attach() 0 29 7
1
<?php
2
/**
3
 * @link     https://github.com/nnx-framework/zf2-test-toolkit
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\ZF2TestToolkit\Listener;
7
8
use Zend\EventManager\AbstractListenerAggregate;
9
use Zend\EventManager\EventManagerInterface;
10
11
/**
12
 * Class CallbacksListenerAggregate
13
 *
14
 * @package Nnx\ZF2TestToolkit\Listener
15
 */
16
class CallbacksListenerAggregate extends AbstractListenerAggregate
17
{
18
19
    /**
20
     * Карта событий
21
     *
22
     * @var array
23
     */
24
    protected $eventMap = [];
25
26
    /**
27
     * CallbacksListenerAggregate constructor.
28
     *
29
     * @param array $eventMap
30
     */
31
    public function __construct(array $eventMap = [])
32
    {
33
        $this->eventMap = $eventMap;
34
    }
35
36
37
    /**
38
     * @param EventManagerInterface $events
39
     *
40
     * @throws \Nnx\ZF2TestToolkit\Listener\Exception\RuntimeException
41
     */
42
    public function attach(EventManagerInterface $events)
43
    {
44
        foreach ($this->eventMap as $eventName => $items) {
45
            if (!is_array($items)) {
46
                $errMsg = 'Handlers config not array';
47
                throw new Exception\RuntimeException($errMsg);
48
            }
49
50
            foreach ($items as $item) {
51
                if (!is_array($item)) {
52
                    $errMsg = 'Handler not array';
53
                    throw new Exception\RuntimeException($errMsg);
54
                }
55
56
                if (!array_key_exists('handler', $item)) {
57
                    $errMsg = 'Handler not found';
58
                    throw new Exception\RuntimeException($errMsg);
59
                }
60
                $handler = $item['handler'];
61
62
                $priority = 1;
63
                if (array_key_exists('priority', $item)) {
64
                    $priority = (integer)$priority;
65
                }
66
67
                $this->listeners[] = $events->attach($eventName, $handler, $priority);
68
            }
69
        }
70
    }
71
}
72