Passed
Push — master ( 3311dc...3c99d6 )
by Aimeos
05:26
created

lib/mshoplib/setup/unittest/BaseAddTestData.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Provides basic methods
14
 */
15
class BaseAddTestData extends \Aimeos\MW\Setup\Task\Base
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function getPreDependencies()
23
	{
24
		return ['MShopSetLocale'];
25
	}
26
27
28
	/**
29
	 * Adds product test data
30
	 */
31
	public function migrate()
32
	{
33
	}
34
35
36
	/**
37
	 * Adds the property test data
38
	 *
39
	 * @param \Aimeos\MShop\Common\Manager\Iface $addrManager Address manager object
40
	 * @param \Aimeos\MShop\Common\Item\AddressRef\Iface $item Item object
41
	 * @param array $data Associative list of key/list pairs
42
	 * @return \Aimeos\MShop\Common\Item\AddressRef\Iface Modified item object
43
	 */
44
	protected function addAddressData( \Aimeos\MShop\Common\Manager\Iface $addrManager, \Aimeos\MShop\Common\Item\AddressRef\Iface $item, array $data )
45
	{
46
		if( isset( $data['address'] ) )
47
		{
48
			foreach( $data['address'] as $entry ) {
49
				$item->addAddressItem( $addrManager->createItem( $entry )->fromArray( $entry ) );
50
			}
51
		}
52
53
		return $item;
54
	}
55
56
57
	/**
58
	 * Adds the list test data
59
	 *
60
	 * @param \Aimeos\MShop\Common\Manager\Iface $listManager List manager object
61
	 * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item object
62
	 * @param array $data List of key/list pairs lists
63
	 * @return \Aimeos\MShop\Common\Item\ListRef\Iface Modified item object
64
	 */
65
	protected function addListData( \Aimeos\MShop\Common\Manager\Iface $listManager, \Aimeos\MShop\Common\Item\ListRef\Iface $item, array $data )
66
	{
67
		if( isset( $data['lists'] ) )
68
		{
69
			foreach( $data['lists'] as $domain => $entries )
70
			{
71
				$manager = $this->getManager( $domain );
72
				$refItems = $this->getRefItems( $domain );
73
74
				foreach( $entries as $entry )
75
				{
76
					$listItem = $listManager->createItem()->fromArray( $entry );
77
78
					if( isset( $entry['ref'] ) && isset( $refItems[$entry['ref']] ) ) {
79
						$refItem = $refItems[$entry['ref']];
80
					} else {
81
						$refItem = $manager->createItem()->fromArray( $entry );
82
					}
83
84
					if( $refItem instanceof \Aimeos\MShop\Common\Item\ListRef\Iface ) {
85
						$refItem = $this->addListData( $manager->getSubManager( 'lists' ), $refItem, $entry );
86
					}
87
88
					if( $refItem instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface ) {
89
						$refItem = $this->addPropertyData( $manager->getSubManager( 'property' ), $refItem, $entry );
90
					}
91
92
					$item->addListItem( $domain, $listItem, $refItem );
93
				}
94
			}
95
		}
96
97
		return $item;
98
	}
99
100
101
	/**
102
	 * Adds the property test data
103
	 *
104
	 * @param \Aimeos\MShop\Common\Manager\Iface $propManager Property manager object
105
	 * @param \Aimeos\MShop\Common\Item\PropertyRef\Iface $item Item object
106
	 * @param array $data List of key/list pairs lists
107
	 * @return \Aimeos\MShop\Common\Item\PropertyRef\Iface Modified item object
108
	 */
109
	protected function addPropertyData( \Aimeos\MShop\Common\Manager\Iface $propManager, \Aimeos\MShop\Common\Item\PropertyRef\Iface $item, array $data )
110
	{
111
		if( isset( $data['property'] ) )
112
		{
113
			foreach( $data['property'] as $entry ) {
114
				$item->addPropertyItem( $propManager->createItem()->fromArray( $entry ) );
115
			}
116
		}
117
118
		return $item;
119
	}
120
121
122
	/**
123
	 * Returns the manager for the current setup task
124
	 *
125
	 * @param string $domain Domain name of the manager
126
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
127
	 */
128
	protected function getManager( $domain )
129
	{
130
		return \Aimeos\MShop::create( $this->additional, $domain );
131
	}
132
133
134
	/**
135
	 * Returns the items from the given domains
136
	 *
137
	 * @param array $domain Domain names
138
	 * @return array Associative list of label/item pairs
139
	 */
140
	protected function getRefItems( $domain )
141
	{
142
		$list = [];
143
144
		$manager = $this->getManager( $domain );
0 ignored issues
show
$domain of type array is incompatible with the type string expected by parameter $domain of Aimeos\MW\Setup\Task\BaseAddTestData::getManager(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
		$manager = $this->getManager( /** @scrutinizer ignore-type */ $domain );
Loading history...
145
		$search = $manager->createSearch()->setSlice( 0, 10000 );
146
147
		foreach( $manager->searchItems( $search ) as $item ) {
148
			$list[$item->getLabel()] = $item;
149
		}
150
151
		return $list;
152
	}
153
154
155
	/**
156
	 * Creates the type test data
157
	 *
158
	 * @param array $data List of key/list pairs lists
159
	 * @param array $domains List of domain names
160
	 */
161
	protected function storeTypes( array $testdata, array $domains )
162
	{
163
		foreach( $domains as $domain )
164
		{
165
			if( isset( $testdata[$domain] ) )
166
			{
167
				$subnames = explode( '/', $domain );
168
				$manager = $this->getManager( array_shift( $subnames ) );
169
170
				foreach( $subnames as $subname ) {
171
					$manager = $manager->getSubManager( $subname );
172
				}
173
174
				foreach( $testdata[$domain] as $entry )
175
				{
176
					try {
177
						$manager->saveItem( $manager->createItem()->fromArray( $entry ), false );
178
					} catch( \Exception $e ) {} // Duplicate entry
179
				}
180
			}
181
		}
182
	}
183
}
184