EventManager::addEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SfCod\SocketIoBundle\Service;
4
5
use SfCod\SocketIoBundle\Events\EventInterface;
6
use SfCod\SocketIoBundle\Exception\EventDuplicateException;
7
use SfCod\SocketIoBundle\Exception\EventNotFoundException;
8
9
/**
10
 * Class EventManager.
11
 *
12
 * @author Virchenko Maksim <[email protected]>
13
 *
14
 * @package SfCod\SocketIoBundle\Service
15
 */
16
class EventManager
17
{
18
    /**
19
     * List with all events.
20
     *
21
     * @var array
22
     */
23
    protected $list = [];
24
    /**
25
     * Array of events.
26
     *
27
     * @var array
28
     */
29
    protected $namespaces;
30
    /**
31
     * Project root directory.
32
     *
33
     * @var string
34
     */
35
    protected $rootDir;
36
37
    /**
38
     * EventManager constructor.
39
     */
40
    public function __construct(string $rootDir, array $namespaces = [])
41
    {
42
        $this->rootDir = $rootDir;
43
        $this->namespaces = $namespaces;
44
    }
45
46
    /**
47
     * Get events list.
48
     */
49
    public function getList(): array
50
    {
51
        //@todo remove this, move to extension using tags
52
        if (empty(self::$list)) {
53
            foreach ($this->namespaces as $key => $namespace) {
54
                $alias = $this->rootDir . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\'));
55
56
                foreach (glob(sprintf('%s/**.php', $alias)) as $file) {
57
                    $className = sprintf('%s\%s', $namespace, basename($file, '.php'));
58
                    if (method_exists($className, 'name')) {
59
                        $this->list[$className::name()] = $className;
60
                    }
61
                }
62
            }
63
        }
64
65
        return $this->list;
66
    }
67
68
    /**
69
     * Resolve the given class.
70
     */
71
    public function resolve(string $name): EventInterface
72
    {
73
        if (isset($this->list[$name])) {
74
            return $this->list[$name];
75
        }
76
77
        throw new EventNotFoundException("Event handler '$name' not found.");
78
    }
79
80
    /**
81
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     */
83
    public function addEvent(EventInterface $event)
84
    {
85
        if (isset($this->list[$event::name()])) {
86
            throw new EventDuplicateException("Event '{$event::name()}' already exists.");
87
        }
88
89
        $this->list[$event::name()] = $event;
90
    }
91
}
92