NcbiPubMedFilteredHttpResponseParser   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.7%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 175
ccs 88
cts 91
cp 0.967
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
C doProcessSummary() 0 59 14
A doProcessAbstract() 0 8 1
A getRawResponseById() 0 6 1
A doFilterResponseById() 0 20 3
A requestSummaryResponseFor() 0 22 1
B requestAbstractResponseFor() 0 24 1
1
<?php
2
3
namespace Onoi\Remi\Ncbi;
4
5
use Onoi\Remi\FilteredHttpResponseParser;
6
use Onoi\Remi\FilteredRecord;
7
use DOMDocument;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 0.1
12
 *
13
 * @author mwjames
14
 */
15
class NcbiPubMedFilteredHttpResponseParser extends FilteredHttpResponseParser {
16
17
	/**
18
	 * @see http://www.ncbi.nlm.nih.gov/books/NBK25501/
19
	 * @see http://www.ncbi.nlm.nih.gov/books/NBK1058/
20
	 */
21
	const SUMMARY_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?";
22
	const FETCH_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?";
23
24
	/**
25
	 * @since 0.1
26
	 *
27
	 * {@inheritDoc}
28
	 */
29 4
	public function getRawResponseById( $id ) {
30
31 4
		$db = $this->filteredRecord->get( 'ncbi-dbtype' );
32
33 4
		return $this->requestSummaryResponseFor( $id, $db ) . $this->requestAbstractResponseFor( $id, $db );
34
	}
35
36
	/**
37
	 * @since 0.1
38
	 *
39
	 * {@inheritDoc}
40
	 */
41 6
	public function doFilterResponseById( $id ) {
42
43 6
		$db = $this->filteredRecord->get( 'ncbi-dbtype' );
44
45 6
		$text = $this->requestSummaryResponseFor( $id, $db );
46
47 6
		if ( $this->httpRequest->getLastError() !== '' ) {
48 1
			return $this->addMessage( array( 'onoi-remi-request-error', $this->httpRequest->getLastError(), $id ) );
49
		}
50
51 5
		$this->doProcessSummary( $text, $id, $db );
52
53 5
		if ( $this->getMessages() !== array() ) {
54 1
			return null;
55
		}
56
57 4
		$this->doProcessAbstract(
58 4
			$this->requestAbstractResponseFor( $id, $db )
59 4
		);
60 4
	}
61
62 5
	private function doProcessSummary( $text, $id, $db ) {
63
64 5
		$result = json_decode(
65 5
			$text,
66
			true
67 5
		);
68
69 5
		if ( !isset( $result['result'][$id] ) ) {
70 1
			return $this->addMessage( array( 'onoi-remi-response-empty', $id ) );
71
		}
72
73 4
		$record = $result['result'][$id];
74
75 4
		if ( isset( $record['error'] ) ) {
76
			return $this->addMessage( array( 'onoi-remi-request-error', $record['error'], $id ) );
77
		}
78
79 4
		if ( isset( $record['pubtype'] ) ) {
80 1
			foreach ( $record['pubtype'] as $type ) {
81 1
				$this->filteredRecord->append( 'type', $type );
82 1
			}
83 1
		}
84
85 4
		foreach ( $record['articleids'] as $articleids ) {
86
87 4
			if ( $articleids['idtype'] === 'doi' ) {
88 3
				$this->filteredRecord->set( 'doi', $articleids['value'] );
89 3
			}
90
91 4
			if ( $articleids['idtype'] === 'pmid' ) {
92 3
				$this->filteredRecord->set( 'pmid', $articleids['value'] );
93 3
			}
94
95 4
			if ( $articleids['idtype'] === 'pubmed' ) {
96 1
				$this->filteredRecord->set( 'pmid', $articleids['value'] );
97 1
			}
98
99 4
			if ( $articleids['idtype'] === 'pmcid' && $db === 'pmc' ) {
100 3
				$this->filteredRecord->set( 'pmcid', $articleids['value'] );
101 3
			}
102
103 4
			if ( $articleids['idtype'] === 'pmc' && $db === 'pubmed' ) {
104
				$this->filteredRecord->set( 'pmcid', $articleids['value'] );
105
			}
106 4
		}
107
108 4
		foreach ( $record['authors'] as $author ) {
109 3
			$this->filteredRecord->append( 'author', $author['name'] );
110 4
		}
111
112 4
		$this->filteredRecord->set( 'title', $record['title'] );
113 4
		$this->filteredRecord->set( 'journal', $record['fulljournalname'] );
114 4
		$this->filteredRecord->set( 'pubdate', $record['pubdate'] );
115 4
		$this->filteredRecord->set( 'volume', $record['volume'] );
116 4
		$this->filteredRecord->set( 'issue', $record['issue'] );
117 4
		$this->filteredRecord->set( 'pages', $record['pages'] );
118
119 4
		$this->filteredRecord->set( 'retrieved-from', 'http://www.ncbi.nlm.nih.gov/' );
120 4
	}
121
122 4
	private function doProcessAbstract( $xml ) {
123
124 4
		$ncbiEntrezAbstractXMLProcessor = new NcbiEntrezAbstractXMLProcessor(
125 4
			$this->filteredRecord
126 4
		);
127
128 4
		$ncbiEntrezAbstractXMLProcessor->doProcess( $xml );
129 4
	}
130
131
	/**
132
	 * @param string $id
133
	 *
134
	 * @return string
135
	 */
136 6
	public function requestSummaryResponseFor( $id, $type ) {
137
138 6
		$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true ); // put result into variable
139 6
		$this->httpRequest->setOption( CURLOPT_FAILONERROR, true );
140
141 6
		$this->httpRequest->setOption( CURLOPT_URL, self::SUMMARY_URL );
142 6
		$this->httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
143
144 6
		$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array(
145 6
			'Accept: application/sparql-results+json, application/rdf+json, application/json',
146
			'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
147 6
		) );
148
149 6
		$this->httpRequest->setOption( CURLOPT_POST, true );
150
151 6
		$this->httpRequest->setOption(
152 6
			CURLOPT_POSTFIELDS,
153 6
			"db={$type}&retmode=json&id=" . $id
154 6
		);
155
156 6
		return $this->httpRequest->execute();
157
	}
158
159
	/**
160
	 * @param string $id
161
	 *
162
	 * @return string
163
	 */
164 4
	public function requestAbstractResponseFor( $id, $type ) {
165
166
		// http://www.fredtrotter.com/2014/11/14/hacking-on-the-pubmed-api/
167
168 4
		$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true ); // put result into variable
169 4
		$this->httpRequest->setOption( CURLOPT_FAILONERROR, true );
170
171 4
		$this->httpRequest->setOption( CURLOPT_URL, self::FETCH_URL );
172 4
		$this->httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
173
174 4
		$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array(
175 4
			'Accept: application/sparql-results+xml, application/rdf+xml, application/xml',
176
			'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
177 4
		) );
178
179 4
		$this->httpRequest->setOption( CURLOPT_POST, true );
180
181 4
		$this->httpRequest->setOption(
182 4
			CURLOPT_POSTFIELDS,
183 4
			"db={$type}&rettype=abstract&retmode=xml&id=" . $id
184 4
		);
185
186 4
		return $this->httpRequest->execute();
187
	}
188
189
}
190