1 | <?php |
||
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 ) { |
|
65 | |||
66 | /** |
||
67 | * @since 1.0 |
||
68 | * |
||
69 | * @param string $httpRequestEndpoint |
||
70 | */ |
||
71 | 2 | public function setHttpRequestEndpoint( $httpRequestEndpoint ) { |
|
74 | |||
75 | /** |
||
76 | * @since 1.0 |
||
77 | * |
||
78 | * @param string $repositoryTargetUrl |
||
79 | */ |
||
80 | 2 | public function setRepositoryTargetUrl( $repositoryTargetUrl ) { |
|
83 | |||
84 | /** |
||
85 | * @since 1.0 |
||
86 | * |
||
87 | * @param string $httpResponseCachePrefix |
||
88 | */ |
||
89 | 1 | public function setHttpResponseCachePrefix( $httpResponseCachePrefix ) { |
|
92 | |||
93 | /** |
||
94 | * @since 1.0 |
||
95 | * |
||
96 | * @param integer $httpResponseCacheLifetime |
||
97 | */ |
||
98 | 1 | public function setHttpResponseCacheLifetime( $httpResponseCacheLifetime ) { |
|
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 ) { |
|
167 | |||
168 | 5 | private function doMakeHttpRequestFor( $query ) { |
|
192 | |||
193 | } |
||
194 |