Completed
Push — master ( 910995...d41f9d )
by Aimeos
02:36
created

Standard::entry()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 33
rs 7.3166
cc 11
nc 24
nop 3

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