Completed
Pull Request — master (#77)
by mw
02:46 queued 01:34
created

PageBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 8
dl 0
loc 253
ccs 90
cts 110
cp 0.8182
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isReadOnly() 0 3 1
A getRawResponseFor() 0 12 1
A getHtmlFor() 0 38 3
B doRenderHtml() 0 61 5
B prepareLog() 0 34 6
A getTypeIdIntroText() 0 14 2
A tryToFindCitationResourceMatches() 0 20 4
1
<?php
2
3
namespace SCI\Specials\CitableMetadata;
4
5
use SMW\ApplicationFactory;
6
use SCI\CitationResourceMatchFinder;
7
use SMW\MediaWiki\Renderer\HtmlFormRenderer;
8
use SMW\MediaWiki\Renderer\HtmlColumnListRenderer;
9
use SCI\FilteredMetadata\HttpResponseParserFactory;
10
use SCI\FilteredMetadata\BibliographicFilteredRecord;
11
12
/**
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class PageBuilder {
19
20
	/**
21
	 * @var HtmlFormRenderer
22
	 */
23
	private $htmlFormRenderer;
24
25
	/**
26
	 * @var HtmlColumnListRenderer
27
	 */
28
	private $htmlColumnListRenderer;
29
30
	/**
31
	 * @var CitationResourceMatchFinder
32
	 */
33
	private $citationResourceMatchFinder;
34
35
	/**
36
	 * @var HttpResponseParserFactory
37
	 */
38
	private $httpResponseParserFactory;
39
40
	/**
41
	 * @var boolean
42
	 */
43
	private $isReadOnly = false;
44
45
	/**
46
	 * @since 1.0
47
	 *
48
	 * @param HtmlFormRenderer $htmlFormRenderer
49
	 * @param HtmlColumnListRenderer $htmlColumnListRenderer
50
	 * @param CitationResourceMatchFinder $citationResourceMatchFinder
51
	 * @param HttpResponseParserFactory $httpResponseParserFactory
52
	 */
53 3
	public function __construct( HtmlFormRenderer $htmlFormRenderer, HtmlColumnListRenderer $htmlColumnListRenderer, CitationResourceMatchFinder $citationResourceMatchFinder, HttpResponseParserFactory $httpResponseParserFactory ) {
54 3
		$this->htmlFormRenderer = $htmlFormRenderer;
55 3
		$this->htmlColumnListRenderer = $htmlColumnListRenderer;
56 3
		$this->citationResourceMatchFinder = $citationResourceMatchFinder;
57 3
		$this->httpResponseParserFactory = $httpResponseParserFactory;
58 3
	}
59
60
	/**
61
	 * @since 1.4
62
	 *
63
	 * @param boolean $isReadOnly
64
	 */
65
	public function isReadOnly( $isReadOnly ) {
66
		$this->isReadOnly = (bool)$isReadOnly;
67
	}
68
69
	/**
70
	 * @since 1.0
71
	 *
72
	 * @param string $type
73
	 * @param string $id
74
	 *
75
	 * @return string
76
	 */
77 1
	public function getRawResponseFor( $type, $id ) {
78
79 1
		$responseParser = $this->httpResponseParserFactory->newResponseParserForType(
80 1
			$type
81
		);
82
83 1
		$htmlResponseParserRenderer = new HtmlResponseParserRenderer(
84 1
			$responseParser
85
		);
86
87 1
		return $htmlResponseParserRenderer->getRawResponse( $id );
88
	}
89
90
	/**
91
	 * @since 1.0
92
	 *
93
	 * @param string $type
94
	 * @param string $id
95
	 *
96
	 * @return string
97
	 */
98 1
	public function getHtmlFor( $type, $id ) {
99
100 1
		$text = '';
101 1
		$success = true;
102 1
		$log = '';
103 1
		$matches = '';
104
105 1
		if ( $type !== '' && $id !== '' ) {
106
107 1
			$responseParser = $this->httpResponseParserFactory->newResponseParserForType(
108 1
				$type
109
			);
110
111 1
			$htmlResponseParserRenderer = new HtmlResponseParserRenderer(
112 1
				$responseParser
113
			);
114
115 1
			$htmlResponseParserRenderer->isReadOnly(
116 1
				$this->isReadOnly
117
			);
118
119 1
			$text = $htmlResponseParserRenderer->renderTextFor( $id );
120
121 1
			$matches = $this->tryToFindCitationResourceMatches(
122 1
				$responseParser->getFilteredRecord()
0 ignored issues
show
Compatibility introduced by
$responseParser->getFilteredRecord() of type object<Onoi\Remi\FilteredRecord> is not a sub-type of object<SCI\FilteredMetad...ographicFilteredRecord>. It seems like you assume a child class of the class Onoi\Remi\FilteredRecord to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
123
			);
124
125 1
			$success = $responseParser->getMessages() === [];
126
127 1
			$log = $this->prepareLog(
128 1
				$responseParser->getMessages(),
129 1
				$matches,
130 1
				$responseParser->usesCache()
131
			);
132
		}
133
134 1
		return $this->doRenderHtml( $type, $id, $success, $text, $log, $matches );
135
	}
136
137 1
	private function doRenderHtml( $type, $id, $success, $text, $log, $matches ) {
138
139 1
		$htmlFormRenderer = $this->htmlFormRenderer;
140 1
		$messageBuilder = $this->htmlFormRenderer->getMessageBuilder();
141
142
		$types = [
143 1
			'pubmed' => 'PMID',
144
			'pmc'  => 'PMCID',
145
			'doi'  => 'DOI',
146
			'oclc' => 'OCLC',
147
			'viaf' => 'VIAF',
148
			'ol'   => 'OLID',
149
		];
150
151
152 1
		if ( $matches !== '' ) {
153
			$htmlFormRenderer->addParagraph(
154
				'<div class="smw-callout smw-callout-info">' .
155
				$messageBuilder->getMessage( 'sci-metadata-search-has-match', $matches )->text() .
156
				'</div>'
157
			);
158
		}
159
160 1
		$html = $htmlFormRenderer->setName( 'sci-metadata-search-form' )
161 1
			->withFieldset()
162 1
			->setMethod( 'get' )
163 1
			->addParagraph( $messageBuilder->getMessage( 'sci-metadata-search-intro' )->parse() )
164 1
			->addParagraph( $this->getTypeIdIntroText( $messageBuilder ) )
165 1
			->addHorizontalRule()
166 1
			->addOptionSelectList(
167 1
				$messageBuilder->getMessage( 'sci-metadata-search-select-label' )->text(),
168 1
				'type',
169 1
				$type,
170 1
				$types )
171 1
			->addInputField(
172 1
				'',
173 1
				'id',
174 1
				$id,
175 1
				'id',
176 1
				40 )
177 1
			->addNonBreakingSpace()
178 1
			->addSubmitButton( $messageBuilder->getMessage( 'sci-metadata-search-form-submit' )->text() )
179 1
			->addNonBreakingSpace()
180 1
			->addCheckbox( 'Raw', 'format', 'raw' )
181 1
			->getForm();
182
183 1
		if ( $text !== '' && $success ) {
184
			$htmlFormRenderer
185 1
				->addHeader( 'h2', $messageBuilder->getMessage( 'sci-metadata-search-header-result' )->text() )
186 1
				->addParagraph( $text );
187
		}
188
189 1
		if ( $log !== '' ) {
190
			$htmlFormRenderer
191
				->setName( 'metadata-match' )
192
				->addHeader( 'h2', $messageBuilder->getMessage( 'sci-metadata-search-header-log' )->text() )
193
				->addParagraph( $log );
194
		}
195
196 1
		return $html . $htmlFormRenderer->getForm();
197
	}
198
199 1
	private function prepareLog( $messages, $matches, $usesCache ) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
200
201 1
		$messageBuilder = $this->htmlFormRenderer->getMessageBuilder();
202
203 1
		$log = [];
204
205 1
		foreach ( $messages as $m ) {
206
207
			if ( call_user_func_array( 'wfMessage', $m )->exists() ) {
208
				$m = call_user_func_array( 'wfMessage', $m )->parse();
209
			} else {
210
				$m = current( $m );
211
			}
212
213
			$log[]  = $m;
214
		}
215
216 1
		if ( $usesCache ) {
217
			$log[] = $messageBuilder->getMessage( 'sci-metadata-search-cached' )->text();
218
		}
219
220 1
		if ( $this->isReadOnly ) {
221
			$log[] = $messageBuilder->getMessage( 'sci-metadata-search-read-only' )->text();
222
		}
223
224 1
		if ( $log === [] ) {
225 1
			return '';
226
		}
227
228
		$this->htmlColumnListRenderer->addContentsByNoIndex( $log );
229
		$this->htmlColumnListRenderer->setNumberOfColumns( 1 );
230
231
		return $this->htmlColumnListRenderer->getHtml();
232
	}
