ElementsCacheBuilder   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.23%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 218
ccs 98
cts 104
cp 0.9423
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getElements() 0 42 6
A buildElements() 0 18 2
B buildQuery() 0 57 2
A getDefinitionValue() 0 16 2
A getLinkValue() 0 13 2
A getStyleValue() 0 13 2
A getTerms() 0 15 3
1
<?php
2
3
namespace SG\Cache;
4
5
use SG\PropertyRegistrationHelper;
6
use SMW\DataValueFactory;
7
use SMW\Store;
8
use SMW\DIProperty;
9
use SMWStringValue as StringValue;
10
use SMWPrintRequest as PrintRequest;
11
use SMWPropertyValue as PropertyValue;
12
use SMWThingDescription as ThingDescription;
13
use SMWSomeProperty as SomeProperty;
14
use SMWQuery as Query;
15
use Lingo\Element;
16
17
/**
18
 * @ingroup SG
19
 * @ingroup SemanticGlossary
20
 *
21
 * @license GNU GPL v2+
22
 * @since 1.1
23
 *
24
 * @author Stephan Gambke
25
 * @author mwjames
26
 */
27
class ElementsCacheBuilder {
28
29
	/* @var Store */
30
	private $store;
31
32
	/* @var GlossaryCache */
33
	private $glossaryCache;
34
35
	private $mDiTerm;
36
	private $mDiDefinition;
37
	private $mDiLink;
38
	private $mDiStyle;
39
40
	private $mDvTerm;
41
	private $mDvDefinition;
42
	private $mDvLink;
43
	private $mDvStyle;
44
45
	private $queryResults;
46
47
	/**
48
	 * @since  1.1
49
	 *
50
	 * @param SMWStore $store
51
	 * @param GlossaryCache $cache
0 ignored issues
show
Bug introduced by
There is no parameter named $cache. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
	 */
53 2
	public function __construct( Store $store, GlossaryCache $glossaryCache ) {
54 2
		$this->store = $store;
55 2
		$this->glossaryCache = $glossaryCache;
56 2
	}
57
58
	/**
59
	 * @since 1.1
60
	 *
61
	 * @return array
62
	 */
63 1
	public function getElements() {
64
65 1
		$ret = array();
66
67 1
		if ( $this->queryResults === null ) {
68 1
			$this->queryResults = $this->store->getQueryResult( $this->buildQuery() )->getResults();
69
		}
70
71
		// find next line
72 1
		$page = current( $this->queryResults );
73
74 1
		if ( $page && count( $ret ) == 0 ) {
75
76 1
			next( $this->queryResults );
77
78 1
			$cachekey = $this->glossaryCache->getKeyForSubject( $page );
79 1
			$cachedResult = $this->glossaryCache->getCache()->get( $cachekey );
80
81
			// cache hit?
82 1
			if ( $cachedResult !== false && $cachedResult !== null ) {
83
84
				wfDebug( "Cache hit: Got glossary entry $cachekey from cache.\n" );
85
				$ret = &$cachedResult;
86
			} else {
87
88 1
				wfDebug( "Cache miss: Glossary entry $cachekey not found in cache.\n" );
89
90 1
				$ret = $this->buildElements(
91 1
					$this->getTerms( $page ),
92 1
					$this->getDefinitionValue( $page ),
93 1
					$this->getLinkValue( $page ),
94 1
					$this->getStyleValue( $page ),
95 1
					$page
96
				);
97
98 1
				wfDebug( "Cached glossary entry $cachekey.\n" );
99 1
				$this->glossaryCache->getCache()->set( $cachekey, $ret );
100
			}
101
		}
102
103 1
		return $ret;
104
	}
105
106 1
	private function buildElements( $terms, $definition, $link, $style, $page ) {
107
108 1
		$ret = array();
109
110 1
		foreach ( $terms as $term ) {
111
			$tmp_ret = array(
112 1
				Element::ELEMENT_TERM => $term,
113 1
				Element::ELEMENT_DEFINITION => $definition,
114 1
				Element::ELEMENT_LINK => $link,
115 1
				Element::ELEMENT_STYLE => $style,
116 1
				Element::ELEMENT_SOURCE => $page
117
			);
118
119 1
			$ret[] = $tmp_ret;
120
		}
121
122 1
		return $ret;
123
	}
124
125 1
	private function buildQuery() {
126
127 1
		$dataValueFactory = DataValueFactory::getInstance();
128
129
		// build term data item and data value for later use
130 1
		$this->mDiTerm = new DIProperty( PropertyRegistrationHelper::SG_TERM );
131 1
		$this->mDvTerm = $dataValueFactory->newDataValueByType( '_txt' );
132 1
		$this->mDvTerm->setProperty( $this->mDiTerm );
133
134 1
		$pvTerm = $dataValueFactory->newDataValueByType( '__pro' );
135 1
		$pvTerm->setDataItem( $this->mDiTerm );
136 1
		$prTerm = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvTerm );
137
138
		// build definition data item and data value for later use
139 1
		$this->mDiDefinition = new DIProperty( PropertyRegistrationHelper::SG_DEFINITION );
140 1
		$this->mDvDefinition = $dataValueFactory->newDataValueByType( '_txt' );
141 1
		$this->mDvDefinition->setProperty( $this->mDiDefinition );
142
143 1
		$pvDefinition = $dataValueFactory->newDataValueByType( '__pro' );
144 1
		$pvDefinition->setDataItem( $this->mDiDefinition );
145 1
		$prDefinition = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvDefinition );
