Passed
Push — master ( bada6f...d61b94 )
by Aimeos
04:42
created

DemoAddProductData   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 85
dl 0
loc 243
c 0
b 0
f 0
rs 10
wmc 29

9 Methods

Rating   Name   Duplication   Size   Complexity  
A after() 0 3 1
A addDemoData() 0 23 4
A before() 0 3 1
A removeAttributeItems() 0 8 1
A addPropertyItems() 0 14 3
A addStockItems() 0 8 2
A up() 0 37 5
B addRefItems() 0 66 11
A removeStockItems() 0 7 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
9
10
namespace Aimeos\Upscheme\Task;
11
12
13
/**
14
 * Adds demo records to product tables.
15
 */
16
class DemoAddProductData extends MShopAddDataAbstract
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 ['MShopAddTypeDataDefault', 'MShopAddCodeDataDefault', 'DemoAddTypeData'];
26
	}
27
28
29
	/**
30
	 * Returns the list of task names which depends on this task.
31
	 *
32
	 * @return string[] List of task names
33
	 */
34
	public function before() : array
35
	{
36
		return ['DemoRebuildIndex'];
37
	}
38
39
40
	/**
41
	 * Insert product data.
42
	 */
43
	public function up()
44
	{
45
		$this->info( 'Processing product demo data', 'v' );
46
47
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddProductData. 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

47
		/** @scrutinizer ignore-call */ 
48
  $context = $this->context();
Loading history...
48
		$value = $context->getConfig()->get( 'setup/default/demo', '' );
49
50
		if( $value === '' ) {
51
			return;
52
		}
53
54
55
		$domains = ['media', 'price', 'text'];
56
		$manager = \Aimeos\MShop::create( $context, 'product' );
57
58
		$search = $manager->filter();
59
		$search->setConditions( $search->compare( '=~', 'product.code', 'demo-' ) );
60
		$products = $manager->search( $search, $domains );
61
62
		foreach( $domains as $domain )
63
		{
64
			$rmIds = map();
65
66
			foreach( $products as $item ) {
67
				$rmIds = $rmIds->merge( $item->getRefItems( $domain, null, null, false )->keys() );
68
			}
69
70
			\Aimeos\MShop::create( $context, $domain )->delete( $rmIds->toArray() );
71
		}
72
73
		$manager->delete( $products->toArray() );
74
		$this->removeStockItems( $products->keys() );
75
		$this->removeAttributeItems();
76
77
78
		if( $value === '1' ) {
79
			$this->addDemoData();
80
		}
81
	}
82
83
84
	/**
85
	 * Adds the demo data to the database.
86
	 *
87
	 * @throws \Aimeos\MShop\Exception If the file isn't found
88
	 */
89
	protected function addDemoData()
90
	{
91
		$ds = DIRECTORY_SEPARATOR;
92
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-product.php';
93
94
		if( ( $data = include( $path ) ) == false ) {
95
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for product domain', $path ) );
96
		}
97
98
		$context = $this->getContext();
0 ignored issues
show
Bug introduced by
The method getContext() does not exist on Aimeos\Upscheme\Task\DemoAddProductData. 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

98
		/** @scrutinizer ignore-call */ 
99
  $context = $this->getContext();
Loading history...
99
		$manager = \Aimeos\MShop::create( $context, 'product' );
100
101
		foreach( $data as $entry )
102
		{
103
			$item = $manager->create()->fromArray( $entry );
104
105
			$this->addRefItems( $item, $entry );
106
			$this->addPropertyItems( $item, $entry );
107
108
			$manager->save( $item );
109
110
			if( isset( $entry['stock'] ) ) {
111
				$this->addStockItems( $item->getId(), $entry['stock'] );
112
			}
113
		}
114
	}
115
116
117
	/**
118
	 * Adds the properties from the given entry data.
119
	 *
120
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item
121
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
122
	 * @return \Aimeos\MShop\Product\Item\Iface $item Updated product item
123
	 */
124
	protected function addPropertyItems( \Aimeos\MShop\Product\Item\Iface $item, array $entry )
