Completed
Push — master ( 1b8948...05c133 )
by mw
20:16
created

findCitationResourceLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 3
crap 2
1
<?php
2
3
namespace SCI;
4
5
use SMW\Query\Language\SomeProperty;
6
use SMW\Query\Language\ValueDescription;
7
use SMW\Query\PrintRequest;
8
use SMW\Store;
9
use SMW\DIProperty;
10
use SMWPropertyValue as PropertyValue;
11
use SMWQuery as Query;
12
use SMWDIBlob as DIBlob;
13
use SMW\DataValueFactory;
14
use SCI\DataValues\ResourceIdentifierFactory;
15
16
/**
17
 * @license GNU GPL v2+
18
 * @since 2.2
19
 *
20
 * @author mwjames
21
 */
22
class CitationResourceMatchFinder {
23
24
	/**
25
	 * @var Store
26
	 */
27
	private $store;
28
29
	/**
30
	 * @var DataValueFactory
31
	 */
32
	private $dataValueFactory;
33
34
	/**
35
	 * @since 1.0
36
	 *
37
	 * @param Store $store
38
	 */
39 16
	public function __construct( Store $store ) {
40 16
		$this->store = $store;
41 16
		$this->dataValueFactory = DataValueFactory::getInstance();
42 16
	}
43
44
	/**
45
	 * @since 1.0
46
	 *
47
	 * @param array $subjects
48
	 * @param string $linkClass
49
	 *
50
	 * @return array
51
	 */
52 2
	public function findCitationResourceLinks( array $subjects, $linkClass = '', $caption = '' ) {
53
54 1
		$citationResourceLinks = [];
55
56
		foreach ( $subjects as $subject ) {
57 1
58
			$dataValue = $this->dataValueFactory->newDataItemValue(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newDataItemValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByItem

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59 1
				$subject,
60
				null
61 1
			);
62 1
63
			$browselink = \SMWInfolink::newBrowsingLink(
64 1
				$caption,
65
				$dataValue->getWikiValue(),
66 1
				$linkClass
67 1
			);
68 2
69
			$citationResourceLinks[] = $browselink->getHTML();
70 1
		}
71
72 1
		return $citationResourceLinks;
73 1
	}
74
75 1
	/**
76
	 * Find match for [[OCLC::SomeOclcKey]]
77
	 *
78
	 * @since 1.0
79
	 *
80
	 * @param string $type
81
	 * @param string|null $id
82
	 *
83
	 * @return array
84
	 */
85
	public function findMatchForResourceIdentifierTypeToValue( $type, $id = null ) {
86
87
		if ( $id === null || $id === '' ) {
88 6
			return [];
89
		}
90 6
91
		$resourceIdentifierFactory = new ResourceIdentifierFactory();
92
93
		$resourceIdentifierStringValue = $resourceIdentifierFactory->newResourceIdentifierStringValueForType( $type );
94 6
		$resourceIdentifierStringValue->setUserValue( $id );
95
		$id = $resourceIdentifierStringValue->getWikiValue();
96 6
97 6
		$description = new SomeProperty(
98 6
			$resourceIdentifierStringValue->getProperty(),
0 ignored issues
show
Bug introduced by
It seems like $resourceIdentifierStringValue->getProperty() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
99
			new ValueDescription( new DIBlob( $id ) )
100 6
		);
101 6
102 6
		$query = new Query(
103 6
			$description,
104
			false,
105 6
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
106 6
		);
107 6
108
		$query->querymode = Query::MODE_INSTANCES;
109 6
		$query->setLimit( 10 );
110
111 6
		if ( defined( 'SMWQuery::NO_CACHE' ) ) {
112 6
			$query->setOption( Query::NO_CACHE, true );
113
		}
114 6
115 6
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
116 6
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
117
		}
118 6
119 6
		return $this->store->getQueryResult( $query )->getResults();
120 6
	}
121
122 6
	/**
123
	 * @since 1.0
124
	 *
125
	 * @param string $citationReference
126
	 *
127
	 * @return array
128
	 */
129
	public function findCitationTextFor( $citationReference ) {
130
131
		$text = '';
132 8
		$subjects = [];
133
134 8
		$queryResult = $this->findMatchForCitationReference(
135 8
			$citationReference
136
		);
137 8
138
		if ( !$queryResult instanceof \SMWQueryResult ) {
139 8
			return [ $subjects, $text ];
140
		}
141 8
142
		while ( $resultArray = $queryResult->getNext() ) {
143
			foreach ( $resultArray as $result ) {
144
145 8
				// Collect all subjects for the same reference because it can happen
146 8
				// that the same reference key is used for different citation
147
				// resources therefore only return one (the last) valid citation
148
				// text but return all subjects to make it easier to find them later
149
				$subjects[] = $result->getResultSubject();
150
151
				while ( ( $dataValue = $result->getNextDataValue() ) !== false ) {
152 8
					$text = $dataValue->getShortWikiText();
153
				}
154 8
			}
155 8
		}
156 8
157 8
		return [ $subjects, $text ];
158 8
	}
159
160 8
	/**
161
	 * Find match for [[Citation key::SomeKey]]|?Citation text
162
	 *
163
	 * @since 1.0
164
	 *
165
	 * @param string $citationReference
166
	 *
167
	 * @return QueryResult
168
	 */
169
	public function findMatchForCitationReference( $citationReference ) {
170
171
		$description = new SomeProperty(
172 9
			new DIProperty( PropertyRegistry::SCI_CITE_KEY ),
173
			new ValueDescription( new DIBlob( $citationReference ) )
174 8
		);
175 8
176 8
		$propertyValue = $this->dataValueFactory->newDataValueByType( '__pro' );
177 8
		$propertyValue->setDataItem(
178
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
179 8
		);
180 8
181 8
		$description->addPrintRequest(
182 8
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
183
		);
184 8
185 8
		$query = new Query(
186 8
			$description,
187
			false,
188 9
			false
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
189 8
		);
190 8
191
		$query->querymode = Query::MODE_INSTANCES;
192 8
		$query->setLimit( 10 );
193
194 8
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
195 8
			$query->setOption( Query::PROC_CONTEXT, 'SCI.CitationResourceMatchFinder' );
196
		}
197 8
198 8
		return $this->store->getQueryResult( $query );
199 8
	}
200
201
}
202