Completed
Push — master ( fef851...52af6f )
by Stephan
04:43
created

HookRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SG;
4
5
use SMW\Store;
6
use SMW\DIWikiPage;
7
use Hooks;
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
	 *
25
	 * @param Store $store
26
	 */
27 1
	public function __construct( Store $store ) {
28 1
		$this->addCallbackHandlers( $store );
29 1
	}
30
31
	/**
32
	 * @since  1.1
33
	 *
34
	 * @param string $name
35
	 *
36
	 * @return boolean
37
	 */
38
	public function isRegistered( $name ) {
39
		return Hooks::isRegistered( $name );
40
	}
41
42
	/**
43
	 * @since  1.1
44
	 *
45
	 * @param string $name
46
	 *
47
	 * @return Callable|false
48
	 */
49
	public function getHandlerFor( $name ) {
50
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
51
	}
52
53
	/**
54
	 * @since  1.0
55
	 */
56
	public function register() {
57
		foreach ( $this->handlers as $name => $callback ) {
58
			Hooks::register( $name, $callback );
59
		}
60
	}
61
62 3
	private function addCallbackHandlers( $store ) {
0 ignored issues
show
Unused Code introduced by
The parameter $store 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...
63
64 1
		$propertyRegistry = new PropertyRegistry();
65
66
		/**
67
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
68
		 */
69
		$this->handlers['SMW::Property::initProperties'] = function () use ( $propertyRegistry ) {
70 2
			return PropertyRegistry::getInstance()->register();
71
		};
72
73
		/**
74
		 * Invalidate on update
75
		 *
76
		 * @since 1.0
77
		 */
78
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) {
79 3
			return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnStoreUpdate( $store, $semanticData );
80
		};
81
82
		/**
83
		 * Invalidate on delete
84
		 *
85
		 * @since 1.0
86
		 */
87
		$this->handlers['SMW::SQLStore::AfterDeleteSubjectComplete'] = function ( $store, $title ) {
88 1
			return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnPageDelete(
89 1
				$store,
90 1
				DIWikiPage::newFromTitle( $title )
91
			);
92
		};
93
94
		/**
95
		 * Invalidate on title move
96
		 *
97
		 * @since 1.0
98
		 */
99
		$this->handlers['TitleMoveComplete'] = function ( &$old_title ) {
100
			return \SG\Cache\CacheInvalidator::getInstance()->invalidateCacheOnPageMove( $old_title );
101
		};
102
103 1
	}
104
105
}
106