Completed
Push — master ( d1bd53...48f3de )
by mw
8s
created

HookRegistry   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 87
ccs 30
cts 30
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 5 2
A getHandlerFor() 0 3 2
A isRegistered() 0 3 1
B registerCallbackHandlers() 0 29 2
A deregister() 0 5 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...
Coding Style introduced by
registerCallbackHandlers uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
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
				'wgLanguageCode' => $GLOBALS['wgLang']->getCode()
93 1
			);
94
95 1
			return true;
96
		};
97 2
	}
98
99
}
100