Completed
Push — master ( 57a73d...51c7de )
by Alexey
38:40
created

EventManager::addEvent()   A

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\QueueBundle\Base\JobInterface;
6
use SfCod\QueueBundle\Exception\JobNotFoundException;
7
use SfCod\SocketIoBundle\Events\EventInterface;
8
use SfCod\SocketIoBundle\Exception\EventDuplicateException;
9
use SfCod\SocketIoBundle\Exception\EventNotFoundException;
10
11
/**
12
 * Class EventManager
13
 *
14
 * @author Virchenko Maksim <[email protected]>
15
 *
16
 * @package SfCod\SocketIoBundle\Service
17
 */
18
class EventManager
19
{
20
    /**
21
     * List with all events
22
     *
23
     * @var array
24
     */
25
    protected $list = [];
26
    /**
27
     * Array of events
28
     *
29
     * @var array
30
     */
31
    protected $namespaces;
32
    /**
33
     * Project root directory
34
     *
35
     * @var string
36
     */
37
    protected $rootDir;
38
39
    /**
40
     * EventManager constructor.
41
     *
42
     * @param string $rootDir
43
     * @param array $namespaces
44
     */
45
    public function __construct(string $rootDir, array $namespaces = [])
46
    {
47
        $this->rootDir = $rootDir;
48
        $this->namespaces = $namespaces;
49
    }
50
51
    /**
52
     * Get events list
53
     *
54
     * @return array
55
     */
56
    public function getList(): array
57
    {
58
        //@todo remove this, move to extension using tags
59
        if (empty(self::$list)) {
60
            foreach ($this->namespaces as $key => $namespace) {
61
                $alias = $this->rootDir . '/../' . str_replace('\\', DIRECTORY_SEPARATOR, trim($namespace, '\\'));
62
63
                foreach (glob(sprintf('%s/**.php', $alias)) as $file) {
64
                    $className = sprintf('%s\%s', $namespace, basename($file, '.php'));
65
                    if (method_exists($className, 'name')) {
66
                        $this->list[$className::name()] = $className;
67
                    }
68
                }
69
            }
70
        }
71
72
        return $this->list;
73
    }
74
75
    /**
76
     * Resolve the given class.
77
     *
78
     * @param string $name
79
     *
80
     * @return EventInterface
81
     */
82
    public function resolve(string $name): EventInterface
83
    {
84
        if (isset($this->list[$name])) {
85
            return $this->list[$name];
86
        }
87
88
        throw new EventNotFoundException("Event handler '$name' not found.");
89
    }
90
91
    /**
92
     * @inheritDoc
93
     *
94
     * @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...
95
     * @param EventInterface $event
96
     */
97
    public function addEvent(EventInterface $event)
98
    {
99
        if (isset($this->list[$event::name()])) {
100
            throw new EventDuplicateException("Event '$name' already exists.");
101
        }
102
103
        $this->list[$event::name()] = $event;
104
    }
105
}
106