Completed
Push — master ( aabfab...16fce1 )
by Aimeos
24:03
created

SupplierListAddTestData::getMediaData()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds supplier list test data.
14
 */
15
class SupplierListAddTestData 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 array( 'MShopSetLocale', 'TextAddTestData', 'ProductAddTestData', 'SupplierAddTestData' );
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return string[] List of task names
32
	 */
33
	public function getPostDependencies()
34
	{
35
		return array( 'CatalogRebuildTestIndex' );
36
	}
37
38
39
	/**
40
	 * Adds supplier test data.
41
	 */
42
	public function migrate()
43
	{
44
		$iface = '\\Aimeos\\MShop\\Context\\Item\\Iface';
45
		if( !( $this->additional instanceof $iface ) ) {
46
			throw new \Aimeos\MW\Setup\Exception( sprintf( 'Additionally provided object is not of type "%1$s"', $iface ) );
47
		}
48
49
		$this->msg( 'Adding supplier-list test data', 0 );
50
		$this->additional->setEditor( 'core:unittest' );
51
52
		$ds = DIRECTORY_SEPARATOR;
53
		$path = __DIR__ . $ds . 'data' . $ds . 'supplier-list.php';
54
55
		if( ( $testdata = include( $path ) ) == false ){
56
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for supplier list domain', $path ) );
57
		}
58
59
		$refKeys = [];
60
		foreach( $testdata['supplier/lists'] as $dataset ) {
61
			$refKeys[ $dataset['domain'] ][] = $dataset['refid'];
62
		}
63
64
		$refIds = [];
65
		$refIds['text'] = $this->getTextData( $refKeys['text'] );
66
		$refIds['media'] = $this->getMediaData( $refKeys['media'] );
67
68
		if( isset( $refKeys['product'] ) ) {
69
			$refIds['product'] = $this->getProductData( $refKeys['product'] );
70
		}
71
72
		$this->addSupplierListData( $testdata, $refIds );
73
74
		$this->status( 'done' );
75
	}
76
77
78
	/**
79
	 * Returns required media item ids.
80
	 *
81
	 * @param array $keys List of keys for search
82
	 * @return array $refIds List with referenced Ids
83
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
84
	 */
85
	protected function getMediaData( array $keys )
86
	{
87
		$mediaManager = \Aimeos\MShop\Media\Manager\Factory::createManager( $this->additional, 'Standard' );
88
89
		$urls = [];
90
		foreach( $keys as $dataset )
91
		{
92
			if( ( $pos = strpos( $dataset, '/' ) ) === false || ( $str = substr( $dataset, $pos + 1 ) ) === false ) {
93
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref media are set wrong "%1$s"', $dataset ) );
94
			}
95
96
			$urls[] = $str;
97
		}
98
99
		$search = $mediaManager->createSearch();
100
		$search->setConditions( $search->compare( '==', 'media.url', $urls ) );
101
102
		$refIds = [];
103
		foreach( $mediaManager->searchItems( $search ) as $item ) {
104
			$refIds['media/' . $item->getUrl()] = $item->getId();
105
		}
106
107
		return $refIds;
108
	}
109
110
111
	/**
112
	 * Gets required product item ids.
113
	 *
114
	 * @param array $keys List of keys for search
115
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
116
	 */
117
	protected function getProductData( array $keys )
118
	{
119
		$manager = \Aimeos\MShop\Product\Manager\Factory::createManager( $this->additional, 'Standard' );
120
121
		$codes = [];
122
		foreach( $keys as $dataset )
123
		{
124
			if( ( $pos = strpos( $dataset, '/' ) ) === false || ( $str = substr( $dataset, $pos+1 ) ) === false ) {
125
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref products are set wrong "%1$s"', $dataset ) );
126
			}
127
128
			$codes[] = $str;
129
		}
130
131
		$search = $manager->createSearch();
132
		$search->setConditions( $search->compare( '==', 'product.code', $codes ) );
133
134
		$refIds = [];
135
		foreach( $manager->searchItems( $search ) as $item ) {
136
			$refIds[ 'product/' . $item->getCode() ] = $item->getId();
137
		}
138
139
		return $refIds;
140
	}
141
142
143
	/**
144
	 * Gets required text item ids.
145
	 *
146
	 * @param array $keys List of keys for search
147
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
148
	 */
149
	protected function getTextData( array $keys )
