OLBooksAPIJsonProcessor   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 1
dl 0
loc 109
ccs 74
cts 74
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A doProcess() 0 5 2
C doProcessElements() 0 41 14
A collectClassifications() 0 5 2
A collectSubjects() 0 5 2
A collectAuthors() 0 5 2
B collectIdentifiers() 0 20 6
1
<?php
2
3
namespace Onoi\Remi\OpenLibrary;
4
5
use Onoi\Remi\FilteredRecord;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 0.1
10
 *
11
 * @author mwjames
12
 */
13
class OLBooksAPIJsonProcessor {
14
15
	/**
16
	 * @var FilteredRecord
17
	 */
18
	private $filteredRecord;
19
20
	/**
21
	 * @since 0.1
22
	 *
23
	 * @param FilteredRecord $filteredRecord
24
	 */
25 3
	public function __construct( FilteredRecord $filteredRecord ) {
26 3
		$this->filteredRecord = $filteredRecord;
27 3
	}
28
29
	/**
30
	 * @since 0.1
31
	 *
32
	 * @param array $json
33
	 */
34 3
	public function doProcess( array $json ) {
35 3
		foreach ( $json as $key => $record ) {
36 3
			$this->doProcessElements( $record );
37 3
		}
38 3
	}
39
40 3
	private function doProcessElements( $record ) {
41 3
		foreach ( $record as $key => $value ) {
42
43
			switch ( $key ) {
44 3
				case 'title':
45 3
					$this->filteredRecord->set( 'title', $value );
46 3
					break;
47 3
				case 'subtitle':
48 2
					$this->filteredRecord->set( 'subtitle', $value );
49 2
					break;
50 3
				case 'url':
51 3
					$this->filteredRecord->set( 'url', $value );
52 3
					break;
53 3
				case 'publishers':
54 3
					$this->filteredRecord->set( 'publisher', end( $value ) );
55 3
					break;
56 3
				case 'publish_date':
57 3
					$this->filteredRecord->set( 'pubdate', $value );
58 3
					break;
59 3
				case 'number_of_pages':
60 3
					$this->filteredRecord->set( 'pages', $value );
61 3
					break;
62 3
				case 'classifications':
63 1
					$this->collectClassifications( $value );
64 1
					break;
65 3
				case 'identifiers':
66 3
					$this->collectIdentifiers( $value );
67 3
					break;
68 3
				case 'subject_places':
69 3
				case 'subjects':
70 2
					$this->collectSubjects( $value );
71 2
					break;
72 3
				case 'authors':
73 2
					$this->collectAuthors( $value );
74 2
					break;
75 3
				case 'cover':
76 2
					$this->filteredRecord->set( 'cover', end( $value ) );
77 2
					break;
78
			}
79 3
		}
80 3
	}
81
82 1
	private function collectClassifications( $value ) {
83 1
		foreach ( $value as $id => $v ) {
84 1
			$this->filteredRecord->append( str_replace( '_', ' ', $id ), end( $v ) );
85 1
		}
86 1
	}
87
88 2
	private function collectSubjects( $value ) {
89 2
		foreach ( $value as $id => $v ) {
90 2
			$this->filteredRecord->append( 'subject', $v['name'] );
91 2
		}
92 2
	}
93
94 2
	private function collectAuthors( $value ) {
95 2
		foreach ( $value as $id => $v ) {
96 2
			$this->filteredRecord->append( 'author', $v['name'] );
97 2
		}
98 2
	}
99
100 3
	private function collectIdentifiers( $value ) {
101
102 3
		foreach ( $value as $id => $v ) {
103
104 3
			if ( $id === 'openlibrary' ) {
105 3
				$id = 'olid';
106 3
			}
107
108 3
			if ( $id === 'isbn_13' ||  $id === 'isbn_10' ) {
109 3
				$id = 'isbn';
110 3
			}
111
112
			// No commercial provider
113 3
			if ( !in_array( $id , array( 'isbn', 'olid', 'oclc', 'lccn' ) ) ) {
114 2
				continue;
115
			}
116
117 3
			$this->filteredRecord->append( $id, end( $v ) );
118 3
		}
119 3
	}
120
121
}
122