FakeDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * This file is part of the bee4/events package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author Stephane HULARD <[email protected]>
9
 * @package Bee4\Test
10
 */
11
12
namespace Bee4\Test;
13
14
use Bee4\Events\DispatcherInterface;
15
use Bee4\Events\EventInterface;
16
17
/**
18
 * A basic dispatcher implementation that can be used for testing purpose
19
 * @package Bee4\Test
20
 */
21
class FakeDispatcher implements DispatcherInterface
22
{
23
    /**
24
     * Listener collection
25
     * @var array
26
     */
27
    protected $listeners = [];
28
29
    /**
30
     * @param string $name
31
     * @param EventInterface $event
32
     * @return EventInterface
33
     */
34
    public function dispatch($name, EventInterface $event)
35
    {
36
        if (isset($this->listeners[$name])) {
37
            foreach ($this->listeners[$name] as $priority) {
38
                foreach ($priority as $callable) {
39
                    call_user_func($callable, $event, $name, $this);
40
                }
41
            }
42
        }
43
44
        return $event;
45
    }
46
47
    /**
48
     * Add a listener for the given event
49
     * @param string $name
50
     * @param Callable $listener
51
     * @param int $priority
52
     * @return DispatcherInterface
53
     */
54
    public function add($name, callable $listener, $priority = 0)
55
    {
56
        $this->listeners[$name][$priority][] = $listener;
57
        sort($this->listeners[$name]);
58
        return $this;
59
    }
60
61
    public function remove($name, callable $listener)
62
    {
63
    }
64
    public function get($name)
65
    {
66
    }
67
}
68