PageBuilder::prepareLog()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 11.3413

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 33
ccs 8
cts 17
cp 0.4706
rs 9.0777
cc 6
nc 24
nop 3
crap 11.3413
1
<?php
2
3
namespace SCI\Specials\CitableMetadata;
4
5
use SMW\Services\ServicesFactory as ApplicationFactory;
0 ignored issues
show
Bug introduced by
The type SMW\Services\ServicesFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SCI\CitationResourceMatchFinder;
7
use SMW\MediaWiki\Renderer\HtmlFormRenderer;
0 ignored issues
show
Bug introduced by
The type SMW\MediaWiki\Renderer\HtmlFormRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SMW\MediaWiki\Renderer\HtmlColumnListRenderer;
0 ignored issues
show
Bug introduced by
The type SMW\MediaWiki\Renderer\HtmlColumnListRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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()
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

199
	private function prepareLog( $messages, /** @scrutinizer ignore-unused */ $matches, $usesCache ) {

This check looks for 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