QueryResultFetcher   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.92%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 0
loc 178
ccs 63
cts 65
cp 0.9692
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setHttpRequestEndpoint() 0 3 1
A setRepositoryTargetUrl() 0 3 1
A setHttpResponseCachePrefix() 0 3 1
A setHttpResponseCacheLifetime() 0 3 1
A doResetPrintRequestsToQuerySource() 0 19 3
A doMakeHttpRequestFor() 0 24 1
B fetchQueryResult() 0 38 7
1
<?php
2
3
namespace SEQL\ByHttpRequest;
4
5
use Onoi\HttpRequest\HttpRequestFactory;
6
use SEQL\QueryEncoder;
7
use SEQL\QueryResultFactory;
8
use SMWQuery as Query;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class QueryResultFetcher {
17
18
	/**
19
	 * @var HttpRequestFactory
20
	 */
21
	private $httpRequestFactory;
22
23
	/**
24
	 * @var QueryResultFactory
25
	 */
26
	private $queryResultFactory;
27
28
	/**
29
	 * @var JsonResponseParser
30
	 */
31
	private $jsonResponseParser;
32
33
	/**
34
	 * @var string
35
	 */
36
	private $httpRequestEndpoint = '';
37
38
	/**
39
	 * @var string
40
	 */
41
	private $repositoryTargetUrl = '';
42
43
	/**
44
	 * @var string
45
	 */
46
	private $httpResponseCachePrefix;
47
48
	/**
49
	 * @var integer
50
	 */
51
	private $httpResponseCacheLifetime;
52
53
	/**
54
	 * @since 1.0
55
	 *
56
	 * @param HttpRequestFactory $httpRequestFactory
57
	 * @param QueryResultFactory $queryResultFactory
58
	 * @param JsonResponseParser $jsonResponseParser
59
	 */
60 6
	public function __construct( HttpRequestFactory $httpRequestFactory, QueryResultFactory $queryResultFactory, JsonResponseParser $jsonResponseParser ) {
61 6
		$this->httpRequestFactory = $httpRequestFactory;
62 6
		$this->queryResultFactory = $queryResultFactory;
63 6
		$this->jsonResponseParser = $jsonResponseParser;
64 6
	}
65
66
	/**
67
	 * @since 1.0
68
	 *
69
	 * @param string $httpRequestEndpoint
70
	 */
71 2
	public function setHttpRequestEndpoint( $httpRequestEndpoint ) {
72 2
		$this->httpRequestEndpoint = $httpRequestEndpoint;
73 2
	}
74
75
	/**
76
	 * @since 1.0
77
	 *
78
	 * @param string $repositoryTargetUrl
79
	 */
80 2
	public function setRepositoryTargetUrl( $repositoryTargetUrl ) {
81 2
		$this->repositoryTargetUrl = $repositoryTargetUrl;
82 2
	}
83
84
	/**
85
	 * @since 1.0
86
	 *
87
	 * @param string $httpResponseCachePrefix
88
	 */
89 1
	public function setHttpResponseCachePrefix( $httpResponseCachePrefix ) {
90 1
		$this->httpResponseCachePrefix = $httpResponseCachePrefix;
91 1
	}
92
93
	/**
94
	 * @since 1.0
95
	 *
96
	 * @param integer $httpResponseCacheLifetime
97
	 */
98 1
	public function setHttpResponseCacheLifetime( $httpResponseCacheLifetime ) {
99 1
		$this->httpResponseCacheLifetime = $httpResponseCacheLifetime;
100 1
	}
101
102
	/**
103
	 * @since 1.0
104
	 *
105
	 * @param Query $query
106
	 *
107
	 * @return QueryResult
108
	 */
109 5
	public function fetchQueryResult( Query $query ) {
110
111 5
		$this->doResetPrintRequestsToQuerySource( $query );
112
113 5
		list( $result, $isFromCache ) = $this->doMakeHttpRequestFor( $query );
114
115 5
		if ( $result === array() || $result === false || $result === null ) {
116 3
			return $this->queryResultFactory->newEmptyQueryResult( $query );
117
		}
118
119 2
		if ( isset( $result['error'] ) ) {
120 1
			$query->addErrors(
121 1
				isset( $result['error']['info'] ) ? array( implode( ', ', $result['error'] ) ) : $result['error']['query']
122 1
			);
123
124 1
			return $this->queryResultFactory->newEmptyQueryResult( $query );
125
		}
126
127
		// Add the source from where the result was retrieved
128 1
		if ( isset( $result['query']['meta']['source'] ) ) {
129
			$result['query']['meta']['source'] = $query->getQuerySource();
130
		}
131
132 1
		$this->jsonResponseParser->doParse( $result );
133
134 1
		$queryResult = $this->queryResultFactory->newByHttpRequestQueryResult(
135 1
			$query,
136 1
			$this->jsonResponseParser
137 1
		);
138
139 1
		$queryResult->setFromCache( $isFromCache );
140
141 1
		$queryResult->setRemoteTargetUrl(
142 1
			str_replace( '$1', '',  $this->repositoryTargetUrl )
143 1
		);
144
145 1
		return $queryResult;
146
	}
147
148 5
	private function doResetPrintRequestsToQuerySource( $query ) {
149
150 5
		$querySource = $query->getQuerySource();
151
152 5
		foreach ( $query->getExtraPrintouts() as $printRequest ) {
153
154 5
			if ( $printRequest->getData() === null ) {
155 4
				continue;
156
			}
157
158 1
			$property = $printRequest->getData()->getDataItem();
159 1
			$property->setInterwiki( $querySource );
160
161 1
			$printRequest->getData()->setDataItem( $property );
162
163
			// Reset label after dataItem was re-added
164 1
			$printRequest->setLabel( $printRequest->getLabel() );
165 5
		}
166 5
	}
167
168 5
	private function doMakeHttpRequestFor( $query ) {
169
170 5
		$httpRequest = $this->httpRequestFactory->newCachedCurlRequest();
171
172 5
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, $this->httpResponseCacheLifetime );
173 5
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, $this->httpResponseCachePrefix . ':seql:' );
174
175 5
		$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
176
177 5
		$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
178 5
		$httpRequest->setOption( CURLOPT_FAILONERROR, true );
179 5
		$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
180
181 5
		$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint . '?action=ask&format=json&query=' . QueryEncoder::rawUrlEncode( $query ) );
182
183 5
		$httpRequest->setOption( CURLOPT_HTTPHEADER, array(
184 5
			'Accept: application/json',
185
			'Content-Type: application/json; charset=utf-8'
186 5
		) );
187
188 5
		$response = $httpRequest->execute();
189
190 5
		return array( json_decode( $response, true ), $httpRequest->isFromCache() );
191
	}
192
193
}
194