Passed
Pull Request — patch_1-1-7 (#3404)
by
unknown
15:47
created

Event::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * An event.
5
 * In fact a container that holds a list of classes to be called when an event
6
 * is triggered
7
 *
8
 * @name      ElkArte Forum
9
 * @copyright ElkArte Forum contributors
10
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
11
 *
12
 * @version 1.1
13
 *
14
 */
15
16
/**
17
 * An event.
18
 * In fact a container that holds a list of classes to be called when an event
19
 * is triggered
20
 */
21
class Event
22
{
23
	/**
24
	 * If the classes have already been sorted by priority.
25
	 * @var bool
26
	 */
27
	protected $_sorted = false;
28
29
	/**
30
	 * List of classes.
31
	 * @var string[]
32
	 */
33
	protected $_events = array();
34
35
	/**
36
	 * The priority object.
37
	 * @var object
38
	 */
39
	protected $_priority = array();
40
41
	/**
42
	 * Initialize the class.
43
	 *
44
	 * @param Priority $priority the object that handles priorities
45
	 */
46
	public function __construct($priority)
47
	{
48
		$this->_priority = $priority;
49
	}
50
51
	/**
52
	 * Add a new event with a certain priority.
53
	 *
54
	 * @param mixed[] $event An array describing the event we want to trigger:
55
	 * 	array(
56
	 *     	0 => string - the position at which the event will be triggered
57
	 *      1 => string[] - the class and method we want to call:
58
	 *         	array(
59
	 *            	0 => string - name of the class to instantiate
60
	 *              1 => string - name of the method to call
61
	 *          )
62
	 *		2 => null|string[] - an array of dependencies in the form of strings representing the
63
	 *                           name of the variables the method requires.
64
	 *                           The variables can be from:
65
	 *                           - the default list of variables passed to the trigger
66
	 *                           - properties (private, protected, or public) of the object that
67
	 *                             instantiate the Event_Manager (i.e. the controller)
68
	 *                           - globals
69
	 *	)
70
	 * @param int $priority A value that defines the relative priority at which
71
	 *            the event should be triggered.
72
	 */
73
	public function add($event, $priority)
74
	{
75
		if (is_array($event[1]))
76
			$name = $event[1][0];
77
		else
78
			$name = $event[1];
79
80
		$this->_priority->add($name, $priority);
0 ignored issues
show
Bug Best Practice introduced by
The property _priority does not exist on Event. Did you maybe forget to declare it?
Loading history...
81
		$this->_events[$name] = $event;
82
	}
83
84
	/**
85
	 * Determines if there are events added or not.
86
	 *
87
	 * @return bool
88
	 */
89
	public function hasEvents()
90
	{
91
		if ($this->_sorted === false)
0 ignored issues
show
Bug Best Practice introduced by
The property _sorted does not exist on Event. Did you maybe forget to declare it?
Loading history...
92
		{
93
			$this->_doSorting();
0 ignored issues
show
Bug introduced by
The method _doSorting() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
			$this->/** @scrutinizer ignore-call */ 
94
          _doSorting();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
		}
95
96
		return $this->_priority->hasEntities();
0 ignored issues
show
Bug Best Practice introduced by
The property _priority does not exist on Event. Did you maybe forget to declare it?
Loading history...
97
	}
98
99
	protected function _doSorting()
100
	{
101
		$this->_priority->sort();
0 ignored issues
show
Bug Best Practice introduced by
The property _priority does not exist on Event. Did you maybe forget to declare it?
Loading history...
102
		$this->_sorted = true;
103
	}
104
105
	/**
106
	 * Returns the list of sorted events to be triggered.
107
	 *
108
	 * @return mixed[]
109
	 */
110
	public function getEvents()
111
	{
112
		$return = array();
113
		if ($this->_sorted === false)
0 ignored issues
show
Bug Best Practice introduced by
The property _sorted does not exist on Event. Did you maybe forget to declare it?
Loading history...
114
			$this->_doSorting();
115
116
		foreach ($this->_priority->getSortedEntities() as $value)
0 ignored issues
show
Bug Best Practice introduced by
The property _priority does not exist on Event. Did you maybe forget to declare it?
Loading history...
117
			$return[] = $this->_events[$value];
0 ignored issues
show
Bug Best Practice introduced by
The property _events does not exist on Event. Did you maybe forget to declare it?
Loading history...
118
119
		return $return;
120
	}
121
}