Passed
Push — master ( 53f86a...d46db0 )
by Aimeos
05:02
created

DemoAddOrderData   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 78
c 2
b 0
f 0
dl 0
loc 189
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A after() 0 3 1
A up() 0 19 3
B addDemoData() 0 57 7
A address() 0 12 1
A products() 0 15 2
A locales() 0 11 2
A services() 0 15 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 ['Order', 'DemoAddCustomerData', 'DemoAddCouponData', 'DemoAddProductData', 'DemoAddServiceData'];
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
		$filter = $manager->filter()->add( 'order.channel', '==', 'demo' );
45
		$manager->delete( $manager->search( $filter ) );
46
47
48
		if( $value === '1' ) {
49
			$this->addDemoData();
50
		}
51
	}
52
53
54
	/**
55
	 * Adds the demo orders.
56
	 */
57
	protected function addDemoData()
58
	{
59
		$num = 0;
60
		$paystatus = [
61
			\Aimeos\MShop\Order\Item\Base::PAY_RECEIVED,
62
			\Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED,
63
			\Aimeos\MShop\Order\Item\Base::PAY_RECEIVED,
64
			\Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED,
65
			\Aimeos\MShop\Order\Item\Base::PAY_PENDING
66
		];
67
		$customer = \Aimeos\MShop::create( $this->context(), 'customer' )->find( '[email protected]' );
68
69
		foreach( $this->locales() as $locale )
70
		{
71
			$lcontext = clone $this->context();
72
			$lcontext->setLocale( $locale );
73
74
			$products = $this->products( $lcontext );
75
			$services = $this->services( $lcontext );
76
77
			$manager = \Aimeos\MShop::create( $lcontext, 'order' );
78
			$manager->begin();
79
80
			for( $i = 0; $i < 10; $i++ )
81
			{
82
				for( $j = 0; $j < floor( $i / 4 ) + 1; $j++ )
83
				{
84
					$lcontext->setDateTime( date( 'Y-m-d H:i:s', time() + ( -9 + $i ) * 86400 - 15000 + ( $num % 10 ) * 1357 ) );
85
86
					$item = $manager->create()
87
						->setChannel( 'demo' )
88
						->setCustomerId( $customer->getId() )
89
						->setStatusPayment( $paystatus[$num % 5] )
90
						->setDatePayment( $lcontext->datetime() )
91
						->setLocale( $locale );
92
93
					foreach( map( $services->get( 'delivery', [] ) )->random( 1 ) as $service ) {
94
						$item->addService( clone $service, 'delivery' );
95
					}
96
97
					foreach( map( $services->get( 'payment', [] ) )->random( 1 ) as $service ) {
98
						$item->addService( clone $service, 'payment' );
99
					}
100
101
					foreach( $products->random( rand( 1, 2 ) ) as $product ) {
102
						$item->addProduct( clone $product );
103
					}
104
105
					$item->addAddress( $this->address( $customer->getPaymentAddress(), $num ), 'payment' );
106
107
					$manager->save( $item );
108
					++$num;
109
				}
110
			}
111
112
			$manager->commit();
113
			$num += 7;
114
		}
115
	}
116
117
118
	/**
119
	 * Returns the order address item based on the passed number
120
	 *
121
	 * @param \Aimeos\MShop\Common\Item\Address\Standard $address Address item
122
	 * @param int $num Sequential number
123
	 * @return \Aimeos\MShop\Order\Item\Address\Iface Order address item
124
	 */
125
	protected function address( \Aimeos\MShop\Common\Item\Address\Standard $address, int $num ) : \Aimeos\MShop\Order\Item\Address\Iface
126
	{
127
		$manager = \Aimeos\MShop::create( $this->context(), 'order/address' );
128
		$firstname = ['John', 'Jose', 'Sandrine', 'Anna', 'Angel', 'Emma', 'Peter', 'Lucy', 'Paul', 'Lilly'];
129
		$lastname = ['Doe', 'Alva', 'Hugo', 'Lodz', 'Hidalgo', 'Wilson', 'Evans', 'Singh', 'Li', 'Müller'];
130
		$country = ['AU', 'BR', 'FR', 'PL', 'ES', 'US', 'ZA', 'IN', 'CN', 'DE'];
131
132
		return $manager->create()->copyFrom( $address )
133
			->setFirstname( $firstname[$num % 10] )
134
			->setLastname( $lastname[$num % 10] )
135
			->setCountryId( $country[$num % 10] )
136
			->setCompany( '' );
137
	}
138
139
140
	/**
141
	 * Returns the available locale objects
142
	 *
143
	 * @return \Aimeos\Map List of locale objects
144
	 */
145
	protected function locales() : \Aimeos\Map
146
	{
147
		$manager = \Aimeos\MShop::create( $this->context(), 'locale' );
148
		$items = $manager->search( $manager->filter()->add( 'locale.site.code', '==', 'default' ) );
149
		$list = [];
150
151
		foreach( $items as $item ) {
152
			$list[] = $manager->bootstrap( $item->getSiteCode(), $item->getLanguageId(), $item->getCurrencyId() );
153
		}
154
155
		return map( $list );
156
	}
157
158
159
	/**
160
	 * Returns the available products
161
	 *
162
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Context\Item\Iface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
163
	 * @return \Aimeos\Map List of product items
164
	 */
165
	protected function products( \Aimeos\MShop\ContextIface $context ) : \Aimeos\Map
166
	{
167
		$manager = \Aimeos\MShop::create( $context, 'product' );
168
		$items = $manager->search( $manager->filter()->add( 'product.code', '=~', 'demo-article' ), ['media', 'price', 'text'] );
169
170
		$manager = \Aimeos\MShop::create( $context, 'order/product' );
171
		$list = [];
172
173
		foreach( $items as $item )
174
		{
175
			$price = $item->getRefItems( 'price', 'default', 'default' )->first();
176
			$list[$item->getId()] = $manager->create()->copyFrom( $item )->setQuantity( rand( 1, 3 ) )->setPrice( $price );
177
		}
178
179
		return map( $list );
180
	}
181
182
183
	/**
184
	 * Returns the available services
185
	 *
186
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
187
	 * @return \Aimeos\Map List of service items
188
	 */
189
	protected function services( \Aimeos\MShop\ContextIface $context ) : \Aimeos\Map
190
	{
191
		$manager = \Aimeos\MShop::create( $context, 'service' );
192
		$items = $manager->search( $manager->filter(), ['media', 'price', 'text'] );
193
194
		$manager = \Aimeos\MShop::create( $context, 'order/service' );
195
		$list = [];
196
197
		foreach( $items as $item )
198
		{
199
			$price = $item->getRefItems( 'price', 'default', 'default' )->first();
200
			$list[$item->getId()] = $manager->create()->copyFrom( $item )->setPrice( $price );
201
		}
202
203
		return map( $list )->groupBy( 'order.service.type' );
204
	}
205
}
206