Completed
Push — master ( 535bcb...cacb64 )
by mw
33:49 queued 32:14
created

HookRegistry   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.11%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 141
ccs 52
cts 53
cp 0.9811
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isRegistered() 0 3 1
A getHandlerFor() 0 3 2
A addCallbackHandlers() 0 74 3
A clear() 0 5 2
A register() 0 5 2
1
<?php
2
3
namespace SUC;
4
5
use Hooks;
6
use MWNamespace;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.0
11
 *
12
 * @author mwjames
13
 */
14
class HookRegistry {
15
16
	/**
17
	 * @var array
18
	 */
19
	private $handlers = array();
20
21
	/**
22
	 * @var Options
23
	 */
24
	private $options;
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @param Options $options
30
	 */
31 2
	public function __construct( Options $options ) {
32 2
		$this->options = $options;
33
34 2
		$this->addCallbackHandlers(
35 2
			$this->options
36 2
		);
37 2
	}
38
39
	/**
40
	 * @since  1.0
41
	 *
42
	 * @param string $name
43
	 *
44
	 * @return boolean
45
	 */
46 1
	public function isRegistered( $name ) {
47 1
		return Hooks::isRegistered( $name );
48
	}
49
50
	/**
51
	 * @since  1.0
52
	 */
53 1
	public function clear() {
54 1
		foreach ( $this->handlers as $name => $callback ) {
55 1
			Hooks::clear( $name );
56 1
		}
57 1
	}
58
59
	/**
60
	 * @since  1.0
61
	 *
62
	 * @param string $name
63
	 *
64
	 * @return Callable|false
65
	 */
66 1
	public function getHandlerFor( $name ) {
67 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
68
	}
69
70
	/**
71
	 * @since  1.0
72
	 */
73 1
	public function register() {
74 1
		foreach ( $this->handlers as $name => $callback ) {
75 1
			Hooks::register( $name, $callback );
76 1
		}
77 1
	}
78
79 2
	private function addCallbackHandlers( $options ) {
0 ignored issues
show
Coding Style introduced by
addCallbackHandlers 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...
80
81
		/**
82
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
83
		 */
84 2
		$this->handlers['BeforePageDisplay'] = function ( &$outputPage, &$skin ) {
0 ignored issues
show
Unused Code introduced by
The parameter $skin 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...
85
86 1
			$outputPage->addModuleStyles( 'ext.summary.cards.styles' );
87
88 1
			$outputPage->addModules(
89
				array(
90
					'ext.summary.cards'
91 1
				)
92 1
			);
93
94 1
			return true;
95
		};
96
97
		/**
98
		 * Hook: NewRevisionFromEditComplete called when a revision was inserted
99
		 * due to an edit
100
		 *
101
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
102
		 */
103
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage, $revision, $baseId, $user ) use( $options ) {
0 ignored issues
show
Unused Code introduced by
The parameter $revision 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...
Unused Code introduced by
The parameter $baseId 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...
Unused Code introduced by
The parameter $user 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...
104 1
			return CacheHelper::newFromOptions( $options )->invalidateCache( $wikiPage->getTitle() );
105
		};
106
107
		/**
108
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
109
		 */
110 2
		$this->handlers['GetPreferences'] = function ( $user, &$preferences ) {
111
112
			// Option to enable tooltip info
113 1
			$preferences['suc-tooltip-disabled'] = array(
114 1
				'type'          => 'toggle',
115 1
				'label-message' => 'suc-tooltip-disabled',
116 1
				'section'       => 'rendering/suc-options',
117
			);
118
119 1
			return true;
120
		};
121
122
		/**
123
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
124
		 */
125
		$this->handlers['ResourceLoaderGetConfigVars'] = function ( &$vars ) use ( $options ) {
126
127 1
			$contentLanguage = $GLOBALS['wgContLang'];
128 1
			$enabledNamespaceWithTemplate = array();
129 1
			$namespacesByContentLanguage = array();
130
131 1
			foreach ( $options->get( 'enabledNamespaceWithTemplate' ) as $ns => $template ) {
132
				$enabledNamespaceWithTemplate[$contentLanguage->getNsText( $ns )] = $template;
133 1
			}
134
135
			// Get literals to match and split when comparing href's during the
136
			// JS parse process
137 1
			foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
138 1
				$namespacesByContentLanguage[$contentLanguage->getNsText( $ns )] = $name;
139 1
			}
140
141 1
			$vars['ext.summaryCards.config'] = array(
142 1
				'tooltipRequestCacheTTL'          => $options->get( 'tooltipRequestCacheTTL' ),
143 1
				'cachePrefix'                     => $options->get( 'cachePrefix' ),
144 1
				'enabledForAnonUsers'             => $options->get( 'enabledForAnonUsers' ),
145 1
				'enabledNamespaceWithTemplate'    => $enabledNamespaceWithTemplate,
146 1
				'namespacesByContentLanguage'     => $namespacesByContentLanguage,
147 1
				'namespacesByCanonicalIdentifier' => array_flip( $namespacesByContentLanguage )
148 1
			);
149
150 1
			return true;
151
		};
152 2
	}
153
154
}
155