Passed
Push — master ( 2fe25d...e89b12 )
by Aimeos
05:34
created

DemoAddProductData::removeItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
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-2024
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 [
26
			'Product', 'Index', 'Attribute', 'Media', 'Price', 'Stock', 'Text',
27
			'MShopSetLocale', 'MShopAddTypeDataDefault', 'MShopAddCodeDataDefault',
28
			'DemoAddTypeData', 'DemoAddCatalogData', 'DemoAddSupplierData'
29
		];
30
	}
31
32
33
	/**
34
	 * Returns the list of task names which depends on this task.
35
	 *
36
	 * @return string[] List of task names
37
	 */
38
	public function before() : array
39
	{
40
		return ['DemoRebuildIndex'];
41
	}
42
43
44
	/**
45
	 * Insert product data.
46
	 */
47
	public function up()
48
	{
49
		$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

49
		/** @scrutinizer ignore-call */ 
50
  $context = $this->context();
Loading history...
50
		$value = $context->config()->get( 'setup/default/demo', '' );
51
52
		if( $value === '' ) {
53
			return;
54
		}
55
56
57
		$this->info( 'Processing product demo data', 'vv' );
58
59
		$items = $this->removeItems();
60
		$this->removeStockItems( $items );
61
		$this->removeAttributeItems();
62
63
64
		if( $value === '1' ) {
65
			$this->addDemoData();
66
		}
67
	}
68
69
70
	/**
71
	 * Adds the demo data to the database.
72
	 *
73
	 * @throws \RuntimeException If the file isn't found
74
	 */
75
	protected function addDemoData()
76
	{
77
		$ds = DIRECTORY_SEPARATOR;
78
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-product.php';
79
80
		if( ( $data = include( $path ) ) == false ) {
81
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for product domain', $path ) );
82
		}
83
84
		$context = $this->context();
85
		$manager = \Aimeos\MShop::create( $context, 'product' );
86
87
		foreach( $data as $entry )
88
		{
89
			$item = $manager->create()->fromArray( $entry );
90
91
			$this->addRefItems( $item, $entry );
92
			$this->addPropertyItems( $item, $entry );
93
94
			$manager->save( $item );
95
			$manager->rate( $item->getId(), $entry['rating'] ?? 0, $entry['ratings'] ?? 0 );
96
97
			if( isset( $entry['stock'] ) ) {
98
				$this->addStockItems( $item->getId(), $entry['stock'] );
99
			}
100
		}
101
	}
102
103
104
	/**
105
	 * Adds the properties from the given entry data.
106
	 *
107
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item
108
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
109
	 * @return \Aimeos\MShop\Product\Item\Iface $item Updated product item
110
	 */
111
	protected function addPropertyItems( \Aimeos\MShop\Product\Item\Iface $item, array $entry )
112
	{
113
		if( isset( $entry['property'] ) )
114
		{
115
			$manager = \Aimeos\MShop::create( $this->context(), 'product/property' );
116
117
			foreach( (array) $entry['property'] as $values )
118
			{
119
				$propItem = $manager->create()->fromArray( $values );
120
				$item->addPropertyItem( $propItem );
121
			}
122
		}
123
124
		return $item;
125
	}
126
127
128
	/**
129
	 * Adds stock levels to the given product in the database.
130
	 *
131
	 * @param string $productId ID of the product item where the stock levels should be associated to
132
	 * @param array $data Two dimensional associative list of product stock data
133
	 */
134
	protected function addStockItems( $productId, array $data )
135
	{
136
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
137
138
		foreach( $data as $entry )
139
		{
140
			$item = $manager->create()->fromArray( $entry )->setProductId( $productId );
141
			$manager->save( $item, false );
142
		}
143
	}
144
145
146
	/**
147
	 * Deletes the demo product items
148
	 */
149
	protected function removeItems() : \Aimeos\Map
150
	{
151
		$context = $this->context();
152
		$domains = ['media', 'price', 'text'];
153
		$manager = \Aimeos\MShop::create( $context, 'product' );
154
155
		$filter = $manager->filter()->add( 'product.code', '=~', 'demo-' )->slice( 0, 0x7fffffff );
156
		$items = $manager->search( $filter, $domains );
157
158
		$this->removeRefItems( $items, $domains );
159
		$manager->delete( $items );
160
161
		return $items;
162
	}
163
164
165
	/**
166
	 * Deletes the demo attribute items
167
	 */
168
	protected function removeAttributeItems()
169
	{
170
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
171
172
		$search = $manager->filter();
173
		$search->setConditions( $search->compare( '=~', 'attribute.label', 'Demo:' ) );
174
175
		$manager->delete( $manager->search( $search ) );
176
	}
177
178
179
	/**
180
	 * Deletes the demo stock items
181
	 */
182
	protected function removeStockItems( \Aimeos\Map $products )
183
	{
184
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
185
186
		$filter = $manager->filter()->add( ['stock.productid' => $products] )->slice( 0, 0x7fffffff );
187
188
		$manager->delete( $manager->search( $filter ) );
189
	}
190
}
191