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

getRawResponseById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Onoi\Remi\Viaf;
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 ViafFilteredHttpResponseParser extends FilteredHttpResponseParser {
16
17
	/**
18
	 * @see http://crosscite.org/cn/
19
	 */
20
	const VIAF_REST = "http://viaf.org/viaf/";
21
22
	/**
23
	 * @since 0.1
24
	 *
25
	 * {@inheritDoc}
26
	 */
27 4
	public function getRawResponseById( $viafID ) {
28 4
		return $this->requestResponseFor( $viafID );
29
	}
30
31
	/**
32
	 * @since 0.1
33
	 *
34
	 * {@inheritDoc}
35
	 */
36 6
	public function doFilterResponseById( $viafID ) {
37
38 6
		$xml = $this->requestResponseFor( $viafID );
39
40 6
		if ( $this->httpRequest->getLastError() !== '' ) {
41 1
			return $this->addMessage( array( 'onoi-remi-request-error', $this->httpRequest->getLastError(), $viafID ) );
42
		}
43
44 5
		if ( $xml === null || $xml === '' || $xml === false ) {
45 1
			return $this->addMessage( array( 'onoi-remi-response-empty', $viafID ) );
46
		}
47
48 4
		$dom = new DOMDocument();
49 4
		$dom->loadXml( $xml );
50
51 4
		$this->doProcessDom( $dom, $viafID );
52
53 4
		$this->filteredRecord->set( 'retrieved-from', 'http://viaf.org/' );
54 4
	}
55
56 4
	private function doProcessDom( $dom, $viafID ) {
57
58 4
		$viaf = '';
59
60 4
		foreach ( $dom->getElementsByTagName( 'viafID' ) as $item ) {
61 4
			$viaf = $item->nodeValue;
62 4
		}
63
64 4
		if ( $viaf != $viafID ) {
65
			return $this->addMessage( array( 'onoi-remi-parser-no-id-match', $viafID ) );
66
		}
67
68 4
		$this->filteredRecord->set( 'viaf', $viafID );
69
70 4
		foreach ( $dom->getElementsByTagName( 'nameType' ) as $item ) {
71 4
			$this->filteredRecord->set( 'type', $item->nodeValue );
72 4
		}
73
74 4
		foreach ( $dom->getElementsByTagName( 'sources' ) as $item ) {
75 4
			foreach ( $item->getElementsByTagName( 'source' ) as $i ) {
76 4
				list( $key, $value ) = explode( '|', $i->nodeValue, 2 );
77 4
				$this->filteredRecord->set( strtolower( $key ), str_replace( ' ' ,'', $value ) );
78 4
			}
79 4
		}
80
81
		// Not sure what we want to search/iterate for therefore stop after the
82
		// first data/name element
83 4
		foreach ( $dom->getElementsByTagName( 'data' ) as $item ) {
84
85 4
			foreach ( $item->getElementsByTagName( 'text' ) as $i ) {
86 4
				$this->filteredRecord->set( 'name', str_replace( '.', '', $i->nodeValue ) );
87 4
				break;
88 4
			}
89
90 4
			break;
91 4
		}
92 4
	}
93
94
	/**
95
	 * @param string $id
96
	 *
97
	 * @return string
98
	 */
99 6
	private function requestResponseFor( $id ) {
100
101 6
		$this->httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
102
103 6
		$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
104 6
		$this->httpRequest->setOption( CURLOPT_FAILONERROR, true );
105 6
		$this->httpRequest->setOption( CURLOPT_URL, self::VIAF_REST . $id );
106
107 6
		$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array(
108 6
			'Accept: application/xml',
109
			'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'
110 6
		) );
111
112 6
		return $this->httpRequest->execute();
113
	}
114
115
}
116