Passed
Push — master ( 03797e...f65fd3 )
by Aimeos
02:45
created

Standard::map()   C

Complexity

Conditions 16
Paths 33

Size

Total Lines 65
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 65
rs 5.5666
nc 33
nop 3
cc 16

How to fix   Long Method    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\Jincluded;
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
	 * @param array $fcn Associative list of resource types as keys and anonymous mapping functions are values
31
	 * @return array List of entries to include in the JSON:API response
32
	 */
33
	public function transform( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
34
	{
35
		$this->map = [];
36
37
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
38
		{
39
			foreach( $item->getChildren() as $catItem )
40
			{
41
				if( $catItem->isAvailable() ) {
42
					$this->map( $catItem, $fields, $fcn );
43
				}
44
			}
45
		}
46
47
		if( $item instanceof \Aimeos\MShop\Common\Item\AddressRef\Iface )
48
		{
49
			foreach( $item->getAddressItems() as $addrItem ) {
50
				$this->map( $addrItem, $fields, $fcn );
51
			}
52
		}
53
54
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
55
		{
56
			foreach( $item->getListItems() as $listItem )
57
			{
58
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
59
					$this->map( $refItem, $fields, $fcn );
60
				}
61
			}
62
		}
63
64
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
65
		{
66
			foreach( $item->getPropertyItems() as $propItem ) {
67
				$this->map( $propItem, $fields, $fcn );
68
			}
69
		}
70
71
		$result = [];
72
73
		foreach( $this->map as $list )
74
		{
75
			foreach( $list as $entry ) {
76
				$result[] = $entry;
77
			}
78
		}
79
80
		return $result;
81
	}
82
83
84
	/**
85
	 * Returns the included data for the JSON:API response
86
	 *
87
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
88
	 * @param array $fields Associative list of resource types as keys and field names to output as values
89
	 * @param array $fcn Associative list of resource types as keys and anonymous mapping functions are values
90
	 * @return array Multi-dimensional array of included data
91
	 */
92
	protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
93
	{
94
		$id = $item->getId();
95
		$type = $item->getResourceType();
96
97
		if( isset( $this->map[$type][$id] ) || !$item->isAvailable() ) {
98
			return;
99
		}
100
101
		$attributes = $item->toArray();
102
103
		if( isset( $fields[$type] ) ) {
104
			$attributes = array_intersect_key( $attributes, $fields[$type] );
105
		}
106
107
		$entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes];
108
109
		if( isset( $fcn[$type] ) && $fcn[$type] instanceof \Closure ) {
110
			$entry = $fcn[$type]( $item, $entry );
111
		}
112
113
		$this->map[$type][$id] = $entry; // first content, avoid infinite loops
114
115
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
116
		{
117
			foreach( $item->getChildren() as $childItem )
118
			{
119
				if( $childItem->isAvailable() )
120
				{
121
					$rtype = $childItem->getResourceType();
122
					$entry['relationships'][$rtype]['data'][] = ['id' => $childItem->getId(), 'type' => $rtype];
123
					$this->map( $childItem, $fields, $fcn );
124
				}
125
			}
126
		}
127
128
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
129
		{
130
			foreach( $item->getListItems() as $listItem )
131
			{
132
				if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() )
133
				{
134
					$rtype = $refItem->getResourceType();
135
					$data = ['id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $listItem->toArray()];
136
					$entry['relationships'][$rtype]['data'][] = $data;
137
					$this->map( $refItem, $fields, $fcn );
138
				}
139
			}
140
		}
141
142
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
143
		{
144
			foreach( $item->getPropertyItems() as $propItem )
145
			{
146
				if( $propItem->isAvailable() )
147
				{
148
					$propId = $propItem->getId();
149
					$rtype = $propItem->getResourceType();
150
					$entry['relationships'][$rtype]['data'][] = ['id' => $propId, 'type' => $rtype];
151
					$this->map( $propItem, $fields, $fcn );
152
				}
153
			}
154
		}
155
156
		$this->map[$type][$id] = $entry; // full content
157
	}
158
}
159