Passed
Pull Request — develop (#912)
by Shandak
05:28
created

JObserverUpdater   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 30
c 1
b 0
f 0
dl 0
loc 163
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 11 4
A doCallObservers() 0 6 1
A attachObserver() 0 21 3
A getObserverOfClass() 0 15 3
A detachObserver() 0 12 3
A __construct() 0 2 1
1
<?php
2
/**
3
 * @package     Joomla.Platform
4
 * @subpackage  Observer
5
 *
6
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
7
 * @license     GNU General Public License version 2 or later; see LICENSE
8
 */
9
10
defined('JPATH_PLATFORM') or die;
11
12
/**
13
 * Observer updater pattern implementation for Joomla
14
 *
15
 * @since  3.1.2
16
 */
17
class JObserverUpdater implements JObserverUpdaterInterface
18
{
19
	/**
20
	 * Holds the key aliases for observers.
21
	 *
22
	 * @var    array
23
	 * @since  3.9.0
24
	 */
25
	protected $aliases = array();
26
27
	/**
28
	 * Generic JObserverInterface observers for this JObservableInterface
29
	 *
30
	 * @var    JObserverInterface
31
	 * @since  3.1.2
32
	 */
33
	protected $observers = array();
34
35
	/**
36
	 * Process observers (useful when a class extends significantly an observerved method, and calls observers itself
37
	 *
38
	 * @var    boolean
39
	 * @since  3.1.2
40
	 */
41
	protected $doCallObservers = true;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param   JObservableInterface  $observable  The observable subject object
47
	 *
48
	 * @since   3.1.2
49
	 */
50
	public function __construct(JObservableInterface $observable)
51
	{
52
		// Not yet needed, but possible:  $this->observable = $observable;
53
	}
54
55
	/**
56
	 * Adds an observer to the JObservableInterface instance updated by this
57
	 * This method can be called from JObservableInterface::attachObserver
58
	 *
59
	 * @param   JObserverInterface  $observer  The observer object
60
	 *
61
	 * @return  void
62
	 *
63
	 * @since   3.1.2
64
	 */
65
	public function attachObserver(JObserverInterface $observer)
66
	{
67
		$class = get_class($observer);
68
69
		// Also register the alias if exists
70
		foreach (JLoader::getDeprecatedAliases() as $alias)
71
		{
72
			$realClass  = trim($alias['new'], '\\');
73
74
			// Check if we have an alias for the observer class
75
			if ($realClass === $class)
76
			{
77
				$aliasClass = trim($alias['old'], '\\');
78
79
				// Add an alias to known aliases
80
				$this->aliases[$aliasClass] = $class;
81
			}
82
		}
83
84
		// Register the real class
85
		$this->observers[$class] = $observer;
86
	}
87
88
	/**
89
	 * Removes an observer from the JObservableInterface instance updated by this
90
	 * This method can be called from JObservableInterface::attachObserver
91
	 *
92
	 * @param   String  $observer  The observer class name
93
	 *
94
	 * @return  void
95
	 *
96
	 * @since   3.6.0
97
	 */
98
	public function detachObserver($observer)
99
	{
100
		$observer = trim($observer, '\\');
101
102
		if (isset($this->aliases[$observer]))
103
		{
104
			$observer = $this->aliases[$observer];
105
		}
106
107
		if (isset($this->observers[$observer]))
108
		{
109
			unset($this->observers[$observer]);
110
		}
111
	}
112
113
	/**
114
	 * Gets the instance of the observer of class $observerClass
115
	 *
116
	 * @param   string  $observerClass  The class name of the observer
117
	 *
118
	 * @return  JTableObserver|null  The observer object of this class if any
119
	 *
120
	 * @since   3.1.2
121
	 */
122
	public function getObserverOfClass($observerClass)
123
	{
124
		$observerClass = trim($observerClass, '\\');
125
126
		if (isset($this->aliases[$observerClass]))
127
		{
128
			$observerClass = $this->aliases[$observerClass];
129
		}
130
131
		if (isset($this->observers[$observerClass]))
132
		{
133
			return $this->observers[$observerClass];
134
		}
135
136
		return null;
137
	}
138
139
	/**
140
	 * Call all observers for $event with $params
141
	 *
142
	 * @param   string  $event   Name of the event
143
	 * @param   array   $params  Params of the event
144
	 *
145
	 * @return  void
146
	 *
147
	 * @since   3.1.2
148
	 */
149
	public function update($event, $params)
150
	{
151
		if ($this->doCallObservers)
152
		{
153
			foreach ($this->observers as $observer)
154
			{
155
				$eventListener = array($observer, $event);
156
157
				if (is_callable($eventListener))
158
				{
159
					call_user_func_array($eventListener, $params);
160
				}
161
			}
162
		}
163
	}
164
165
	/**
166
	 * Enable/Disable calling of observers (this is useful when calling parent:: function
167
	 *
168
	 * @param   boolean  $enabled  Enable (true) or Disable (false) the observer events
169
	 *
170
	 * @return  boolean  Returns old state
171
	 *
172
	 * @since   3.1.2
173
	 */
174
	public function doCallObservers($enabled)
175
	{
176
		$oldState = $this->doCallObservers;
177
		$this->doCallObservers = $enabled;
178
179
		return $oldState;
180
	}
181
}
182