Completed
Push — master ( 3e30e6...aad109 )
by mw
02:31
created

ByAskApiHttpRequestQueryLookup::getCacheFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SEQL;
4
5
use Onoi\HttpRequest\HttpRequestFactory;
6
use SEQL\ByAskApiHttpRequest\JsonResponseParser;
7
use SEQL\ByAskApiHttpRequest\QueryResultFetcher;
8
use SMW\SQLStore\SQLStore;
9
use SMW\ApplicationFactory;
10
use SMWQuery as Query;
11
use SMWQueryResult as QueryResult;
12
use Interwiki;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 1.0
17
 *
18
 * @author mwjames
19
 */
20
class ByAskApiHttpRequestQueryLookup extends SQLStore {
21
22
	/**
23
	 * @var QueryResultFactory
24
	 */
25
	private $queryResultFactory;
26
27
	/**
28
	 * @var CacheFactory
29
	 */
30
	private $cacheFactory;
31
32
	/**
33
	 * @since 1.0
34
	 *
35
	 * @param Query $query
36
	 *
37
	 * @return QueryResult
38
	 */
39 3
	public function getQueryResult( Query $query ) {
40
41 3
		$this->queryResultFactory = new QueryResultFactory( $this );
42
43 3
		if ( $query->querymode === Query::MODE_DEBUG ) {
44 1
			$query->addErrors( array( wfMessage( 'seql-debug-query-not-supported' )->text() ) );
45 1
			return $this->queryResultFactory->newEmptyQueryResult( $query );
46
		}
47
48 2
		$interwiki = $this->tryToMatchInterwikiFor( $query );
49
50 2
		if ( $interwiki === false || $interwiki === null ) {
51 1
			$query->addErrors( array( wfMessage( 'seql-interwiki-prefix-is-missing', $query->getQuerySource() )->text() ) );
52 1
			return $this->queryResultFactory->newEmptyQueryResult( $query );
53
		}
54
55 1
		return $this->fetchQueryResultFor( $query, $interwiki );
56
	}
57
58 1
	protected function tryToMatchInterwikiFor( Query $query ) {
59 1
		return Interwiki::fetch( $query->getQuerySource() );
60
	}
61
62 1
	protected function fetchQueryResultFor( Query $query, $interwiki ) {
0 ignored issues
show
Coding Style introduced by
fetchQueryResultFor uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
64 1
		$queryResultFetcher = new QueryResultFetcher(
65 1
			new HttpRequestFactory( $this->getCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['seqlgHttpResponseCacheType'] ) ),
66 1
			$this->queryResultFactory,
67 1
			new JsonResponseParser( new DataValueDeserializer( $query->getQuerySource() ) )
68 1
		);
69
70 1
		$queryResultFetcher->setHttpRequestEndpoint( $interwiki->getApi() );
71 1
		$queryResultFetcher->setRepositoryTargetUrl( $interwiki->getUrl() );
72
73 1
		$queryResultFetcher->setHttpResponseCachePrefix( $this->getCacheFactory()->getCachePrefix() );
74 1
		$queryResultFetcher->setHttpResponseCacheLifetime( $GLOBALS['seqlgHttpResponseCacheLifetime'] );
75
76 1
		return $queryResultFetcher->fetchQueryResult( $query );
77
	}
78
79 1
	private function getCacheFactory() {
80
81 1
		if ( $this->cacheFactory === null ) {
82 1
			$this->cacheFactory = ApplicationFactory::getInstance()->newCacheFactory();
83 1
		}
84
85 1
		return $this->cacheFactory;
86
	}
87
88
}
89