Completed
Push — master ( f04487...03797e )
by Aimeos
04:01
created

Standard::transform()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 38
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 38
rs 7.3166
cc 11
nc 24
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Included;
12
13
14
/**
15
 * View helper class for generating "included" data used by JSON:API
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Standard extends \Aimeos\MW\View\Helper\Base implements Iface
21
{
22
	private $map;
23
24
25
	/**
26
	 * Returns the included data for the JSON:API response
27
	 *
28
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
29
	 * @param array $fields Associative list of resource types as keys and field names to output as values
30
	 * @return array List of entries to include in the JSON:API response
31
	 */
32
	public function transform( \Aimeos\MShop\Common\Item\Iface $item, array $fields )
33
	{
34
		$this->map = [];
35
36
		if( $item instanceof \Aimeos\MShop\Common\Item\AddressRef\Iface )
37
		{
38
			foreach( $item->getAddressItems() as $addItem ) {
39
				$this->map( $addItem, $fields );
40
			}
41
		}
42
43
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
44
		{
45
			foreach( $item->getListItems() as $listItem )
46
			{
47
				if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() ) {
48
					$this->map( $refItem, $fields );
49
				}
50
			}
51
		}
52
53
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
54
		{
55
			foreach( $item->getPropertyItems() as $propertyItem ) {
56
				$this->map( $propertyItem, $fields );
57
			}
58
		}
59
60
		$result = [];
61
62
		foreach( $this->map as $list )
63
		{
64
			foreach( $list as $entry ) {
65
				$result[] = $entry;
66
			}
67
		}
68
69
		return $result;
70
	}
71
72
73
	/**
74
	 * Returns the included data for the JSON:API response
75
	 *
76
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
77
	 * @param array $fields Associative list of resource types as keys and field names to output as values
78
	 * @return array Multi-dimensional array of included data
79
	 */
80
	protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields )
81
	{
82
		$id = $item->getId();
83
		$type = $item->getResourceType();
84
85
		if( isset( $this->map[$type][$id] ) ) {
86
			return;
87
		}
88
89
		$attributes = $item->toArray();
90
91
		if( isset( $fields[$type] ) ) {
92
			$attributes = array_intersect_key( $attributes, $fields[$type] );
93
		}
94
95
		$entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes];
96
		$this->map[$type][$id] = $entry; // first content, avoid infinite loops
97
98
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
99
		{
100
			foreach( $item->getListItems() as $listItem )
101
			{
102
				if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() )
103
				{
104
					$reftype = $refItem->getResourceType();
105
					$data = ['id' => $refItem->getId(), 'type' => $reftype, 'attributes' => $listItem->toArray()];
106
					$entry['relationships'][$reftype]['data'][] = $data;
107
					$this->map( $refItem, $fields );
108
				}
109
			}
110
		}
111
112
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
113
		{
114
			foreach( $item->getPropertyItems() as $propItem )
115
			{
116
				$propId = $propItem->getId();
117
				$propType = $propItem->getResourceType();
118
				$entry['relationships'][$propType]['data'][] = ['id' => $propId, 'type' => $propType];
119
				$this->map( $propItem, $fields );
120
			}
121
		}
122
123
		$this->map[$type][$id] = $entry; // full content
124
	}
125
}
126