SimpleItemSerializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 51
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 9 2
A serializeItem() 0 7 1
A getDataSection() 0 9 2
1
<?php
2
3
namespace Queryr\Serialization;
4
5
use Queryr\Resources\SimpleItem;
6
use Serializers\Exceptions\UnsupportedObjectException;
7
use Serializers\Serializer;
8
9
/**
10
 * @access private
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class SimpleItemSerializer implements Serializer {
15
16
	/**
17
	 * @var Serializer
18
	 */
19
	private $foundationalSerializer;
20
21
	/**
22
	 * @var Serializer
23
	 */
24
	private $statementSerializer;
25
26
	/**
27
	 * @var SimpleItem
28
	 */
29
	private $item;
30
31 2
	public function __construct( Serializer $foundationalSerializer, Serializer $statementSerializer ) {
32 2
		$this->foundationalSerializer = $foundationalSerializer;
33 2
		$this->statementSerializer = $statementSerializer;
34 2
	}
35
36 2
	public function serialize( $object ) {
37 2
		if ( !( $object instanceof SimpleItem ) ) {
38 1
			throw new UnsupportedObjectException( $object, 'Can only serialize instances of SimpleItem' );
39
		}
40
41 1
		$this->item = $object;
42
43 1
		return $this->serializeItem();
44
	}
45
46 1
	private function serializeItem() {
47 1
		$serialization = $this->foundationalSerializer->serialize( $this->item );
48
49 1
		$serialization['data'] = $this->getDataSection();
50
51 1
		return $serialization;
52
	}
53
54 1
	private function getDataSection() {
55 1
		$data = [];
56
57 1
		foreach ( $this->item->statements as $simpleStatement ) {
58 1
			$data[$simpleStatement->propertyName] = $this->statementSerializer->serialize( $simpleStatement );
59 1
		}
60
61 1
		return $data;
62
	}
63
64
}