Passed
Push — master ( 4eaa64...ddab28 )
by Aimeos
08:45
created

Standard::entry()   C

Complexity

Conditions 14
Paths 80

Size

Total Lines 44
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 44
rs 6.2666
cc 14
nc 80
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-2021
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 functions for generating the array entries as 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_map( $item ) || 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
		return $this->map;
49
	}
50
51
52
	/**
53
	 * Processes a single item to create the included data for the JSON:API response
54
	 *
55
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
56
	 * @param array $fields Associative list of resource types as keys and field names to output as values
57
	 * @param array $fcn Associative list of resource types as keys and anonymous functions for generating the array entries as values
58
	 */
59
	protected function entry( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
60
	{
61
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
62
		{
63
			foreach( $item->getChildren() as $catItem )
64
			{
65
				if( $catItem->isAvailable() ) {
66
					$this->map( $catItem, $fields, $fcn );
67
				}
68
			}
69
		}
70
71
		if( $item instanceof \Aimeos\MShop\Common\Item\AddressRef\Iface )
72
		{
73
			foreach( $item->getAddressItems() as $addrItem ) {
74
				$this->map( $addrItem, $fields, $fcn );
75
			}
76
		}
77
78
		if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface )
79
		{
80
			foreach( $item->getListItems() as $listItem )
81
			{
82
				if( $refItem = $listItem->getRefItem() ) {
83
					$this->map( $refItem, $fields, $fcn );
84
				}
85
			}
86
		}
87
88
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
89
		{
90
			foreach( $item->getPropertyItems() as $propItem ) {
91
				$this->map( $propItem, $fields, $fcn );
92
			}
93
		}
94
95
		if( $item instanceof \Aimeos\MShop\Product\Item\Iface )
96
		{
97
			foreach( $item->getCatalogItems() as $catItem ) {
98
				$this->map( $catItem, $fields, $fcn );
99
			}
100
101
			foreach( $item->getStockItems() as $stockItem ) {
102
				$this->map( $stockItem, $fields, $fcn );
103
			}
104
		}
105
	}
106
107
108
	/**
109
	 * Populates the map class property with the included data for the JSON:API response
110
	 *
111
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
112
	 * @param array $fields Associative list of resource types as keys and field names to output as values
113
	 * @param array $fcn Associative list of resource types as keys and anonymous functions for generating the array entries as values
114
	 */
115
	protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
116
	{
117
		$id = $item->getId();
118
		$type = $item->getResourceType();
119
120
		if( isset( $this->map[$type][$id] ) || !$item->isAvailable() ) {
121
			return;
122
		}
123
124
		$attributes = $item->toArray();
125
126
		if( isset( $fields[$type] ) ) {
127
			$attributes = array_intersect_key( $attributes, $fields[$type] );
128
		}
129
130
		$entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes];
131
132
		if( isset( $fcn[$type] ) && $fcn[$type] instanceof \Closure ) {
133
			$entry = $fcn[$type]( $item, $entry );
134
		}
135
136
		$this->map[$type][$id] = $entry; // first content, avoid infinite loops
137
138
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
139
		{
140
			foreach( $item->getChildren() as $childItem )
141
			{
142
				if( $childItem->isAvailable() )
143
				{
144
					$rtype = $childItem->getResourceType();
145
					$entry['relationships'][$rtype]['data'][] = ['id' => $childItem->getId(), 'type' => $rtype];
146
					$this->map( $childItem, $fields, $fcn );
147
				}
148
			}
149
		}
150
151
		if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface )
152
		{
153
			foreach( $item->getListItems() as $listItem )
154
			{
155
				if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() )
156
				{
157
					$ltype = $listItem->getResourceType();
158
					$rtype = $refItem->getResourceType();
159
					$attributes = $listItem->toArray();
160
161
					if( isset( $fields[$ltype] ) ) {
162
						$attributes = array_intersect_key( $attributes, $fields[$ltype] );
163
					}
164
165
					$data = ['id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $attributes];
166
					$entry['relationships'][$rtype]['data'][] = $data;
167
					$this->map( $refItem, $fields, $fcn );
168
				}
169
			}
170
		}
171
172
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
173
		{
174
			foreach( $item->getPropertyItems() as $propItem )
175
			{
176
				if( $propItem->isAvailable() )
177
				{
178
					$propId = $propItem->getId();
179
					$rtype = $propItem->getResourceType();
180
					$entry['relationships'][$rtype]['data'][] = ['id' => $propId, 'type' => $rtype];
181
					$this->map( $propItem, $fields, $fcn );
182
				}
183
			}
184
		}
185
186
		$this->map[$type][$id] = $entry; // full content
187
	}
188
}
189