CoordinateDescription   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryString() 0 8 2
B getSQLCondition() 0 36 6
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\SemanticMW;
6
7
use SMW\DataValueFactory;
8
use SMW\Query\Language\ValueDescription;
9
use SMWDIGeoCoord;
10
use Wikimedia\Rdbms\IDatabase;
11
12
/**
13
 * Description of one data value of type Geographical Coordinates.
14
 *
15
 * @author Jeroen De Dauw
16
 */
17
class CoordinateDescription extends ValueDescription {
18
19
	public function getQueryString( $asValue = false ) {
20
		$queryString = DataValueFactory::getInstance()->newDataValueByItem(
21
			$this->getDataItem(),
22
			$this->getProperty()
23
		)->getWikiValue();
24
25
		return $asValue ? $queryString : "[[$queryString]]";
26
	}
27
28
	/**
29
	 * @see SomePropertyInterpreter::mapValueDescription
30
	 *
31
	 * FIXME: store specific code should be in the store component
32
	 *
33
	 * @param string $tableName
34
	 * @param string[] $fieldNames
35
	 * @param IDatabase $db
36
	 *
37
	 * @return string|false
38
	 */
39
	public function getSQLCondition( $tableName, array $fieldNames, IDatabase $db ) {
40
		$dataItem = $this->getDataItem();
41
42
		// Only execute the query when the description's type is geographical coordinates,
43
		// the description is valid, and the near comparator is used.
44
		if ( $dataItem instanceof SMWDIGeoCoord ) {
0 ignored issues
show
Bug introduced by
The class SMWDIGeoCoord does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
45
			switch ( $this->getComparator() ) {
46
				case SMW_CMP_EQ:
47
					$comparator = '=';
48
					break;
49
				case SMW_CMP_LEQ:
50
					$comparator = '<=';
51
					break;
52
				case SMW_CMP_GEQ:
53
					$comparator = '>=';
54
					break;
55
				case SMW_CMP_NEQ:
56
					$comparator = '!=';
57
					break;
58
				default:
59
					return false;
60
			}
61
62
			$lat = $db->addQuotes( $dataItem->getLatitude() );
63
			$lon = $db->addQuotes( $dataItem->getLongitude() );
64
65
			$conditions = [];
66
67
			$conditions[] = "{$tableName}.$fieldNames[1] $comparator $lat";
68
			$conditions[] = "{$tableName}.$fieldNames[2] $comparator $lon";
69
70
			return implode( ' AND ', $conditions );
71
		}
72
73
		return false;
74
	}
75
76
}
77