Passed
Push — master ( af159b...53f86a )
by Aimeos
14:14
created

DemoAddOrderData   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 67
c 1
b 0
f 0
dl 0
loc 156
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 0 48 7
A up() 0 24 3
A address() 0 7 1
A products() 0 14 2
A locales() 0 11 2
A after() 0 3 1
A services() 0 14 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Adds demo records to order tables.
14
 */
15
class DemoAddOrderData extends MShopAddDataAbstract
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 after() : array
23
	{
24
		return ['Customer', 'Coupon', 'Order', 'Product', 'Serivce'];
25
	}
26
27
28
	/**
29
	 * Insert order data.
30
	 */
31
	public function up()
32
	{
33
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddOrderData. 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

33
		/** @scrutinizer ignore-call */ 
34
  $context = $this->context();
Loading history...
34
		$value = $context->config()->get( 'setup/default/demo', '' );
35
36
		if( $value === '' ) {
37
			return;
38
		}
39
40
41
		$this->info( 'Processing order demo data', 'vv' );
42
43
		$manager = \Aimeos\MShop::create( $context, 'order' );
44
		$customer = \Aimeos\MShop::create( $context, 'customer' )->find( '[email protected]' );
45
46
		$filter = $manager->filter();
47
		$filter->add( 'order.customerid', '==', $customer->getId() );
48
		$items = $manager->search( $filter );
49
50
		$manager->delete( $items );
51
52
53
		if( $value === '1' ) {
54
			$this->add( $customer );
55
		}
56
	}
57
58
59
	/**
60
	 * Adds the demo orders.
61
	 *
62
	 * @param \Aimeos\MShop\Customer\Item\Iface $customer Customer item object
63
	 */
64
	protected function add( \Aimeos\MShop\Customer\Item\Iface $customer )
65
	{
66
		$paystatus = [
67
			\Aimeos\MShop\Order\Item\Base::PAY_RECEIVED,
68
			\Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED,
69
			\Aimeos\MShop\Order\Item\Base::PAY_PENDING
70
		];
71
72
		foreach( $this->locales() as $locale )
73
		{
74
			$lcontext = clone $this->context();
75
			$lcontext->setLocale( $locale );
76
77
			$products = $this->products( $lcontext );
78
			$services = $this->services( $lcontext );
79
80
			$manager = \Aimeos\MShop::create( $lcontext, 'order' );
81
			$manager->begin();
82
83
			for( $i = 0; $i < 10; $i++ )
84
			{
85
				for( $j = 0; $j < floor( $i / 4 ) + 1; $j++ )
86
				{
87
					$item = $manager->create();
88
					$item->setCustomerId( $customer->getId() );
89
					$item->setStatusPayment( $paystatus[( $i + $j ) % 3] );
90
					$item->setDatePayment( date( 'Y-m-d H:i:s', time() + ( -10 + $i ) * ( 66400 + $j * 10000 ) ) );
91
					$item->setLocale( $locale );
92
93
					$item->addAddress( $this->address( $customer->getPaymentAddress() ), 'payment' );
94
95
					foreach( $products->random( rand( 1, 2 ) ) as $product ) {
96
						$item->addProduct( $product );
97
					}
98
99
					foreach( map( $services->get( 'delivery', [] ) )->random( 1 ) as $service ) {
100
						$item->addService( $service, 'delivery' );
101
					}
102
103
					foreach( map( $services->get( 'payment', [] ) )->random( 1 ) as $service ) {
104
						$item->addService( $service, 'payment' );
105
					}
106
107
					$manager->save( $item );
108
				}
109
			}
110
111
			$manager->commit();
112
		}
113
	}
114
115
116
	protected function address( \Aimeos\MShop\Common\Item\Address\Standard $address ) : \Aimeos\MShop\Order\Item\Address\Iface
117
	{
118
		$manager = \Aimeos\MShop::create( $this->context(), 'order/address' );
119
		$firstname = ['John', 'Jane', 'Max', 'Anna', 'Tom', 'Emma', 'Peter', 'Lucy', 'Paul', 'Lily'];
120
		$lastname = ['Doe', 'Smith', 'Miller', 'Brown', 'Taylor', 'Wilson', 'Evans', 'Singh', 'Li', 'Müller'];
121
122
		return $manager->create()->copyFrom( $address )->setFirstname( $firstname[rand( 0, 9 )] )->setLastname( $lastname[rand( 0, 9 )] );
123
	}
124
125
126
	protected function locales() : \Aimeos\Map
127
	{
128
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
129
		$items = $manager->search( $manager->filter() );
130
		$list = [];
131
132
		foreach( $items as $item ) {
133
			$list[] = $manager->bootstrap( $item->getSiteCode(), $item->getLanguageId(), $item->getCurrencyId() );
134
		}
135
136
		return map( $list );
137
	}
138
139
140
	protected function products( \Aimeos\MShop\ContextIface $context ) : \Aimeos\Map
141
	{
142
		$domains = ['media', 'price', 'product' => 'default', 'text'];
143
		$manager = \Aimeos\MShop::create( $context, 'product' );
144
		$items = $manager->search( $manager->filter()->add( 'product.type', '=~', 'demo-article' ), $domains );
145
146
		$manager = \Aimeos\MShop::create( $context, 'order/product' );
147
		$list = [];
148
149
		foreach( $items as $item ) {
150
			$list[$item->getId()] = $manager->create()->copyFrom( $item )->setQuantity( rand( 1, 3 ) );
151
		}
152
153
		return map( $list );
154
	}
155
156
157
	protected function services( \Aimeos\MShop\ContextIface $context ) : \Aimeos\Map
158
	{
159
		$manager = \Aimeos\MShop::create( $context, 'service' );
160
		$items = $manager->search( $manager->filter(), ['media', 'price', 'text'] );
161
162
163
		$manager = \Aimeos\MShop::create( $context, 'order/service' );
164
		$list = [];
165
166
		foreach( $items as $item ) {
167
			$list[$item->getId()] = $manager->create()->copyFrom( $item );
168
		}
169
170
		return map( $list )->groupBy( 'service.type' );
171
	}
172
}
173