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

doFilterResponseById()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Onoi\Remi\Oclc;
4
5
use Onoi\Remi\FilteredHttpResponseParser;
6
use Onoi\Remi\FilteredRecord;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 0.1
11
 *
12
 * @author mwjames
13
 */
14
class OclcFilteredHttpResponseParser extends FilteredHttpResponseParser {
15
16
	/**
17
	 * @see http://dataliberate.com/2013/06/content-negotiation-for-worldcat/
18
	 */
19
	const OCLC_REST = "http://www.worldcat.org/oclc/";
20
21
	/**
22
	 * @since 0.1
23
	 *
24
	 * {@inheritDoc}
25
	 */
26 5
	public function getRawResponseById( $oclcID ) {
27 5
		return $this->requestResponseFor( $oclcID );
28
	}
29
30
	/**
31
	 * @since 0.1
32
	 *
33
	 * {@inheritDoc}
34
	 */
35 7
	public function doFilterResponseById( $oclcID ) {
36
37 7
		$text = $this->requestResponseFor( $oclcID );
38
39 7
		if ( $this->httpRequest->getLastError() !== '' ) {
40 1
			return $this->addMessage( array( 'onoi-remi-request-error', $this->httpRequest->getLastError(), $oclcID ) );
41
		}
42
43 6
		$jsonld = json_decode( $text, true );
44
45 6
		if ( $jsonld === null || $jsonld === '' ) {
46 1
			return $this->addMessage( array( 'onoi-remi-response-empty', $oclcID ) );
47
		}
48
49 5
		$this->doProcessJsonLd( $oclcID, $jsonld );
50
51 5
		$this->filteredRecord->set( 'retrieved-from', 'http://www.worldcat.org/' );
52 5
	}
53
54 5
	private function doProcessJsonLd( $oclcID, $jsonld ) {
55
56 5
		$simpleOclcJsonLdGraphProcessor = new SimpleOclcJsonLdGraphProcessor(
57 5
			$this->filteredRecord
58 5
		);
59
60 5
		$simpleOclcJsonLdGraphProcessor->doProcess( $oclcID, $jsonld );
61 5
	}
62
63
	/**
64
	 * @param string $id
65
	 *
66
	 * @return string
67
	 */
68 7
	public function requestResponseFor( $id ) {
69
70 7
		$this->httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
71
72 7
		$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
73 7
		$this->httpRequest->setOption( CURLOPT_FAILONERROR, true );
74 7
		$this->httpRequest->setOption( CURLOPT_URL, self::OCLC_REST . $id );
75
76 7
		$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array(
77 7
			'Accept: application/ld+json',
78
			'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
79 7
		) );
80
81 7
		return $this->httpRequest->execute();
82
	}
83
84
}
85