150
	{
151
		$textManager = \Aimeos\MShop\Text\Manager\Factory::createManager( $this->additional, 'Standard' );
152
153
		$labels = [];
154
		foreach( $keys as $dataset )
155
		{
156
			if( ( $pos = strpos( $dataset, '/' ) ) === false || ( $str = substr( $dataset, $pos+1 ) ) === false ) {
157
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref text are set wrong "%1$s"', $dataset ) );
158
			}
159
160
			$labels[] = $str;
161
		}
162
163
		$search = $textManager->createSearch();
164
		$search->setConditions( $search->compare( '==', 'text.label', $labels ) );
165
166
		$refIds = [];
167
		foreach( $textManager->searchItems( $search ) as $item )	{
168
			$refIds[ 'text/'.$item->getLabel() ] = $item->getId();
169
		}
170
171
		return $refIds;
172
	}
173
174
175
	/**
176
	 * Adds the supplier-list test data.
177
	 *
178
	 * @param array $testdata Associative list of key/list pairs
179
	 * @param array $refIds Associative list of domains and the keys/IDs of the inserted items
180
	 * @param string $type Manager type string
181
	 * @throws \Aimeos\MW\Setup\Exception If a required ID is not available
182
	 */
183
	protected function addSupplierListData( array $testdata, array $refIds, $type = 'Standard' )
184
	{
185
		$supplierManager = \Aimeos\MShop\Supplier\Manager\Factory::createManager( $this->additional, $type );
186
		$supplierListManager = $supplierManager->getSubManager( 'lists', $type );
187
		$supplierListTypeManager = $supplierListManager->getSubmanager( 'type', $type );
188
189
		$itemCode = [];
190
		foreach( $testdata['supplier/lists'] as $dataset )
191
		{
192
			if( ( $pos = strpos( $dataset['parentid'], '/' ) ) === false || ( $str = substr( $dataset['parentid'], $pos+1 ) ) === false ) {
193
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for parentid are set wrong "%1$s"', $dataset['parentid'] ) );
194
			}
195
196
			$itemCode[] = $str;
197
		}
198
199
		$search = $supplierManager->createSearch();
200
		$search->setConditions( $search->compare( '==', 'supplier.code', $itemCode) );
201
202
		$parentIds = [];
203
		foreach( $supplierManager->searchItems( $search ) as $item )	{
204
			$parentIds[ 'supplier/'.$item->getCode() ] = $item->getId();
205
		}
206
207
		$listItemTypeIds = [];
208
		$listItemType = $supplierListTypeManager->createItem();
209
210
		$this->conn->begin();
211
212
		foreach( $testdata['supplier/lists/type'] as $key => $dataset )
213
		{
214
			$listItemType->setId( null );
215
			$listItemType->setCode( $dataset['code'] );
216
			$listItemType->setDomain( $dataset['domain'] );
217
			$listItemType->setLabel( $dataset['label'] );
218
			$listItemType->setStatus( $dataset['status'] );
219
220
			$supplierListTypeManager->saveItem( $listItemType );
221
			$listItemTypeIds[ $key ] = $listItemType->getId();
222
		}
223
224
		$listItem = $supplierListManager->createItem();
225
		foreach( $testdata['supplier/lists'] as $dataset )
226
		{
227
			if( !isset( $parentIds[ $dataset['parentid'] ] ) ) {
228
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No supplier ID found for "%1$s"', $dataset['parentid'] ) );
229
			}
230
231
			if( !isset( $refIds[ $dataset['domain'] ][ $dataset['refid'] ] ) ) {
232
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No "%2$s" ref ID found for "%1$s"', $dataset['refid'], $dataset['domain'] ) );
233
			}
234
235
			if( !isset( $listItemTypeIds[ $dataset['typeid'] ] ) ) {
236
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No supplier list type ID found for "%1$s"', $dataset['typeid'] ) );
237
			}
238
239
			$listItem->setId( null );
240
			$listItem->setParentId( $parentIds[ $dataset['parentid'] ] );
241
			$listItem->setTypeId( $listItemTypeIds[ $dataset['typeid'] ] );
242
			$listItem->setRefId( $refIds[ $dataset['domain'] ] [ $dataset['refid'] ] );
243
			$listItem->setDomain( $dataset['domain'] );
244
			$listItem->setDateStart( $dataset['start'] );
245
			$listItem->setDateEnd( $dataset['end'] );
246
			$listItem->setConfig( $dataset['config'] );
247
			$listItem->setPosition( $dataset['pos'] );
248
			$listItem->setStatus( $dataset['status'] );
249
250
			$supplierListManager->saveItem( $listItem, false );
251
		}
252
253
		$this->conn->commit();
254
	}
255
}