StaticEventDispatcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 16 2
A setEventManager() 0 4 1
A getEventManager() 0 8 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Event
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Event;
16
17
use Phossa2\Event\Message\Message;
18
use Phossa2\Shared\Base\StaticAbstract;
19
use Phossa2\Event\Exception\BadMethodCallException;
20
use Phossa2\Event\Interfaces\EventManagerInterface;
21
22
/**
23
 * StaticEventDispatcher
24
 *
25
 * A static wrapper of EventDispatcher
26
 *
27
 * ```php
28
 * // attach
29
 * StaticEventDispatcher::attach('test', function() { echo 'test'; });
30
 *
31
 * // trigger
32
 * StaticEventDispatcher::trigger('test');
33
 * ```
34
 *
35
 * @package Phossa2\Event
36
 * @author  Hong Zhang <[email protected]>
37
 * @version 2.1.0
38
 * @since   2.0.0 added
39
 * @since   2.1.0 updated
40
 */
41
class StaticEventDispatcher extends StaticAbstract
42
{
43
    /**
44
     * slave event manager
45
     *
46
     * @var    EventManagerInterface[]
47
     * @access protected
48
     * @staticvar
49
     */
50
    protected static $event_manager = [];
51
52
    /**
53
     * Provides a static interface for event dispatcher's dynamic methods
54
     *
55
     * @param  string $name method name
56
     * @param  array $arguments arguments
57
     * @return mixed
58
     * @throws BadMethodCallException if method not found
59
     * @access public
60
     * @static
61
     * @internal
62
     */
63
    public static function __callStatic($name, array $arguments)
64
    {
65
        $mgr = static::getEventManager();
66
        if (method_exists($mgr, $name)) {
67
            return call_user_func_array([$mgr, $name], $arguments);
68
        }
69
70
        throw new BadMethodCallException(
71
            Message::get(
72
                Message::MSG_METHOD_NOTFOUND,
73
                $name,
74
                get_called_class()
75
            ),
76
            Message::MSG_METHOD_NOTFOUND
77
        );
78
    }
79
80
    /**
81
     * Set the inner event manager
82
     *
83
     * @param  EventManagerInterface $eventManager
84
     * @access public
85
     * @api
86
     * @static
87
     */
88
    public static function setEventManager(EventManagerInterface $eventManager)
89
    {
90
        self::$event_manager[get_called_class()] = $eventManager;
91
    }
92
93
    /**
94
     * Get the inner event manager
95
     *
96
     * @return EventManagerInterface $eventManager
97
     * @access public
98
     * @api
99
     * @static
100
     */
101
    public static function getEventManager()
102
    {
103
        if (!isset(self::$event_manager[get_called_class()])) {
104
            self::$event_manager[get_called_class()] =
105
                EventDispatcher::getShareable('__STATIC__');
106
        }
107
        return self::$event_manager[get_called_class()];
108
    }
109
}
110