Completed
Push — master ( cccae4...cbf01e )
by Aimeos
04:11
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-2020
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
		$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
	/**
62
	 * Processes a single item to create the included data for the JSON:API response
63
	 *
64
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
65
	 * @param array $fields Associative list of resource types as keys and field names to output as values
66
	 * @param array $fcn Associative list of resource types as keys and anonymous functions for generating the array entries as values
67
	 */
68
	protected function entry( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
69
	{
70
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
71
		{
72
			foreach( $item->getChildren() as $catItem )
73
			{
74
				if( $catItem->isAvailable() ) {
75
					$this->map( $catItem, $fields, $fcn );
76
				}
77
			}
78
		}
79
80
		if( $item instanceof \Aimeos\MShop\Common\Item\AddressRef\Iface )
81
		{
82
			foreach( $item->getAddressItems() as $addrItem ) {
83
				$this->map( $addrItem, $fields, $fcn );
84
			}
85
		}
86
87
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
88
		{
89
			foreach( $item->getListItems() as $listItem )
90
			{
91
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
92
					$this->map( $refItem, $fields, $fcn );
93
				}
94
			}
95
		}
96
97
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
98
		{
99
			foreach( $item->getPropertyItems() as $propItem ) {
100
				$this->map( $propItem, $fields, $fcn );
101
			}
102
		}
103
	}
104
105
106
	/**
107
	 * Populates the map class property with the included data for the JSON:API response
108
	 *
109
	 * @param \Aimeos\MShop\Common\Item\Iface $item Object to generate the included data for
110
	 * @param array $fields Associative list of resource types as keys and field names to output as values
111
	 * @param array $fcn Associative list of resource types as keys and anonymous functions for generating the array entries as values
112
	 */
113
	protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] )
114
	{
115
		$id = $item->getId();
116
		$type = $item->getResourceType();
117
118
		if( isset( $this->map[$type][$id] ) || !$item->isAvailable() ) {
119
			return;
120
		}
121
122
		$attributes = $item->toArray();
123
124
		if( isset( $fields[$type] ) ) {
125
			$attributes = array_intersect_key( $attributes, $fields[$type] );
126
		}
127
128
		$entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes];
129
130
		if( isset( $fcn[$type] ) && $fcn[$type] instanceof \Closure ) {
131
			$entry = $fcn[$type]( $item, $entry );
132
		}
133
134
		$this->map[$type][$id] = $entry; // first content, avoid infinite loops
135
136
		if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface )
137
		{
138
			foreach( $item->getChildren() as $childItem )
139
			{
140
				if( $childItem->isAvailable() )
141
				{
142
					$rtype = $childItem->getResourceType();
143
					$entry['relationships'][$rtype]['data'][] = ['id' => $childItem->getId(), 'type' => $rtype];
144
					$this->map( $childItem, $fields, $fcn );
145
				}
146
			}
147
		}
148
149
		if( $item instanceof \Aimeos\MShop\Common\Item\ListRef\Iface )
150
		{
151
			foreach( $item->getListItems() as $listItem )
152
			{
153
				if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() )
154
				{
155
					$rtype = $refItem->getResourceType();
156
					$data = ['id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $listItem->toArray()];
157
					$entry['relationships'][$rtype]['data'][] = $data;
158
					$this->map( $refItem, $fields, $fcn );
159
				}
160
			}
161
		}
162
163
		if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface )
164
		{
165
			foreach( $item->getPropertyItems() as $propItem )
166
			{
167
				if( $propItem->isAvailable() )
168
				{
169
					$propId = $propItem->getId();
170
					$rtype = $propItem->getResourceType();
171
					$entry['relationships'][$rtype]['data'][] = ['id' => $propId, 'type' => $rtype];
172
					$this->map( $propItem, $fields, $fcn );
173
				}
174
			}
175
		}
176
177
		$this->map[$type][$id] = $entry; // full content
178
	}
179
}
180