Completed
Push — master ( f47ce4...c9683e )
by mw
04:41
created

AnnotatedLanguageParserFunction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 28
cts 30
cp 0.9333

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 26 4
A createTemplateInclusionCode() 0 14 1
A __construct() 0 3 1
A modifyTargetLink() 0 8 3
1
<?php
2
3
namespace SIL;
4
5
use Title;
6
use Language;
7
8
use SMW\Localizer;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.4
13
 *
14
 * @author mwjames
15
 */
16
class AnnotatedLanguageParserFunction {
17
18
	/**
19
	 * @var InterlanguageLinksLookup
20
	 */
21
	private $interlanguageLinksLookup;
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param InterlanguageLinksLookup $interlanguageLinksLookup
27
	 */
28 4
	public function __construct( InterlanguageLinksLookup $interlanguageLinksLookup ) {
29 4
		$this->interlanguageLinksLookup = $interlanguageLinksLookup;
30 4
	}
31
32
	/**
33
	 * @since 1.0
34
	 *
35
	 * @param Title $source
36
	 * @param string $template
37
	 *
38
	 * @return string
39
	 */
40 3
	public function parse( Title $source, $template ) {
41
42 3
		$source = $this->interlanguageLinksLookup->getRedirectTargetFor( $source );
43
44 3
		if ( $source === null ) {
45
			return '';
46
		}
47
48 3
		$languageCode = $this->interlanguageLinksLookup->findPageLanguageForTarget( $source );
49
50 3
		if ( $languageCode === '' ) {
51 1
			return '';
52
		}
53
54 2
		if ( $template === '' ) {
55 1
			return $languageCode;
56
		}
57
58 1
		$templateText = $this->createTemplateInclusionCode(
59 1
			$source,
60 1
			$languageCode,
61 1
			$template
62
		);
63
64 1
		return [ $templateText, 'noparse' => $templateText === '', 'isHTML' => false ];
65
	}
66
67 1
	private function createTemplateInclusionCode( $source, $languageCode, $template ) {
68
69 1
		$result = '';
70 1
		$templateText = '';
71 1
		$wikitext = '';
72
73 1
		$wikitext .= "|target-link=" . $this->modifyTargetLink( $source );
74 1
		$wikitext .= "|lang-code=" . Localizer::asBCP47FormattedLanguageCode( $languageCode );
75 1
		$wikitext .= "|lang-name=" . Language::fetchLanguageName( $languageCode );
76
77 1
		$templateText .= '{{' . $template . $wikitext . '}}';
78
79 1
		return $templateText;
80
	}
81
82 1
	private function modifyTargetLink( $targetLink ) {
83
84 1
		if ( !$targetLink instanceOf Title ) {
85
			$targetLink = Title::newFromText( $targetLink );
86
		}
87
88 1
		return ( $targetLink->getNamespace() === NS_CATEGORY ? ':' : '' ) . $targetLink->getPrefixedText();
89
	}
90
91
}
92