233
234 1
	private function getTypeIdIntroText( $messageBuilder ) {
235
236 1
		$explain = [];
237
238 1
		foreach ( [ 'doi', 'oclc', 'pubmed', 'ol', 'viaf' ] as $value ) {
239 1
			$explain[] = $messageBuilder->getMessage( 'sci-metadata-search-intro-'. $value )->parse();
240
		}
241
242 1
		$this->htmlColumnListRenderer->setColumnListClass( 'scite-metadata-search-types' );
243 1
		$this->htmlColumnListRenderer->addContentsByNoIndex( $explain );
244 1
		$this->htmlColumnListRenderer->setNumberOfColumns( 2 );
245
246 1
		return $this->htmlColumnListRenderer->getHtml();
247
	}
248
249 1
	private function tryToFindCitationResourceMatches( BibliographicFilteredRecord $bibliographicFilteredRecord ) {
250
251 1
		$html = [];
252
253 1
		foreach ( [ 'doi', 'oclc', 'viaf', 'olid', 'pubmed', 'pmc' ] as $type ) {
254 1
			$subjects = $this->citationResourceMatchFinder->findMatchForResourceIdentifierTypeToValue(
255 1
				$type,
256 1
				$bibliographicFilteredRecord->getSearchMatchSetValueFor( $type )
257
			);
258
259 1
			if ( $subjects !== [] ) {
260
				$html = array_merge(
261
					$html,
262 1
					$this->citationResourceMatchFinder->findCitationResourceLinks( $subjects, '', strtoupper( $type ) )
263
				);
264
			}
265
		}
266
267 1
		return $html !== [] ? '<strong>' . implode( ', ', $html ) . '</strong>' : '';
268
	}
269
270
}
271