Passed
Pull Request — master (#1647)
by
11:06
created

Dispatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 46.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 48
ccs 6
cts 13
cp 0.4615
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A __call() 0 7 2
A addListener() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\EventDispatcher;
13
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
16
/**
17
 * Class Dispatcher.
18
 *
19
 * @author mingyoung <[email protected]>
20
 *
21
 * @method string|null dispatch(object $event)
22
 */
23
class Dispatcher
24
{
25
    /**
26
     * @var \Symfony\Component\EventDispatcher\EventDispatcher
27
     */
28
    protected $dispatcher;
29
30
    /**
31
     * Dispatcher constructor.
32
     */
33 292
    public function __construct()
34
    {
35 292
        $this->dispatcher = class_exists(EventDispatcher::class) ? new EventDispatcher() : null;
36 292
    }
37
38
    /**
39
     * Adds an event listener that listens on the specified events.
40
     *
41
     * @param string   $eventName The event to listen on
42
     * @param callable $listener  The listener
43
     * @param int      $priority  The higher this value, the earlier an event
44
     *                            listener will be triggered in the chain (defaults to 0)
45
     */
46
    public function addListener($eventName, $listener, $priority = 0)
47
    {
48
        // Makes callable.
49
        if (is_string($listener)) {
50
            $listener = new $listener();
51
        }
52
53
        if ($this->dispatcher) {
54
            $this->dispatcher->addListener($eventName, $listener, $priority);
55
        }
56
    }
57
58
    /**
59
     * @param string $name
60
     * @param array  $args
61
     *
62
     * @return mixed
63
     */
64 292
    public function __call($name, $args)
65
    {
66 292
        if (!$this->dispatcher) {
67
            return;
68
        }
69
70 292
        return call_user_func_array([$this->dispatcher, $name], $args);
71
    }
72
}
73