Passed
Push — master ( cb118d...17427a )
by Aimeos
05:39
created

DemoAddSupplierData::addAddressItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds demo records to supplier tables.
14
 */
15
class DemoAddSupplierData extends \Aimeos\MW\Setup\Task\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 getPreDependencies() : array
23
	{
24
		return ['MShopAddTypeDataDefault', 'MShopAddCodeDataDefault', 'DemoAddProductData'];
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return array List of task names
32
	 */
33
	public function getPostDependencies() : array
34
	{
35
		return ['DemoRebuildIndex'];
36
	}
37
38
39
	/**
40
	 * Insert service data.
41
	 */
42
	public function migrate()
43
	{
44
		$this->msg( 'Processing supplier demo data', 0 );
45
46
		$context = $this->getContext();
47
		$value = $context->getConfig()->get( 'setup/default/demo', '' );
48
49
		if( $value === '' )
50
		{
51
			$this->status( 'OK' );
52
			return;
53
		}
54
55
56
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
57
58
		$search = $manager->filter();
59
		$search->setConditions( $search->compare( '=~', 'supplier.code', 'demo-' ) );
60
		$services = $manager->search( $search );
61
62
		$manager->delete( $services->toArray() );
63
64
65
		if( $value === '1' )
66
		{
67
			$ds = DIRECTORY_SEPARATOR;
68
			$path = __DIR__ . $ds . 'data' . $ds . 'demo-supplier.php';
69
70
			if( ( $data = include( $path ) ) == false ) {
71
				throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for supplier domain', $path ) );
72
			}
73
74
			$this->saveItems( $data );
75
76
			$this->status( 'added' );
77
		}
78
		else
79
		{
80
			$this->status( 'removed' );
81
		}
82
	}
83
84
85
	/**
86
	 * Stores the supplier items
87
	 *
88
	 * @param array $data List of arrays containing the supplier properties
89
	 */
90
	protected function saveItems( array $data )
91
	{
92
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
93
94
		foreach( $data as $entry )
95
		{
96
			$item = $manager->create()->fromArray( $entry, true );
97
98
			$item = $this->addRefItems( $item, $entry );
99
			$item = $this->addProductRefs( $item, $entry );
100
			$item = $this->addAddressItems( $item, $entry );
101
102
			$manager->save( $item );
103
		}
104
	}
105
106
107
	/**
108
	 * Adds the referenced product items from the given entry data.
109
	 *
110
	 * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with list items
111
	 * @param array $entry Associative list of data with product section
112
	 * @return \Aimeos\MShop\Common\Item\ListRef\Iface $item Updated item
113
	 */
114
	protected function addAddressItems( \Aimeos\MShop\Common\Item\ListRef\Iface $item, array $entry )
115
	{
116
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/address' );
117
118
		foreach( $entry['address'] ?? [] as $addr ) {
119
			$item->addAddressItem( $manager->create()->fromArray( $addr, true ) );
0 ignored issues
show
Bug introduced by
The method addAddressItem() does not exist on Aimeos\MShop\Common\Item\ListRef\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MShop\Media\Item\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Catalog\Item\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
			$item->/** @scrutinizer ignore-call */ 
120
          addAddressItem( $manager->create()->fromArray( $addr, true ) );
Loading history...
120
		}
121
122
		return $item;
123
	}
124
125
126
	/**
127
	 * Adds the referenced product items from the given entry data.
128
	 *
129
	 * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with list items
130
	 * @param array $entry Associative list of data with product section
131
	 * @return \Aimeos\MShop\Common\Item\ListRef\Iface $item Updated item
132
	 */
133
	protected function addProductRefs( \Aimeos\MShop\Common\Item\ListRef\Iface $item, array $entry )
134
	{
135
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
136
137
		foreach( $entry['product'] ?? [] as $data )
138
		{
139
			$listItem = $manager->createListItem()->fromArray( $data );
140
			$listItem->setRefId( $manager->find( $data['product.code'] )->getId() );
141
142
			$item->addListItem( 'product', $listItem );
143
		}
144
145
		return $item;
146
	}
147
148
149
	/**
150
	 * Adds the referenced items from the given entry data.
151
	 *
152
	 * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with list items
153
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
154
	 * @return \Aimeos\MShop\Common\Item\ListRef\Iface $item Updated item
155
	 */
156
	protected function addRefItems( \Aimeos\MShop\Common\Item\ListRef\Iface $item, array $entry )
157
	{
158
		$context = $this->getContext();
159
		$domain = $item->getResourceType();
160
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
161
162
		foreach( ['media', 'price', 'text'] as $refDomain )
163
		{
164
			if( isset( $entry[$refDomain] ) )
165
			{
166
				$manager = \Aimeos\MShop::create( $context, $refDomain );
167
168
				foreach( $entry[$refDomain] as $data )
169
				{
170
					$listItem = $listManager->create()->fromArray( $data );
171
					$refItem = $manager->create()->fromArray( $data );
172
173
					if( isset( $data['property'] ) )
174
					{
175
						foreach( (array) $data['property'] as $property )
176
						{
177
							$propItem = $manager->createPropertyItem()->fromArray( $property );
178
							$refItem->addPropertyItem( $propItem );
179
						}
180
					}
181
182
					$item->addListItem( $refDomain, $listItem, $refItem );
183
				}
184
			}
185
		}
186
187
		return $item;
188
	}
189
}
190