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

setup/unittest/AttributeListAddTestData.php (3 issues)

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds attribute test data and all items from other domains.
15
 */
16
class AttributeListAddTestData extends \Aimeos\MW\Setup\Task\Base
17
{
18
	/**
19
	 * Returns the list of task names which this task depends on.
20
	 *
21
	 * @return string[] List of task names
22
	 */
23
	public function getPreDependencies()
24
	{
25
		return ['AttributeAddTestData', 'MediaAddTestData', 'PriceAddTestData', 'TextAddTestData'];
26
	}
27
28
29
	/**
30
	 * Adds attribute-list test data.
31
	 */
32
	public function migrate()
33
	{
34
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $this->additional );
35
36
		$this->msg( 'Adding attribute-list test data', 0 );
37
		$this->additional->setEditor( 'core:lib/mshoplib' );
38
39
		$ds = DIRECTORY_SEPARATOR;
40
		$path = __DIR__ . $ds . 'data' . $ds . 'attribute-list.php';
41
42
		if( ( $testdata = include( $path ) ) == false ) {
43
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for attribute domain', $path ) );
44
		}
45
46
		$refKeys = [];
47
		foreach( $testdata['attribute/lists'] as $dataset ) {
48
			$refKeys[$dataset['domain']][] = $dataset['refid'];
49
		}
50
51
		$refIds = [];
52
		$refIds['media'] = $this->getMediaData( $refKeys['media'] );
53
		$refIds['price'] = $this->getPriceData( $refKeys['price'] );
54
		$refIds['text'] = $this->getTextData( $refKeys['text'] );
55
56
		$this->addAttributeListData( $testdata, $refIds );
57
58
		$this->status( 'done' );
59
	}
60
61
62
	/**
63
	 * Gets required media item ids.
64
	 *
65
	 * @param array $keys List with referenced Ids
66
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
67
	 */
68
	private function getMediaData( array $keys )
69
	{
70
		$mediaManager = \Aimeos\MShop\Media\Manager\Factory::create( $this->additional, 'Standard' );
71
72
		$urls = [];
73
		foreach( $keys as $dataset )
74
		{
75
			if( ( $pos = strpos( $dataset, '/' ) ) === false || ( $str = substr( $dataset, $pos + 1 ) ) === false ) {
76
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref media are set wrong "%1$s"', $dataset ) );
77
			}
78
79
			$urls[] = $str;
80
		}
81
82
		$search = $mediaManager->createSearch();
83
		$search->setConditions( $search->compare( '==', 'media.url', $urls ) );
84
85
		$refIds = [];
86
		foreach( $mediaManager->searchItems( $search ) as $item ) {
87
			$refIds['media/' . $item->getUrl()] = $item->getId();
88
		}
89
90
		return $refIds;
91
	}
92
93
94
	/**
95
	 * Gets required text item ids.
96
	 *
97
	 * @param array $keys List with referenced Ids
98
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
99
	 */
100
	private function getTextData( array $keys )
101
	{
102
		$textManager = \Aimeos\MShop\Text\Manager\Factory::create( $this->additional, 'Standard' );
103
104
		$labels = [];
105
		foreach( $keys as $dataset )
106
		{
107
			if( ( $pos = strpos( $dataset, '/' ) ) === false || ( $str = substr( $dataset, $pos + 1 ) ) === false ) {
108
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref text are set wrong "%1$s"', $dataset ) );
109
			}
110
111
			$labels[] = $str;
112
		}
113
114
		$search = $textManager->createSearch();
115
		$search->setConditions( $search->compare( '==', 'text.label', $labels ) );
116
117
		$refIds = [];
118
		foreach( $textManager->searchItems( $search ) as $item ) {
119
			$refIds['text/' . $item->getLabel()] = $item->getId();
120
		}
121
122
		return $refIds;
123
	}
124
125
126
	/**
127
	 * Gets required price item ids.
128
	 *
129
	 * @param array $keys List with referenced Ids
130
	 * @return array $refIds List with referenced Ids
131
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
132
	 */
133
	private function getPriceData( array $keys )
134
	{
135
		$value = $ship = $domain = $code = [];
136
		foreach( $keys as $dataset )
137
		{
138
			$exp = explode( '/', $dataset );
139
140
			if( count( $exp ) != 5 ) {
141
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for ref price are set wrong "%1$s"', $dataset ) );
142
			}
143
144
			$domain[] = $exp[1];
145
			$code[] = $exp[2];
146
			$value[] = $exp[3];
147
			$ship[] = $exp[4];
148
		}
149
150
		return $this->getPriceIds( $value, $ship );
151
	}
152
153
154
	/**
155
	 * Gets the attribute test data and adds attribute-list test data.
156
	 *
157
	 * @param array $testdata Associative list of key/list pairs
158
	 * @param array $refIds Associative list of domains and the keys/IDs of the inserted items
159
	 * @throws \Aimeos\MW\Setup\Exception If a required ID is not available
160
	 */
161
	private function addAttributeListData( array $testdata, array $refIds )
162
	{
163
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->additional, 'Standard' );
164
		$attributeListManager = $attributeManager->getSubManager( 'lists', 'Standard' );
165
166
		$codes = $typeCodes = [];
167
		foreach( $testdata['attribute/lists'] as $dataset )
