Completed
Push — master ( aad109...a11cd2 )
by mw
02:20
created

ByHttpRequestQueryLookup   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryResult() 0 18 4
A tryToMatchInterwikiFor() 0 3 1
A fetchQueryResultFor() 0 16 1
A getCacheFactory() 0 8 2
1
<?php
2
3
namespace SEQL;
4
5
use Onoi\HttpRequest\HttpRequestFactory;
6
use SEQL\ByHttpRequest\JsonResponseParser;
7
use SEQL\ByHttpRequest\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 ByHttpRequestQueryLookup 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