HookRegistry::addCallbackHandlers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 7
cts 9
cp 0.7778
rs 9.1344
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0438
1
<?php
2
3
namespace SG;
4
5
use Hooks;
6
use MediaWiki\Linker\LinkTarget;
7
use SMW\DIWikiPage;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class HookRegistry {
16
17
	/**
18
	 * @var array
19
	 */
20
	private $handlers = array();
21
22
	/**
23
	 * @since 1.0
24 1
	 */
25 1
	public function __construct() {
26 1
		$this->addCallbackHandlers();
27
	}
28
29
	/**
30
	 * @since  1.1
31
	 *
32
	 * @param string $name
33
	 *
34
	 * @return boolean
35
	 */
36
	public function isRegistered( $name ) {
37
		return Hooks::isRegistered( $name );
38
	}
39
40
	/**
41
	 * @since  1.1
42
	 *
43
	 * @param string $name
44
	 *
45
	 * @return Callable|false
46
	 */
47
	public function getHandlerFor( $name ) {
48
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
49
	}
50
51
	/**
52
	 * @since  1.0
53
	 */
54
	public function register() {
55
		foreach ( $this->handlers as $name => $callback ) {
56
			Hooks::register( $name, $callback );
57
		}
58
	}
59 3
60
	private function addCallbackHandlers() {
61
62
		/**
63
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
64
		 */
65
		$this->handlers['SMW::Property::initProperties'] = function ( $propertyRegistry ) {
66 2
67 2
			$propertyRegistrationHelper = new PropertyRegistrationHelper( $propertyRegistry );
68
			return $propertyRegistrationHelper->registerProperties();
69
70
		};
71
72
		/**
73
		 * Invalidate on update
74
		 *
75
		 * @since 1.0
76
		 */
77 3
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) {
78
			return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnStoreUpdate( $store, $semanticData );
79
		};
80
81
		/**
82
		 * Invalidate on delete
83
		 *
84
		 * @since 1.0
85
		 */
86 1
		$this->handlers['SMW::SQLStore::AfterDeleteSubjectComplete'] = function ( $store, $title ) {
87 1
			return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnPageDelete(
88 1
				$store,
89
				DIWikiPage::newFromTitle( $title )
90
			);
91
		};
92
93
		/**
94
		 * Invalidate on title move
95
		 *
96
		 * @since 1.0
97
		 */
98
		if ( version_compare( MW_VERSION, "1.35.0", "<" ) ) {
99
			$this->handlers['TitleMoveComplete'] = function ( &$old_title ) {
100
				return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnPageMove( $old_title );
101 1
			};
102
		} else {
103
			$this->handlers['PageMoveComplete'] = function ( LinkTarget $old_title ) {
104
				return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnPageMove( $old_title );
105
			};
106
		}
107
	}
108
109
}
110