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

MShopAddDataAbstract   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 54
c 0
b 0
f 0
dl 0
loc 145
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addSuppliers() 0 13 2
A up() 0 2 1
A addProducts() 0 13 2
A addAttributes() 0 18 3
A addRefItems() 0 35 6
A addCategories() 0 13 2
A removeRefItems() 0 15 3
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 records to tables.
15
 */
16
class MShopAddDataAbstract extends Base
17
{
18
	private $attributes;
0 ignored issues
show
introduced by
The private property $attributes is not used, and could be removed.
Loading history...
19
20
21
	public function up()
22
	{
23
	}
24
25
26
	/**
27
	 * Adds the referenced items from the given entry data.
28
	 *
29
	 * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with list items
30
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
31
	 * @return \Aimeos\MShop\Common\Item\ListsRef\Iface $item Updated item
32
	 */
33
	protected function addRefItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entry )
34
	{
35
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\MShopAddDataAbstract. 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

35
		/** @scrutinizer ignore-call */ 
36
  $context = $this->context();
Loading history...
36
37
		foreach( ['media', 'price', 'text'] as $refDomain )
38
		{
39
			if( isset( $entry[$refDomain] ) )
40
			{
41
				$manager = \Aimeos\MShop::create( $context, $refDomain );
42
43
				foreach( $entry[$refDomain] as $data )
44
				{
45
					$listItem = $manager->createListItem()->fromArray( $data );
46
					$refItem = $manager->create()->fromArray( $data );
47
48
					if( isset( $data['property'] ) )
49
					{
50
						foreach( (array) $data['property'] as $property )
51
						{
52
							$propItem = $manager->createPropertyItem()->fromArray( $property );
53
							$refItem->addPropertyItem( $propItem );
54
						}
55
					}
56
57
					$item->addListItem( $refDomain, $listItem, $refItem );
58
				}
59
			}
60
		}
61
62
		$this->addAttributes( $item, $entry['attribute'] ?? [] );
63
		$this->addCategories( $item, $entry['catalog'] ?? [] );
64
		$this->addSuppliers( $item, $entry['supplier'] ?? [] );
65
		$this->addProducts( $item, $entry['product'] ?? [] );
66
67
		return $item;
68
	}
69
70
71
	protected function addAttributes( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entries ) : \Aimeos\MShop\Common\Item\ListsRef\Iface
72
	{
73
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
74
75
		foreach( $entries as $data )
76
		{
77
			$listItem = $manager->createListItem()->fromArray( $data );
78
			$refItem = $manager->create()->fromArray( $data );
79
80
			try {
81
				$refItem = $manager->find( $refItem->getCode(), [], $item->getResourceType(), $refItem->getType() );
82
			} catch( \Exception $e ) { ; }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
83
84
			$refItem = $this->addRefItems( $refItem, $data );
85
			$item->addListItem( 'attribute', $listItem, $refItem );
86
		}
87
88
		return $item;
89
	}
90
91
92
	protected function addCategories( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entries ) : \Aimeos\MShop\Common\Item\ListsRef\Iface
93
	{
94
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
95
96
		foreach( $entries as $data )
97
		{
98
			$listItem = $manager->createListItem()->fromArray( $data );
99
			$refItem = $manager->find( $data['catalog.code'] );
100
101
			$item->addListItem( 'catalog', $listItem, $refItem );
102
		}
103
104
		return $item;
105
	}
106
107
108
	protected function addProducts( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entries ) : \Aimeos\MShop\Common\Item\ListsRef\Iface
109
	{
110
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
111
112
		foreach( $entries as $data )
113
		{
114
			$listItem = $manager->createListItem()->fromArray( $data );
115
			$refItem = $manager->find( $data['product.code'] );
116
117
			$item->addListItem( 'product', $listItem, $refItem );
118
		}
119
120
		return $item;
121
	}
122
123
124
	protected function addSuppliers( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entries ) : \Aimeos\MShop\Common\Item\ListsRef\Iface
125
	{
126
		$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
127
128
		foreach( $entries as $data )
129
		{
130
			$listItem = $manager->createListItem()->fromArray( $data );
131
			$refItem = $manager->find( $data['supplier.code'] );
132
133
			$item->addListItem( 'supplier', $listItem, $refItem );
134
		}
135
136
		return $item;
137
	}
138
139
140
	/**
141
	 * Removes the referenced items from the given items.
142
	 *
143
	 * @param \Aimeos\Map $items List of items
144
	 * @param array $domains List of domain names
145
	 */
146
	public function removeRefItems( \Aimeos\Map $items, array $domains )
147
	{
148
		$context = $this->context();
149
150
		foreach( $domains as $domain )
151
		{
152
			$rmItems = map();
153
154
			foreach( $items as $item ) {
155
				$rmItems->merge( $item->getRefItems( $domain, null, null, false )->filter( function( $item ) {
156
					return strncmp( $item->getLabel(), 'Demo', 4 ) === 0;
157
				} ) );
158
			}
159
160
			\Aimeos\MShop::create( $context, $domain )->delete( $rmItems );
161
		}
162
	}
163
}
164