125
	{
126
		if( isset( $entry['property'] ) )
127
		{
128
			$manager = \Aimeos\MShop::create( $this->getContext(), 'product/property' );
129
130
			foreach( (array) $entry['property'] as $values )
131
			{
132
				$propItem = $manager->create()->fromArray( $values );
133
				$item->addPropertyItem( $propItem );
134
			}
135
		}
136
137
		return $item;
138
	}
139
140
141
	/**
142
	 * Adds the referenced items from the given entry data.
143
	 *
144
	 * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with list items
145
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
146
	 * @return \Aimeos\MShop\Common\Item\ListsRef\Iface $item Updated item
147
	 */
148
	protected function addRefItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entry )
149
	{
150
		$context = $this->getContext();
151
		$domain = $item->getResourceType();
152
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
153
154
		if( isset( $entry['attribute'] ) )
155
		{
156
			$manager = \Aimeos\MShop::create( $context, 'attribute' );
157
158
			foreach( $entry['attribute'] as $data )
159
			{
160
				$listItem = $listManager->create()->fromArray( $data );
161
				$refItem = $manager->create()->fromArray( $data );
162
163
				try
164
				{
165
					$manager = \Aimeos\MShop::create( $context, 'attribute' );
166
					$refItem = $manager->find( $refItem->getCode(), [], $domain, $refItem->getType() );
167
				}
168
				catch( \Aimeos\MShop\Exception $e ) { ; } // attribute doesn't exist yet
169
170
				$refItem = $this->addRefItems( $refItem, $data );
171
				$item->addListItem( 'attribute', $listItem, $refItem );
172
			}
173
		}
174
175
		foreach( ['media', 'price', 'text'] as $refDomain )
176
		{
177
			if( isset( $entry[$refDomain] ) )
178
			{
179
				$manager = \Aimeos\MShop::create( $context, $refDomain );
180
181
				foreach( $entry[$refDomain] as $data )
182
				{
183
					$listItem = $listManager->create()->fromArray( $data );
184
					$refItem = $manager->create()->fromArray( $data );
185
186
					if( isset( $data['property'] ) )
187
					{
188
						foreach( (array) $data['property'] as $property )
189
						{
190
							$propItem = $manager->createPropertyItem()->fromArray( $property );
191
							$refItem->addPropertyItem( $propItem );
192
						}
193
					}
194
195
					$item->addListItem( $refDomain, $listItem, $refItem );
196
				}
197
			}
198
		}
199
200
		if( isset( $entry['product'] ) )
201
		{
202
			$manager = \Aimeos\MShop::create( $context, 'product' );
203
204
			foreach( $entry['product'] as $data )
205
			{
206
				$listItem = $listManager->create()->fromArray( $data );
207
				$listItem->setRefId( $manager->find( $data['product.code'] )->getId() );
208
209
				$item->addListItem( 'product', $listItem );
210
			}
211
		}
212
213
		return $item;
214
	}
215
216
217
	/**
218
	 * Adds stock levels to the given product in the database.
219
	 *
220
	 * @param string $productId ID of the product item where the stock levels should be associated to
221
	 * @param array $data Two dimensional associative list of product stock data
222
	 */
223
	protected function addStockItems( $productId, array $data )
224
	{
225
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
226
227
		foreach( $data as $entry )
228
		{
229
			$item = $manager->create()->fromArray( $entry )->setProductId( $productId );
230
			$manager->save( $item, false );
231
		}
232
	}
233
234
235
	/**
236
	 * Deletes the demo attribute items
237
	 */
238
	protected function removeAttributeItems()
239
	{
240
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
241
242
		$search = $manager->filter();
243
		$search->setConditions( $search->compare( '=~', 'attribute.label', 'Demo:' ) );
244
245
		$manager->delete( $manager->search( $search ) );
246
	}
247
248
249
	/**
250
	 * Deletes the demo stock items
251
	 */
252
	protected function removeStockItems( \Aimeos\Map $prodIds )
253
	{
254
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
255
256
		$filter = $manager->filter()->add( ['stock.productid' => $prodIds] )->slice( 0, $prodIds->count() );
257
258
		$manager->delete( $manager->search( $filter ) );
259
	}
260
}
261