Completed
Push — master ( 2902cd...b8eebc )
by mw
10s
created

doFilterResponseById()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

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