Passed
Push — master ( fcd792...8b13e6 )
by Aimeos
07:09 queued 42s
created

OrderAddTestData::getProductItems()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
nc 6
nop 1
dl 0
loc 21
c 0
b 0
f 0
cc 4
rs 9.9
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
9
10
namespace Aimeos\Upscheme\Task;
11
12
13
/**
14
 * Adds order test data.
15
 */
16
class OrderAddTestData extends 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 after() : array
24
	{
25
		return ['Order', 'CustomerAddTestData', 'ProductAddTestData', 'PluginAddTestData', 'ServiceAddTestData', 'StockAddTestData'];
26
	}
27
28
29
	/**
30
	 * Adds order test data.
31
	 */
32
	public function up()
33
	{
34
		$this->info( 'Adding order test data', 'v' );
35
36
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\OrderAddTestData. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

36
		/** @scrutinizer ignore-call */ 
37
  $context = $this->context();
Loading history...
37
		$context->setEditor( 'core:lib/mshoplib' );
38
		$context->getLocale()->setCurrencyId( 'EUR' );
39
40
		$orderBaseManager = \Aimeos\MShop\Order\Manager\Factory::create( $context, 'Standard' )->getSubManager( 'base' );
41
		$filter = $orderBaseManager->filter()->add( ['order.base.sitecode' => ['unittest', 'unit']] );
42
		$orderBaseManager->delete( $orderBaseManager->search( $filter ) );
43
44
		$ds = DIRECTORY_SEPARATOR;
45
		$path = __DIR__ . $ds . 'data' . $ds . 'order.php';
46
47
		if( ( $testdata = include( $path ) ) == false ) {
48
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for order domain', $path ) );
49
		}
50
51
		$this->import( $testdata );
52
53
		$context->getLocale()->setCurrencyId( null );
54
	}
55
56
57
	protected function import( array $data )
