Passed
Push — master ( b35ffc...19a63c )
by Aimeos
04:50
created

OrderAddTestData::import()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 35
rs 9.2888
cc 5
nc 7
nop 2
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-2022
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', 'vv' );
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' );
38
		$context->locale()->setCurrencyId( 'EUR' );
39
40
		$manager = $this->getOrderManager();
41
		$filter = $manager->filter()->add( ['order.sitecode' => 'unittest'] );
42
		$manager->delete( $manager->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, $this->getCustomer()->getId() );
0 ignored issues
show
Bug introduced by
It seems like $this->getCustomer()->getId() can also be of type null; however, parameter $customerId of Aimeos\Upscheme\Task\OrderAddTestData::import() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
		$this->import( $testdata, /** @scrutinizer ignore-type */ $this->getCustomer()->getId() );
Loading history...
52
53
		$context->locale()->setCurrencyId( null );
54
	}
55
56
57
	protected function import( array $data, string $customerId )
58
	{
59
		$orderManager = $this->getOrderManager();
60
		$orderStatusManager = $this->getOrderManager( 'order/status' );
61
62
		$attributes = $this->getAttributes();
63
		$products = $this->getProducts();
64
		$services = $this->getServices();
65
66
		foreach( $data as $entry )
67
		{
68
			$basket = $orderManager->create()->off()
69
				->fromArray( $entry, true )->setCustomerId( $customerId );
70
71
			$basket->setAddresses( $this->createAddresses( $entry['address'] ?? [] ) );
72
			$basket->setProducts( $this->createProducts( $entry['product'] ?? [], $products, $attributes ) );
73
			$basket->setServices( $this->createServices( $entry['service'] ?? [], $services ) );
74
75
			foreach( $entry['coupon'] ?? [] as $map )
76
			{
77
				$list = [];
78
79
				if( ( $pos = $map['ordprodpos'] ?? null ) !== null )
80
				{
81
					$list = [$basket->getProduct( $pos )];
82
					$basket->deleteProduct( $pos );
83
				}
84
85
				$basket->setCoupon( $map['code'], $list );
86
			}
87
88
			$orderManager->save( $basket );
89
90
			foreach( $entry['status'] ?? [] as $map ) {
91
				$orderStatusManager->save( $orderStatusManager->create()->fromArray( $map )->setParentId( $basket->getId() ) );
92
			}
93
		}
94
	}
95
96
97
	protected function createAddresses( array $data ) : array
98
	{
99
		$list = [];
100
		$manager = $this->getOrderManager( 'order/address' );
101
102
		foreach( $data as $entry )
103
		{
104
			$item = $manager->create()->fromArray( $entry, true );
105
			$list[$item->getType()][] = $item;
106
		}
107
108
		return $list;
109
	}
110
111
112
	protected function createProducts( array $data, \Aimeos\Map $products, \Aimeos\Map $attributes ) : array
113
	{
114
		$list = [];
115
		$priceManager = $this->getPriceManager();
116
		$manager = $this->getOrderManager( 'order/product' );
117
		$attrManager = $this->getOrderManager( 'order/product/attribute' );
118
119
		foreach( $data as $entry )
120
		{
121
			$attrs = [];
122
			foreach( $entry['attribute'] ?? [] as $attr )
123
			{
124
				$key = $attr['order.product.attribute.code'] . '/' . $attr['order.product.attribute.value'];
125
				$attrs[] = $attrManager->create()->fromArray( $attr, true )
126
					->setAttributeId( $attributes->get( $key ) );
127
			}
128
129
			$code = $entry['order.product.prodcode'] ?? null;
130
			$price = $priceManager->create()->fromArray( $entry, true );
131
132
			$list[] = $manager->create()->fromArray( $entry, true )
133
				->setProducts( $this->createProducts( $entry['product'] ?? [], $products, $attributes ) )
134
				->setAttributeItems( $attrs )->setPrice( $price )
135
				->setProductId( $products->get( $code ) );
136
		}
137
138
		return $list;
139
	}
140
141
142
	protected function createServices( array $data, \Aimeos\Map $services ) : array
143
	{
144
		$list = [];
145
		$priceManager = $this->getPriceManager();
146
		$manager = $this->getOrderManager( 'order/service' );
147
		$txManager = $this->getOrderManager( 'order/service/transaction' );
148
		$attrManager = $this->getOrderManager( 'order/service/attribute' );
149
150
		foreach( $data as $entry )
151
		{
152
			$attrs = [];
153
			foreach( $entry['attribute'] ?? [] as $attr ) {
154
				$attrs[] = $attrManager->create()->fromArray( $attr, true );
155
			}
156
157
			$trans = [];
158
			foreach( $entry['transaction'] ?? [] as $tx ) {
159
				$trans[] = $txManager->create()->fromArray( $tx, true );
160
			}
161
162
			$code = $entry['order.service.code'] ?? null;
163
			$price = $priceManager->create()->fromArray( $entry, true );
164
165
			$item = $manager->create()->fromArray( $entry, true )
166
				->setAttributeItems( $attrs )->setPrice( $price )
167
				->setServiceId( $services->get( $code ) ?: '' )
168
				->setTransactions( $trans );
169
170
			$list[$item->getType()][] = $item;
171
		}
172
173
		return $list;
174
	}
175
176
177
	protected function getAttributes() : \Aimeos\Map
178
	{
179
		$attributeManager = \Aimeos\MShop::create( $this->context(), 'attribute', 'Standard' );
180
181
		return $attributeManager->search( $attributeManager->filter() )
182
			->groupBy( 'attribute.type' )->map( function( $list ) {
183
				return map( $list )->col( 'attribute.id', 'attribute.code' );
184
			} );
185
	}
186
187
188
	protected function getCustomer() : \Aimeos\MShop\Customer\Item\Iface
189
	{
190
		$customerManager = \Aimeos\MShop::create( $this->context(), 'customer', 'Standard' );
191
		return $customerManager->find( '[email protected]' );
192
	}
193
194
195
	protected function getProducts() : \Aimeos\Map
196
	{
197
		$productManager = \Aimeos\MShop::create( $this->context(), 'product', 'Standard' );
198
		return $productManager->search( $productManager->filter() )->col( 'product.id', 'product.code' );
199
	}
200
201
202
	protected function getServices() : \Aimeos\Map
203
	{
204
		$serviceManager = \Aimeos\MShop::create( $this->context(), 'service', 'Standard' );
205
		return $serviceManager->search( $serviceManager->filter() )->col( 'service.id', 'service.code' );
206
	}
207
208
209
	protected function getOrderManager( $path = 'order' ) : \Aimeos\MShop\Common\Manager\Iface
210
	{
211
		return \Aimeos\MShop::create( $this->context(), $path, 'Standard' );
212
	}
213
214
215
	protected function getPriceManager() : \Aimeos\MShop\Common\Manager\Iface
216
	{
217
		return \Aimeos\MShop::create( $this->context(), 'price', 'Standard' );
218
	}
219
}
220