ManualEventSubscriber::getListenersForEvent()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Event\EventSubscriber;
7
8
9
use Gica\Cqrs\Event\EventSubscriber;
10
11
class ManualEventSubscriber implements EventSubscriber
12
{
13
    private $eventListeners = [];
14
15 1
    public function subscribeToEvent($listener)
16
    {
17 1
        $this->eventListeners[] = $listener;
18 1
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23 5
    public function getListenersForEvent($event)
24
    {
25 5
        $result = [];
26
27 5
        foreach ($this->eventListeners as $eventListener) {
28 1
            $methodName = $this->getMethodName($event);
29
30 1
            $method = [$eventListener, $methodName];
31
32 1
            if (is_callable($method)) {
33 1
                $result[] = $method;
34
            }
35
        }
36
37 5
        return $result;
38
    }
39
40 1
    private function getMethodName($event)
41
    {
42 1
        $parts = explode('\\', get_class($event));
43
44 1
        return 'handle' . end($parts);
45
    }
46
47
}