HookRegistry::getHandlerFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 1
ccs 2
cts 2
cp 1
crap 2
1
<?php
2
3
namespace WNBY;
4
5
use Hooks;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 *
11
 * @author mwjames
12
 */
13
class HookRegistry {
14
15
	/**
16
	 * @var array
17
	 */
18
	private $handlers = array();
19
20
	/**
21
	 * @since 1.0
22
	 *
23
	 * @param array $configuration
24
	 */
25 2
	public function __construct( $configuration = array () ) {
26 2
		$this->registerCallbackHandlers( $configuration );
27 2
	}
28
29
	/**
30
	 * @since  1.0
31
	 */
32 1
	public function register() {
33 1
		foreach ( $this->handlers as $name => $callback ) {
34 1
			Hooks::register( $name, $callback );
35 1
		}
36 1
	}
37
38
	/**
39
	 * @since  1.0
40
	 */
41 1
	public function deregister() {
42 1
		foreach ( array_keys( $this->handlers ) as $name ) {
43 1
			Hooks::clear( $name );
44 1
		}
45 1
	}
46
47
	/**
48
	 * @since  1.0
49
	 *
50
	 * @param string $name
51
	 *
52
	 * @return Callable|false
53
	 */
54 1
	public function getHandlerFor( $name ) {
55 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
56
	}
57
58
	/**
59
	 * @since  1.0
60
	 *
61
	 * @param string $name
62
	 *
63
	 * @return boolean
64
	 */
65 1
	public function isRegistered( $name ) {
66 1
		return Hooks::isRegistered( $name );
67
	}
68
69 2
	private function registerCallbackHandlers( $configuration ) {
0 ignored issues
show
Unused Code introduced by
The parameter $configuration is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
71
		/**
72
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
73
		 */
74 2
		$this->handlers['ParserFirstCallInit'] = function ( &$parser ) {
75
76 1
			$parserFunctionFactory = new ParserFunctionFactory();
77
78 1
			list( $name, $definition, $flag ) = $parserFunctionFactory->newNearbyParserFunctionDefinition();
79
80 1
			$parser->setFunctionHook( $name, $definition, $flag );
81
82 1
			return true;
83
		};
84
85
		/**
86
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
87
		 */
88 2
		$this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) {
89
90 1
			$vars['whats-nearby'] = array(
91 1
				'wgCachePrefix'  => $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'],
92 1
				'wnbyExternalGeoipService' => $GLOBALS['wnbyExternalGeoipService']
93 1
			);
94
95 1
			return true;
96
		};
97
98
		/**
99
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
100
		 */
101 2
		$this->handlers['OutputPageParserOutput'] = function ( $out, $parserOutput ) {
102
103
			// If we know that geoip is not required then don't add it
104 1
			if ( $parserOutput->getExtensionData( 'wnby-geoip' ) === null ||
105 1
				$parserOutput->getExtensionData( 'wnby-geoip' ) === false ) {
106
				return true;
107
			}
108
109
			// Copied from the ULS extension
110 1
			if ( is_string( $GLOBALS['wnbyExternalGeoipService'] ) ) {
111
				$out->addModules( 'ext.whats.nearby.geoip' );
112 1
			} elseif ( $GLOBALS['wnbyExternalGeoipService'] === true ) {
113 1
				$out->addScript( '<script src="//meta.wikimedia.org/geoiplookup"></script>' );
114 1
			}
115
116 1
			return true;
117
		};
118 2
	}
119
120
}
121