EventService::advise()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 12
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
/**
23
 * EventService class
24
 *
25
 * Messaging Subsystem.  Notifies objects when events occur in the system
26
 *
27
 * <code>
28
 * $eventService = new xhelp_eventService();
29
 * // Call $obj->callback($args) when a new ticket is created
30
 * $eventService->advise('new_ticket', $obj, 'callback');
31
 * // Call $obj2->somefunction($args) when a new ticket is created
32
 * $eventService->advise('new_ticket', $obj2, 'somefunction');
33
 * // .. Code to create new ticket
34
 * $eventService->trigger('new_ticket', $new_ticketobj);
35
 * </code>
36
 *
37
 * @author  Brian Wahoff <[email protected]>
38
 */
39
class EventService
40
{
41
    /**
42
     * Array of all function callbacks
43
     *
44
     * @var array
45
     */
46
    public $_ctx = [];
47
48
    /**
49
     * Class Constructor
50
     */
51
    public function __construct()
52
    {
53
        //Do Nothing
54
    }
55
56
    /**
57
     * Add a new class function to be notified
58
     * @param string $context  Event used for callback
59
     * @param string $callback Function to call when event is fired. If only object is supplied, look for function with same name as context
60
     * @param int    $priority Order that callback should be triggered
61
     * @return int      Event cookie, used for unadvise
62
     */
63
    public function advise(string $context, string $callback, int $priority = 10): int
64
    {
65
        $clbk = $callback;
66
        if (!\is_array($callback) && \is_object($callback)) {
0 ignored issues
show
introduced by
The condition is_array($callback) is always false.
Loading history...
introduced by
The condition is_object($callback) is always false.
Loading history...
67
            $clbk = [$callback, $context];
68
        }
69
70
        //Add Element to notification list
71
        $this->_ctx[$context][(string)$priority][] = $clbk;
72
73
        //Return element # in array
74
        return \count($this->_ctx[$context][(string)$priority]) - 1;
75
    }
76
77
    /**
78
     * Remove a class function from the notification list
79
     * @param string $context Event used for callback
80
     * @param int    $cookie  The Event ID returned by xhelp_eventService::advise()
81
     * @param int    $priority
82
     */
83
    public function unadvise(string $context, int $cookie, int $priority = 10): void
84
    {
85
        $this->_ctx[$context][(string)$priority][$cookie] = false;
86
    }
87
88
    /**
89
     * Only have 1 instance of class used
90
     * @return EventService {@link EventService}
91
     */
92
    public static function getInstance(): EventService
93
    {
94
        static $instance;
95
        if (null === $instance) {
96
            $instance = new static();
97
        }
98
99
        return $instance;
100
    }
101
102
    /**
103
     * Tell subscribed objects an event occurred in the system
104
     * @param string $context Event raised by the system
105
     * @param array  $args    Any arguments that need to be passed to the callback functions
106
     */
107
    public function trigger(string $context, array $args): void
108
    {
109
        if (isset($this->_ctx[$context])) {
110
            \ksort($this->_ctx[$context]);
111
            $_notlist = $this->_ctx[$context];
112
            foreach ($_notlist as $priority => $functions) {
113
                foreach ($functions as $func) {
114
                    if (\is_callable($func, true, $func_name)) {
115
                        \call_user_func_array($func, $args);
116
                    }
117
                }
118
            }
119
        }
120
    }
121
}
122