SimpleOclcJsonLdGraphProcessor   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 1
dl 0
loc 138
ccs 84
cts 84
cp 1
rs 8.2857
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A doProcess() 0 5 2
B doProcessGraphElementsFor() 0 11 6
B doProcessOtherElements() 0 19 8
C doProcessTheAboutElement() 0 28 11
A collectIsbn() 0 5 2
A collectGenre() 0 14 4
B collectAbstract() 0 22 5
1
<?php
2
3
namespace Onoi\Remi\Oclc;
4
5
use Onoi\Remi\FilteredRecord;
6
7
/**
8
 * This is a very simple graph parser for metadata we want to harvest from a
9
 * REST response.
10
 *
11
 * @license GNU GPL v2+
12
 * @since 0.1
13
 *
14
 * @author mwjames
15
 */
16
class SimpleOclcJsonLdGraphProcessor {
17
18
	/**
19
	 * @var FilteredRecord
20
	 */
21
	private $filteredRecord;
22
23
	/**
24
	 * @since 0.1
25
	 *
26
	 * @param FilteredRecord $filteredRecord
27
	 */
28 5
	public function __construct( FilteredRecord $filteredRecord ) {
29 5
		$this->filteredRecord = $filteredRecord;
30 5
	}
31
32
	/**
33
	 * @since 0.1
34
	 *
35
	 * @param string $oclcID
36
	 * @param array $jsonld
37
	 */
38 5
	public function doProcess( $oclcID, array $jsonld ) {
39 5
		foreach ( $jsonld as $graph ) {
40 5
			$this->doProcessGraphElementsFor( $oclcID, $graph );
41 5
		}
42 5
	}
43
44
	/**
45
	 * @param string $oclcID
46
	 * @param array $graph
47
	 */
48 5
	private function doProcessGraphElementsFor( $oclcID, array $graph ) {
49 5
		foreach ( $graph as $element ) {
50 5
			$this->doProcessOtherElements( $element );
51
52
			// Looking for the tree component that hosts the metadata
53 5
			if ( ( isset( $element['oclcnum'] ) && $element['oclcnum'] == $oclcID ) ||
54 5
				( isset( $element['library:oclcnum'] ) && $element['library:oclcnum'] == $oclcID ) ) {
55 5
				$this->doProcessTheAboutElement( $element );
56 5
			}
57 5
		}
58 5
	}
59
60 5
	private function doProcessOtherElements( $element ) {
61 5
		if ( isset( $element['isbn'] ) ) {
62 4
			$this->collectIsbn( $element['isbn'] );
63 4
		}
64
65
		// Looking only for an entity of schema:Person
66 5
		if ( isset( $element['@type'] ) && $element['@type'] === 'schema:Person' && strpos( $element['@id'] , 'viaf' ) !== false ) {
67 4
			$this->filteredRecord->append(
68 4
				'viaf',
69 4
				substr( $element['@id'], strrpos( $element['@id'], '/' ) + 1 ) // get the ID not the URL
70 4
			);
71 4
			$this->filteredRecord->append( 'author', $element['name'] );
72 4
		}
73
74
		// Looking only for an entity of schema:Intangible
75 5
		if ( isset( $element['@type'] ) && $element['@type'] === 'schema:Intangible' && isset( $element['name']['@value'] ) ) {
76 4
			$this->filteredRecord->append( 'subject', $element['name']['@value'] );
77 4
		}
78 5
	}
79
80 5
	private function doProcessTheAboutElement( array $values ) {
81 5
		foreach ( $values as $key => $value ) {
82
83
			switch ( $key ) {
84 5
				case '@id':
85 5
					$this->filteredRecord->set( 'url', $value );
86 5
					break;
87 5
				case '@type':
88 5
					$this->filteredRecord->set( 'type', is_string( $value ) ? array( $value) : $value );
89 5
					break;
90 5
				case 'datePublished':
91 5
					$this->filteredRecord->set( 'pubdate', $value );
92 5
					break;
93 5
				case 'description':
94 3
					$this->collectAbstract( $value );
95 3
					break;
96 5
				case 'bookEdition':
97 3
					$this->filteredRecord->set( 'edition', $value );
98 3
					break;
99 5
				case 'name':
100 5
					$this->filteredRecord->set( 'title', is_array( $value ) ? $value['@value'] : $value );
101 5
					break;
102 5
				case 'genre':
103 4
					$this->collectGenre( $value );
104 4
					break;
105
			}
106 5
		}
107 5
	}
108
109 4
	private function collectIsbn( array $values ) {
110 4
		foreach ( $values as $value ) {
111 4
			$this->filteredRecord->append( 'isbn', $value );
112 4
		}
113 4
	}
114
115 4
	private function collectGenre( $values ) {
116
117 4
		foreach ( $values as $key => $value ) {
118
119 4
			if ( is_array( $value ) ) {
120 3
				$this->collectGenre( $value );
121 3
				continue;
122
			}
123
124 4
			if ( $key === '@value' ) {
125 4
				$this->filteredRecord->append( 'genre', $value );
126 4
			}
127 4
		}
128 4
	}
129
130 3
	private function collectAbstract( $values ) {
131
132 3
		foreach ( $values as $key => $value ) {
133
134 3
			if ( is_array( $value ) ) {
135 2
				$this->collectAbstract( $value );
136 2
				continue;
137
			}
138
139
			// This is pure guess work since the description list is
140
			// an unordered list with no indication of what type of
141
			// content is stored, use `--` is indicator that is not
142
			// the abstract
143 3
			if ( strpos( $value, '--' ) !== false ) {
144 2
				continue;
145
			}
146
147 3
			if ( $key === '@value' ) {
148 2
				$this->filteredRecord->append( 'abstract', $value );
149 2
			}
150 3
		}
151 3
	}
152
153
}
154