Completed
Push — master ( ff2cd3...da92b9 )
by mw
236:43 queued 201:44
created

newParametersProfileAnnotator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Query;
4
5
use SMW\DIWikiPage;
6
use SMW\Query\ProfileAnnotators\NullProfileAnnotator;
7
use SMW\Query\ProfileAnnotators\DescriptionProfileAnnotator;
8
use SMW\Query\ProfileAnnotators\FormatProfileAnnotator;
9
use SMW\Query\ProfileAnnotators\ParametersProfileAnnotator;
10
use SMW\Query\ProfileAnnotators\DurationProfileAnnotator;
11
use SMW\Query\ProfileAnnotators\SourceProfileAnnotator;
12
use SMWContainerSemanticData as ContainerSemanticData;
13
use SMWDIContainer as DIContainer;
14
use SMWQuery as Query;
15
16
/**
17
 * @license GNU GPL v2+
18
 * @since 2.1
19
 *
20
 * @author mwjames
21
 */
22
class ProfileAnnotatorFactory {
23
24
	/**
25
	 * @since 2.5
26
	 *
27
	 * @param Query $query
28
	 *
29
	 * @return DescriptionProfileAnnotator
30 101
	 */
31
	public function newDescriptionProfileAnnotator( Query $query ) {
32 101
33 101
		$profileAnnotator = new NullProfileAnnotator(
34
			$this->newDIContainer( $query )
35
		);
36 101
37
		$profileAnnotator = new DescriptionProfileAnnotator(
38 101
			$profileAnnotator,
39
			$query->getDescription()
0 ignored issues
show
Bug introduced by
It seems like $query->getDescription() targeting SMWQuery::getDescription() can also be of type null; however, SMW\Query\ProfileAnnotat...nnotator::__construct() does only seem to accept object<SMW\Query\Language\Description>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
		);
41 101
42
		return $profileAnnotator;
43
	}
44
45
	/**
46
	 * @since 2.1
47
	 *
48
	 * @param Query $query
49
	 * @param string $format
50
	 *
51
	 * @return ProfileAnnotator
52 94
	 */
53
	public function newCombinedProfileAnnotator( Query $query, $format ) {
54 94
55
		$profileAnnotator = $this->newDescriptionProfileAnnotator(
56
			$query
57
		);
58 94
59
		$profileAnnotator = $this->newFormatProfileAnnotator(
60
			$profileAnnotator,
61
			$format
62
		);
63 94
64
		$profileAnnotator = $this->newParametersProfileAnnotator(
65 94
			$profileAnnotator,
66
			$query
67
		);
68 94
69
		$profileAnnotator = $this->newDurationProfileAnnotator(
70 94
			$profileAnnotator,
71
			$query->getOption( Query::PROC_QUERY_TIME )
72
		);
73 94
74
		$profileAnnotator = $this->newSourceProfileAnnotator(
75
			$profileAnnotator,
76 94
			$query->getQuerySource()
77 94
		);
78
79
		return $profileAnnotator;
80 94
	}
81 94
82
	private function newFormatProfileAnnotator( $profileAnnotator, $format ) {
83
		return new FormatProfileAnnotator( $profileAnnotator, $format );
84 94
	}
85
86 94
	private function newParametersProfileAnnotator( $profileAnnotator, $query ) {
87 91
88
		if ( $query->getOption( Query::OPT_PARAMETERS ) === false ) {
89
			return $profileAnnotator;
90 3
		}
91
92
		return new ParametersProfileAnnotator( $profileAnnotator, $query );
93
	}
94
95
	private function newDurationProfileAnnotator( $profileAnnotator, $duration ) {
96
		return new DurationProfileAnnotator( $profileAnnotator, $duration );
97 101
	}
98
99 101
	private function newSourceProfileAnnotator( $profileAnnotator, $querySource ) {
100
101 101
		if ( $querySource === '' ) {
102 1
			return $profileAnnotator;
103
		}
104 100
105 100
		return new SourceProfileAnnotator( $profileAnnotator, $querySource );
106 100
	}
107 100
108 100
	/**
109
	 * #1416 create container manually to avoid any issues that may arise from
110
	 * a failed Title::makeTitleSafe.
111 100
	 */
112
	private function newDIContainer( Query $query ) {
113
114 101
		$subject = $query->getContextPage();
115
116
		if ( $subject === null ) {
117
			$containerSemanticData = ContainerSemanticData::makeAnonymousContainer();
118
		} else {
119
			$subject = new DIWikiPage(
120
				$subject->getDBkey(),
121
				$subject->getNamespace(),
122
				$subject->getInterwiki(),
123
				$query->getQueryId()
124
			);
125
126
			$containerSemanticData = new ContainerSemanticData( $subject );
127
		}
128
129
		return new DIContainer(
130
			$containerSemanticData
131
		);
132
	}
133
134
}
135