Ajde_Event   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 6 1
A unregister() 0 10 2
A has() 0 10 3
C trigger() 0 61 18
A className() 0 9 4
1
<?php
2
3
class Ajde_Event extends Ajde_Object_Static
4
{
5
    protected static $eventStack = [];
6
7
    /**
8
     * Register a callback on an object event. Note that when an object instance
9
     * is passed, only the classname is used. Thus, an Ajde_Event can not be used
10
     * to trigger different callbacks for the same event on different object instances
11
     * of the same class.
12
     *
13
     * @param mixed  $object   Object instance or classname triggering the event
14
     * @param string $event    Event name
15
     * @param mixed  $callback Callback
16
     *
17
     * @return bool true
18
     */
19
    public static function register($object, $event, $callback)
20
    {
21
        self::$eventStack[self::className($object)][$event][] = $callback;
22
23
        return true;
24
    }
25
26
    public static function unregister($object, $event)
27
    {
28
        if (isset(self::$eventStack[self::className($object)][$event])) {
29
            unset(self::$eventStack[self::className($object)][$event]);
30
31
            return true;
32
        }
33
34
        return false;
35
    }
36
37
    public static function has($object, $event, $callback = null)
38
    {
39
        if (isset($callback)) {
40
            return
41
                isset(self::$eventStack[self::className($object)][$event]) &&
42
                in_array($callback, self::$eventStack[self::className($object)][$event]);
43
        } else {
44
            return isset(self::$eventStack[self::className($object)][$event]);
45
        }
46
    }
47
48
    public static function trigger($object, $event, array $parameters = [])
49
    {
50
        foreach (self::$eventStack as $className => $eventStack) {
51
            if (self::className($object) == $className ||
52
                is_subclass_of(self::className($object), $className)
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $className can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
53
            ) {
54
                if (isset($eventStack[$event])) {
55
                    foreach ($eventStack[$event] as $eventCallback) {
56
                        $retval = null;
0 ignored issues
show
Unused Code introduced by
$retval is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
                        $callback = null;
58
                        if (is_callable($eventCallback)) {
59
                            $callback = $eventCallback;
60
                        } elseif (is_string($eventCallback)) {
61
                            if (is_callable([$object, $eventCallback])) {
62
                                $callback = [$object, $eventCallback];
63
                            }
64
                        }
65
                        if (isset($callback)) {
66
                            $trace = debug_backtrace();
67
                            $current = array_shift($trace);
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
                            $caller = array_shift($trace);
69
70
                            if (isset($caller['object'])) {
71
                                // http://www.php.net/manual/en/function.call-user-func.php
72
                                // Note: Note that the parameters for call_user_func() are not passed by reference.
73
                                $parameterArray = array_merge([&$caller['object']], $parameters);
74
                                $retval = call_user_func_array($callback, $parameterArray);
75
                                if (isset($retval)) {
76
                                    // TODO: Aborts execution of cueue!
77
                                    return $retval;
78
                                }
79
                            } elseif (isset($caller['class']) && isset($caller['function']) && $caller['type'] === '::') {
80
                                // triggered from static context
81
                                // TODO: update exception 90015
82
                                $retval = call_user_func_array($callback, $parameters);
83
                                if (isset($retval)) {
84
                                    // TODO: Aborts execution of cueue!
85
                                    return $retval;
86
                                }
87
                            } elseif ($callback instanceof Closure) {
88
                                $retval = $callback();
89
                                if (isset($retval)) {
90
                                    // TODO: Aborts execution of cueue!
91
                                    return $retval;
92
                                }
93
                            } else {
94
                                throw new Ajde_Exception('Event context needed to
95
									fire, none detected', 90015);
96
                            }
97
                        } else {
98
                            // TODO: right now never fires in Object_Magic objects
99
                            // because of the __call magic function. Workaround
100
                            // could be something like in_array("bar",get_class_methods($f1)
101
                            // see: http://php.net/manual/en/function.method-exists.php
102
                            throw new Ajde_Exception('Callback is not valid', 90016);
103
                        }
104
                    }
105
                }
106
            }
107
        }
108
    }
109
110
    protected static function className($object)
111
    {
112
        if (is_object($object)) {
113
            return get_class($object);
114
        } elseif (is_string($object) && class_exists($object)) {
115
            return $object;
116
        }
117
        throw new Ajde_Exception('No classname or object instance given, or classname is incorrect', 90012);
118
    }
119
}
120