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

JObserverMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 64
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addObserverClassToClass() 0 9 2
A attachAllObservers() 0 21 4
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 mapping pattern implementation for Joomla
14
 *
15
 * @since  3.1.2
16
 */
17
class JObserverMapper
18
{
19
	/**
20
	 * Array: array( JObservableInterface_classname => array( JObserverInterface_classname => array( paramname => param, .... ) ) )
21
	 *
22
	 * @var    array
23
	 * @since  3.1.2
24
	 */
25
	protected static $observations = array();
26
27
	/**
28
	 * Adds a mapping to observe $observerClass subjects with $observableClass observer/listener, attaching it on creation with $params
29
	 * on $observableClass instance creations
30
	 *
31
	 * @param   string         $observerClass    The name of the observer class (implementing JObserverInterface)
32
	 * @param   string         $observableClass  The name of the observable class (implementing JObservableInterface)
33
	 * @param   array|boolean  $params           The params to give to the JObserverInterface::createObserver() function, or false to remove mapping
34
	 *
35
	 * @return  void
36
	 *
37
	 * @since   3.1.2
38
	 */
39
	public static function addObserverClassToClass($observerClass, $observableClass, $params = array())
40
	{
41
		if ($params !== false)
42
		{
43
			static::$observations[$observableClass][$observerClass] = $params;
44
		}
45
		else
46
		{
47
			unset(static::$observations[$observableClass][$observerClass]);
48
		}
49
	}
50
51
	/**
52
	 * Attaches all applicable observers to an $observableObject
53
	 *
54
	 * @param   JObservableInterface  $observableObject  The observable subject object
55
	 *
56
	 * @return  void
57
	 *
58
	 * @since   3.1.2
59
	 */
60
	public static function attachAllObservers(JObservableInterface $observableObject)
61
	{
62
		$observableClass = get_class($observableObject);
63
64
		while ($observableClass != false)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $observableClass of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
65
		{
66
			// Attach applicable Observers for the class to the Observable subject:
67
			if (isset(static::$observations[$observableClass]))
68
			{
69
				foreach (static::$observations[$observableClass] as $observerClass => $params)
70
				{
71
					// Attach an Observer to the Observable subject:
72
					/**
73
					 * @var JObserverInterface $observerClass
74
					 */
75
					$observerClass::createObserver($observableObject, $params);
76
				}
77
			}
78
79
			// Get parent class name (or false if none), and redo the above on it:
80
			$observableClass = get_parent_class($observableClass);
81
		}
82
	}
83
}
84