SemanticDataComparator::lookupPropertyValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
ccs 20
cts 20
cp 1
crap 3
1
<?php
2
3
namespace SG;
4
5
use SMW\Store;
6
use SMW\SemanticData;
7
use SMW\DIProperty;
8
9
/**
10
 * @ingroup SG
11
 * @ingroup SemanticGlossary
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author Stephan Gambke
17
 */
18
class SemanticDataComparator {
19
20
	/**
21
	 * @var Store
22
	 */
23
	private $store = null;
24
25
	/**
26
	 * @var SemanticData
27
	 */
28
	private $semanticData = null;
29
30
	/**
31
	 * @since 1.0
32
	 *
33
	 * @param Store $store
34
	 * @param SemanticData $semanticData
35
	 */
36 6
	public function __construct( Store $store, SemanticData $semanticData ) {
37 6
		$this->store = $store;
38 6
		$this->semanticData = $semanticData;
39 6
	}
40
41
	/**
42
	 * @since 1.0
43
	 *
44
	 * @param string $propertyId
45
	 *
46
	 * @return boolean
47
	 */
48 5
	public function compareForProperty( $propertyId ) {
49
50 5
		list( $newEntries, $oldEntries ) = $this->lookupPropertyValues( $propertyId );
51
52 5
		if ( $this->hasNotSamePropertyValuesCount( $newEntries, $oldEntries ) ) {
53 2
			return true;
54
		}
55
56 5
		if ( $this->hasUnmatchPropertyValue( $newEntries, $oldEntries ) ) {
57
			return true;
58
		}
59
60 5
		return false;
61
	}
62
63 5
	private function lookupPropertyValues( $propertyId ) {
64
65 5
		$properties = $this->semanticData->getProperties();
66
67 5
		if ( array_key_exists( $propertyId, $properties ) ) {
68
69 2
			$newEntries = $this->semanticData->getPropertyValues( $properties[$propertyId] );
70 2
			$oldEntries = $this->store->getPropertyValues(
71 2
				$this->semanticData->getSubject(),
72 2
				$properties[$propertyId]
73
			);
74
75
			return array(
76 2
				$newEntries,
77 2
				$oldEntries
78
			);
79
		}
80
81 5
		$newEntries = array();
82 5
		$oldEntries = array();
83
84
		try{
85 5
			$property = new DIProperty( $propertyId );
86 1
		} catch ( \Exception $e ) {
87
			return array(
88 1
				$newEntries,
89 1
				$oldEntries
90
			);
91
		}
92
93 4
		$oldEntries = $this->store->getPropertyValues(
94 4
			$this->semanticData->getSubject(),
95 4
			$property
96
		);
97
98
		return array(
99 4
			$newEntries,
100 4
			$oldEntries
101
		);
102
	}
103
104 5
	private function hasNotSamePropertyValuesCount( $newEntries, $oldEntries ) {
105 5
		return count( $newEntries ) !== count( $oldEntries );
106
	}
107
108 5
	private function hasUnmatchPropertyValue( $newEntries, $oldEntries ) {
109
110 5
		foreach ( $newEntries as $newDi ) {
111 1
			$found = false;
112 1
			foreach ( $oldEntries as $oldKey => $oldDi ) {
113 1
				if ( $newDi->getHash() === $oldDi->getHash() ) {
114 1
					$found = true;
115 1
					unset( $oldEntries[$oldKey] );
116 1
					break;
117
				}
118
			}
119
120
			// If no match was possible...
121 1
			if ( !$found ) {
122 1
				return true;
123
			}
124
		}
125
126
		// Are there unmatched old entries left?
127 5
		if ( count( $oldEntries ) > 0 ) {
128
			return true;
129
		}
130
131 5
		return false;
132
	}
133
134
}
135