Completed
Push — master ( 0bdf57...ff1c2f )
by mw
6s
created

PageContentLanguageModifier::getHashFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SIL;
4
5
use Onoi\Cache\Cache;
6
use SMW\Store;
7
use SMW\DIWikiPage;
8
use SMW\DIProperty;
9
10
use Title;
11
12
/**
13
 * Modifies the content language based on the SIL annotation found
14
 * for the selected page.
15
 *
16
 * @license GNU GPL v2+
17
 * @since 1.0
18
 *
19
 * @author mwjames
20
 */
21
class PageContentLanguageModifier {
22
23
	const POOLCACHE_ID = 'sil.pagecontentlanguage';
24
25
	/**
26
	 * @var InterlanguageLinksLookup
27
	 */
28
	private $interlanguageLinksLookup;
29
30
	/**
31
	 * @var Cache
32
	 */
33
	private $intermediaryCache;
34
35
	/**
36
	 * @since 1.0
37
	 *
38
	 * @param InterlanguageLinksLookup $interlanguageLinksLookup
39
	 * @param Cache $intermediaryCache
40
	 */
41 8
	public function __construct( InterlanguageLinksLookup $interlanguageLinksLookup, Cache $intermediaryCache ) {
42 8
		$this->interlanguageLinksLookup = $interlanguageLinksLookup;
43 8
		$this->intermediaryCache = $intermediaryCache;
44 8
	}
45
46
	/**
47
	 * @since 1.3
48
	 *
49
	 * @param Title $title
50
	 * @param string &languageCode
51
	 */
52 8
	public function addToIntermediaryCache( Title $title, $languageCode ) {
53 8
		$this->intermediaryCache->save( $this->getHashFrom( $title ), $languageCode );
54 8
	}
55
56
	/**
57
	 * @since 1.0
58
	 *
59
	 * @param Title $title
60
	 * @param Language|string &$pageLanguage
61
	 *
62
	 * @return string
63
	 */
64 14
	public function getPageContentLanguage( Title $title, $pageLanguage ) {
65
66 14
		$hash = $this->getHashFrom( $title );
67
68 14
		if ( ( $cachedLanguageCode = $this->intermediaryCache->fetch( $hash ) ) ) {
69 9
			return $cachedLanguageCode;
70
		}
71
72 13
		$lookupLanguageCode = $this->interlanguageLinksLookup->findPageLanguageForTarget( $title );
73
74 13
		if ( $lookupLanguageCode !== null && $lookupLanguageCode !== '' ) {
75 2
			$pageLanguage = $lookupLanguageCode;
76 2
		}
77
78 13
		if ( $pageLanguage instanceof \Language ) {
0 ignored issues
show
Bug introduced by
The class Language does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
79 9
			$pageLanguage = $pageLanguage->getCode();
80 9
		}
81
82 13
		$this->intermediaryCache->save( $hash, $pageLanguage );
83
84 13
		return $pageLanguage;
85
	}
86
87 15
	private function getHashFrom( Title $title ) {
88 15
		return md5( $title->getPrefixedText() );
89
	}
90
91
}
92