58
	{
59
		$orderManager = $this->getOrderManager();
60
		$orderBaseManager = $this->getOrderManager( 'base' );
61
		$orderStatusManager = $this->getOrderManager( 'status' );
62
		$orderCouponManager = $this->getOrderManager( 'base/coupon' );
0 ignored issues
show
Unused Code introduced by
The assignment to $orderCouponManager is dead and can be removed.
Loading history...
63
64
		$customerId = $this->getCustomer()->getId();
65
		$attributes = $this->getAttributes();
66
		$products = $this->getProducts();
67
		$services = $this->getServices();
68
69
		foreach( $data as $entry )
70
		{
71
			if( !isset( $entry['base'] ) ) {
72
				throw new \RuntimeException( 'No base data found for ' . print_r( $entry, true ) );
0 ignored issues
show
Bug introduced by
Are you sure print_r($entry, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

72
				throw new \RuntimeException( 'No base data found for ' . /** @scrutinizer ignore-type */ print_r( $entry, true ) );
Loading history...
73
			}
74
75
			$basket = $orderBaseManager->create()->off()
76
				->fromArray( $entry['base'], true )->setCustomerId( $customerId );
77
78
			$basket->setAddresses( $this->createAddresses( $entry['base']['address'] ?? [] ) );
79
			$basket->setProducts( $this->createProducts( $entry['base']['product'] ?? [], $products, $attributes ) );
80
			$basket->setServices( $this->createServices( $entry['base']['service'] ?? [], $services ) );
81
82
			foreach( $entry['base']['coupon'] ?? [] as $map )
83
			{
84
				$list = [];
85
86
				if( ( $pos = $map['ordprodpos'] ?? null ) !== null )
87
				{
88
					$list = [$basket->getProduct( $pos )];
89
					$basket->deleteProduct( $pos );
90
				}
91
92
				$basket->setCoupon( $map['code'], $list );
93
			}
94
95
			$orderBaseManager->store( $basket );
96
97
			$item = $orderManager->create()->fromArray( $entry, true );
98
			$orderManager->save( $item->setBaseId( $basket->getId() ) );
99
100
			foreach( $entry['status'] ?? [] as $map ) {
101
				$orderStatusManager->save( $orderStatusManager->create()->fromArray( $map )->setParentId( $item->getId() ) );
102
			}
103
		}
104
	}
105
106
107
	protected function createAddresses( array $data ) : array
108
	{
109
		$list = [];
110
		$manager = $this->getOrderManager( 'base/address' );
111
112
		foreach( $data as $entry )
113
		{
114
			$item = $manager->create()->fromArray( $entry, true );
115
			$list[$item->getType()][] = $item;
116
		}
117
118
		return $list;
119
	}
120
121
122
	protected function createProducts( array $data, \Aimeos\Map $products, \Aimeos\Map $attributes ) : array
123
	{
124
		$list = [];
125
		$priceManager = $this->getPriceManager();
126
		$manager = $this->getOrderManager( 'base/product' );
127
		$attrManager = $this->getOrderManager( 'base/product/attribute' );
128
129
		foreach( $data as $entry )
130
		{
131
			$attrs = [];
132
			foreach( $entry['attribute'] ?? [] as $attr )
133
			{
134
				$key = $attr['order.base.product.attribute.code'] . '/' . $attr['order.base.product.attribute.value'];
135
				$attrs[] = $attrManager->create()->fromArray( $attr, true )
136
					->setAttributeId( $attributes->get( $key ) );
137
			}
138
139
			$code = $entry['order.base.product.prodcode'] ?? null;
140
			$price = $priceManager->create()->fromArray( $entry, true );
141
142
			$list[] = $manager->create()->fromArray( $entry, true )
143
				->setProducts( $this->createProducts( $entry['product'] ?? [], $products, $attributes ) )
144
				->setAttributeItems( $attrs )->setPrice( $price )
145
				->setProductId( $products->get( $code ) );
146
		}
147
148
		return $list;
149
	}
150
151
152
	protected function createServices( array $data, \Aimeos\Map $services ) : array
153
	{
154
		$list = [];
155
		$priceManager = $this->getPriceManager();
156
		$manager = $this->getOrderManager( 'base/service' );
157
		$attrManager = $this->getOrderManager( 'base/service/attribute' );
158
159
		foreach( $data as $entry )
160
		{
161
			$attrs = [];
162
			foreach( $entry['attribute'] ?? [] as $attr ) {
163
				$attrs[] = $attrManager->create()->fromArray( $attr, true );
164
			}
165
166
			$code = $entry['order.base.service.code'] ?? null;
167
			$type = $entry['order.base.service.type'] ?? 'payment';
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
168
			$price = $priceManager->create()->fromArray( $entry, true );
169
170
			$item = $manager->create()->fromArray( $entry, true )
171
				->setAttributeItems( $attrs )->setPrice( $price )
172
				->setServiceId( $services->get( $code ) ?: '' );
173
174
			$list[$item->getType()][] = $item;
175
		}
176
177
		return $list;
178
	}
179
180
181
	protected function getAttributes() : \Aimeos\Map
182
	{
183
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context(), 'Standard' );
184
185
		return $attributeManager->search( $attributeManager->filter() )
186
			->groupBy( 'attribute.type' )->map( function( $list ) {
187
				return map( $list )->col( 'attribute.id', 'attribute.code' );
188
			} );
189
	}
190
191
192
	protected function getCustomer() : \Aimeos\MShop\Customer\Item\Iface
193
	{
194
		$customerManager = \Aimeos\MShop\Customer\Manager\Factory::create( $this->context(), 'Standard' );
195
		return $customerManager->find( '[email protected]' );
196
	}
197
198
199
	protected function getProducts() : \Aimeos\Map
200
	{
201
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context(), 'Standard' );
202
		return $productManager->search( $productManager->filter() )->col( 'product.id', 'product.code' );
203
	}
204
205
206
	protected function getServices() : \Aimeos\Map
207
	{
208
		$serviceManager = \Aimeos\MShop\Service\Manager\Factory::create( $this->context(), 'Standard' );
209
		return $serviceManager->search( $serviceManager->filter() )->col( 'service.id', 'service.code' );
210
	}
211
212
213
	protected function getOrderManager( $path = null ) : \Aimeos\MShop\Common\Manager\Iface
214
	{
215
		$manager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context(), 'Standard' );
216
217
		if( $path )
218
		{
219
			foreach( explode( '/', $path ) as $part ) {
220
				$manager = $manager->getSubManager( $part );
221
			}
222
		}
223
224
		return $manager;
225
	}
226
227
228
	protected function getPriceManager() : \Aimeos\MShop\Common\Manager\Iface
229
	{
230
		return \Aimeos\MShop\Price\Manager\Factory::create( $this->context(), 'Standard' );
231
	}
232
}
233