Completed
Push — master ( 86c4c5...49aca9 )
by mw
18:55
created

QueryResultFetcher   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.24%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 16
c 8
b 1
f 1
lcom 1
cbo 7
dl 0
loc 175
ccs 60
cts 63
cp 0.9524
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpRequestEndpoint() 0 3 1
A setRepositoryTargetUrl() 0 3 1
A setHttpResponseCachePrefix() 0 3 1
A setHttpResponseCacheLifetime() 0 3 1
A __construct() 0 5 1
C fetchQueryResult() 0 35 7
A doResetPrintRequestsToQuerySource() 0 19 3
B doMakeHttpRequestFor() 0 24 1
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( isset( $result['error']['info'] ) ? array( $result['error']['info'] ) : $result['error']['query'] );
121 1
			return $this->queryResultFactory->newEmptyQueryResult( $query );
122
		}
123
124
		// Add the source from where the result was retrieved
125 1
		if ( isset( $result['query']['meta']['source'] ) ) {
126
			$result['query']['meta']['source'] = $query->getQuerySource();
127
		}
128
129 1
		$this->jsonResponseParser->doParse( $result );
130
131 1
		$queryResult = $this->queryResultFactory->newByHttpRequestQueryResult(
132 1
			$query,
133 1
			$this->jsonResponseParser
134 1
		);
135
136 1
		$queryResult->setFromCache( $isFromCache );
0 ignored issues
show
Bug introduced by
The method setFromCache() does not seem to exist on object<SEQL\ByHttpRequest\QueryResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137 1
138 1
		$queryResult->setRemoteTargetUrl(
139
			str_replace( '$1', '',  $this->repositoryTargetUrl )
140 1
		);
141
142
		return $queryResult;
143 5
	}
144
145 5
	private function doResetPrintRequestsToQuerySource( $query ) {
146
147 5
		$querySource = $query->getQuerySource();
148
149 5
		foreach ( $query->getExtraPrintouts() as $printRequest ) {
150 4
151
			if ( $printRequest->getData() === null ) {
152
				continue;
153 1
			}
154 1
155
			$property = $printRequest->getData()->getDataItem();
156 1
			$property->setInterwiki( $querySource );
157
158
			$printRequest->getData()->setDataItem( $property );
159 1
160 5
			// Reset label after dataItem was re-added
161 5
			$printRequest->setLabel( $printRequest->getLabel() );
162
		}
163 5
	}
164
165 5
	private function doMakeHttpRequestFor( $query ) {
166
167 5
		$httpRequest = $this->httpRequestFactory->newCachedCurlRequest();
168 5
169
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, $this->httpResponseCacheLifetime );
170 5
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, $this->httpResponseCachePrefix . ':seql:' );
171
172 5
		$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
173 5
174 5
		$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
175
		$httpRequest->setOption( CURLOPT_FAILONERROR, true );
176 5
		$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
177
178 5
		$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint . '?action=ask&format=json&query=' . QueryEncoder::rawUrlEncode( $query ) );
179 5
180
		$httpRequest->setOption( CURLOPT_HTTPHEADER, array(
181 5
			'Accept: application/json',
182
			'Content-Type: application/json; charset=utf-8'
183 5
		) );
184
185 5
		$response = $httpRequest->execute();
186
187
		return array( json_decode( $response, true ), $httpRequest->isFromCache() );
188
	}
189
190
}
191