Passed
Pull Request — master (#1633)
by mingyoung
02:52
created

Dispatcher::addListener()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\EventDispatcher\EventDispatcher was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Class Dispatcher.
18
 *
19
 * @author mingyoung <[email protected]>
20
 */
21
class Dispatcher
22
{
23
    /**
24
     * @var \Symfony\Component\EventDispatcher\EventDispatcher
25
     */
26
    protected $dispatcher;
27
28
    /**
29
     * Dispatcher constructor.
30
     */
31 292
    public function __construct()
32
    {
33 292
        $this->dispatcher = class_exists(EventDispatcher::class) ? new EventDispatcher() : null;
34 292
    }
35
36
    /**
37
     * Adds an event listener that listens on the specified events.
38
     *
39
     * @param string   $eventName The event to listen on
40
     * @param callable $listener  The listener
41
     * @param int      $priority  The higher this value, the earlier an event
42
     *                            listener will be triggered in the chain (defaults to 0)
43
     */
44
    public function addListener($eventName, $listener, $priority = 0)
45
    {
46
        // Makes callable.
47
        if (is_string($listener)) {
48
            $listener = new $listener();
49
        }
50
51
        return $this->__call('addListener', [$eventName, $listener, $priority]);
52
    }
53
54
    /**
55
     * @param string $name
56
     * @param array  $args
57
     *
58
     * @return mixed
59
     */
60 292
    public function __call($name, $args)
61
    {
62 292
        if (!$this->dispatcher) {
63 292
            return;
64
        }
65
66
        return call_user_func_array([$this->dispatcher, $name], $args);
67
    }
68
}
69