146
147
		// build link data item and data value for later use
148 1
		$this->mDiLink = new DIProperty( PropertyRegistrationHelper::SG_LINK );
149 1
		$this->mDvLink = $dataValueFactory->newDataValueByType( '_txt' );
150 1
		$this->mDvLink->setProperty( $this->mDiLink );
151
152 1
		$pvLink = $dataValueFactory->newDataValueByType( '__pro' );
153 1
		$pvLink->setDataItem( $this->mDiLink );
154 1
		$prLink = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvLink );
155
156
		// build style data item and data value for later use
157 1
		$this->mDiStyle = new DIProperty( PropertyRegistrationHelper::SG_STYLE );
158 1
		$this->mDvStyle = $dataValueFactory->newDataValueByType( '_txt' );
159 1
		$this->mDvStyle->setProperty( $this->mDiStyle );
160
161 1
		$pvStyle = $dataValueFactory->newDataValueByType( '__pro' );
162 1
		$pvStyle->setDataItem( $this->mDiStyle );
163 1
		$prStyle = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvStyle );
164
165
		// Create query
166 1
		$desc = new SomeProperty( new DIProperty( '___glt' ), new ThingDescription() );
167 1
		$desc->addPrintRequest( $prTerm );
168 1
		$desc->addPrintRequest( $prDefinition );
169 1
		$desc->addPrintRequest( $prLink );
170 1
		$desc->addPrintRequest( $prStyle );
171
172 1
		$query = new Query( $desc, false, false );
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
173 1
		$query->sort = true;
174 1
		$query->sortkeys['___glt'] = 'ASC';
175
176 1
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
177 1
			$query->setOption( Query::PROC_CONTEXT, 'SG.ElementsCacheBuilder' );
178
		}
179
180 1
		return $query;
181
	}
182
183 1
	private function getDefinitionValue( $page ) {
184
185 1
		$definition  = null;
186
187 1
		$definitions = $this->store->getPropertyValues(
188 1
			$page,
189 1
			$this->mDiDefinition
190
		);
191
192 1
		if ( !empty( $definitions ) ) {
193 1
			$this->mDvDefinition->setDataItem( $definitions[0] );
194 1
			$definition = trim( $this->mDvDefinition->getShortWikiText() );
195
		}
196
197 1
		return $definition;
198
	}
199
200 1
	private function getLinkValue( $page ) {
201
202 1
		$link  = null;
203
204 1
		$links = $this->store->getPropertyValues( $page, $this->mDiLink );;
205
206 1
		if ( !empty( $links ) ) {
207
			$this->mDvLink->setDataItem( $links[0] );
208
			$link = trim( $this->mDvLink->getShortWikiText() );
209
		}
210
211 1
		return $link;
212
	}
213
214 1
	private function getStyleValue( $page ) {
215
216 1
		$style  = null;
217
218 1
		$styles = $this->store->getPropertyValues( $page, $this->mDiStyle );;
219
220 1
		if ( !empty( $styles ) ) {
221
		  $this->mDvStyle->setDataItem( $styles[0] );
222
		  $style = trim( $this->mDvStyle->getShortWikiText() );
223
		}
224
225 1
		return $style;
226
	}
227
228 1
	private function getTerms( $page ) {
229
230 1
		$collectedTerms = array();
231
232 1
		$terms = $this->store->getPropertyValues( $page, $this->mDiTerm );
233
234 1
		if ( $terms !== array() ) {
235 1
			foreach ( $terms as $term ) {
236 1
				$this->mDvTerm->setDataItem( $term );
237 1
				$collectedTerms[] = trim( $this->mDvTerm->getShortWikiText() );
238
			}
239
		}
240
241 1
		return $collectedTerms;
242
	}
243
244
}
245