Completed
Push — master ( 6d1705...9d80f0 )
by Jeroen De
02:45
created

IdGeneratorSetup::onExtensionFunction()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 13
cp 0.8462
rs 9.584
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2.0145
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace IdGenerator;
6
7
use MediaWiki\MediaWikiServices;
8
use Parser;
9
10
class IdGeneratorSetup {
11
12
	/**
13
	 * @var IdGeneratorFactory
14
	 */
15
	private static $factory;
16
17 1
	public static function onExtensionFunction() {
18
		self::$factory = new IdGeneratorFactory();
19
20 1
		$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function ( Parser &$parser ) {
21 1
			foreach ( [ 'generate_id', 'generateid' ] as $functionName ) {
22 1
				$parser->setFunctionHook(
23 1
					$functionName,
24 1
					function ( Parser $parser, string $param = '' ) {
25 1
						$generator = self::$factory->getIdGenerator(
26 1
							MediaWikiServices::getInstance()->getDBLoadBalancer()
27
						);
28
29
						return [
30 1
							$generator->getNewId( $param ),
31
							'noparse' => true
32
						];
33 1
					}
34
				);
35
			}
36 1
		};
37
	}
38
39
}
40