168
		{
169
			$exp = explode( '/', $dataset['parentid'] );
170
171
			if( count( $exp ) != 3 ) {
172
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'Some keys for parentid are set wrong "%1$s"', $dataset['parentid'] ) );
173
			}
174
175
			$typeCodes[] = $exp[1];
176
			$codes[] = $exp[2];
177
		}
178
179
180
		$attributeManager->begin();
181
182
		$parentIds = $this->getAttributeIds( $codes, $typeCodes );
183
		$this->addAttributeListTypeItems( $testdata['attribute/lists/type'] );
184
185
		$listItem = $attributeListManager->createItem();
186
		foreach( $testdata['attribute/lists'] as $dataset )
187
		{
188
			if( !isset( $parentIds[$dataset['parentid']] ) ) {
189
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No attribute ID found for "%1$s"', $dataset['parentid'] ) );
190
			}
191
192
			if( !isset( $refIds[$dataset['domain']][$dataset['refid']] ) ) {
193
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No "%1$s" ref ID found for "%2$s"', $dataset['refid'], $dataset['domain'] ) );
194
			}
195
196
			$listItem->setId( null );
197
			$listItem->setParentId( $parentIds[$dataset['parentid']] );
198
			$listItem->setRefId( $refIds[$dataset['domain']][$dataset['refid']] );
199
			$listItem->setType( $dataset['type'] );
200
			$listItem->setDomain( $dataset['domain'] );
201
			$listItem->setDateStart( $dataset['start'] );
0 ignored issues
show
The method setDateStart() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

201
			$listItem->/** @scrutinizer ignore-call */ 
202
              setDateStart( $dataset['start'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
			$listItem->setDateEnd( $dataset['end'] );
0 ignored issues
show
The method setDateEnd() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

202
			$listItem->/** @scrutinizer ignore-call */ 
203
              setDateEnd( $dataset['end'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
			$listItem->setConfig( $dataset['config'] );
0 ignored issues
show
The method setConfig() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

203
			$listItem->/** @scrutinizer ignore-call */ 
204
              setConfig( $dataset['config'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
			$listItem->setPosition( $dataset['pos'] );
205
			$listItem->setStatus( $dataset['status'] );
206
207
			$attributeListManager->saveItem( $listItem, false );
208
		}
209
210
		$attributeManager->commit();
211
	}
212
213
214
	/**
215
	 * Returns the attribute IDs for the given data
216
	 *
217
	 * @param array $codes Attribute codes
218
	 * @param array $typeCodes List of attribute type codes
219
	 * @return array Associative list of identifiers as keys and attribute IDs as values
220
	 */
221
	protected function getAttributeIds( array $codes, array $typeCodes )
222
	{
223
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->additional, 'Standard' );
224
225
		$search = $manager->createSearch();
226
		$expr = array(
227
			$search->compare( '==', 'attribute.code', $codes ),
228
			$search->compare( '==', 'attribute.type', $typeCodes ),
229
		);
230
		$search->setConditions( $search->combine( '&&', $expr ) );
231
232
		$parentIds = [];
233
		foreach( $manager->searchItems( $search ) as $item ) {
234
			$parentIds[$item->getDomain() . '/' . $item->getType() . '/' . $item->getCode()] = $item->getId();
235
		}
236
237
		return $parentIds;
238
	}
239
240
241
	/**
242
	 * Adds the attribute list type items
243
	 *
244
	 * @param array $data Associative list of identifiers as keys and data sets as values
245
	 */
246
	protected function addAttributeListTypeItems( array $data )
247
	{
248
		$manager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->additional, 'Standard' );
249
		$listManager = $manager->getSubManager( 'lists', 'Standard' );
250
		$listTypeManager = $listManager->getSubManager( 'type', 'Standard' );
251
252
		$listItemType = $listTypeManager->createItem();
253
254
		foreach( $data as $key => $dataset )
255
		{
256
			$listItemType->setId( null );
257
			$listItemType->setCode( $dataset['code'] );
258
			$listItemType->setDomain( $dataset['domain'] );
259
			$listItemType->setLabel( $dataset['label'] );
260
			$listItemType->setStatus( $dataset['status'] );
261
262
			$listTypeManager->saveItem( $listItemType );
263
		}
264
	}
265
266
267
	/**
268
	 * Returns the price IDs for the given data
269
	 *
270
	 * @param array $value Price values
271
	 * @param array $ship Price shipping costs
272
	 * @param array Associative list of identifiers as keys and price IDs as values
273
	 */
274
	protected function getPriceIds( array $value, array $ship )
275
	{
276
		$manager = \Aimeos\MShop\Price\Manager\Factory::create( $this->additional, 'Standard' );
277
278
		$search = $manager->createSearch();
279
		$expr = array(
280
			$search->compare( '==', 'price.value', $value ),
281
			$search->compare( '==', 'price.costs', $ship ),
282
		);
283
		$search->setConditions( $search->combine( '&&', $expr ) );
284
285
		$parentIds = [];
286
		foreach( $manager->searchItems( $search ) as $item ) {
287
			$parentIds['price/' . $item->getDomain() . '/' . $item->getType() . '/' . $item->getValue() . '/' . $item->getCosts()] = $item->getId();
288
		}
289
290
		return $parentIds;
291
	}
292
}
293