Passed
Push — master ( 02b303...90ef4e )
by Aimeos
04:28
created

DemoAddSupplierData::addProductRefs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
nc 2
nop 2
dl 0
loc 13
c 0
b 0
f 0
cc 2
rs 10
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 supplier tables.
14
 */
15
class DemoAddSupplierData 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 ['Supplier', 'Media', 'Text', 'MShopAddTypeDataDefault', 'MShopAddCodeDataDefault'];
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 before() : array
34
	{
35
		return ['DemoRebuildIndex'];
36
	}
37
38
39
	/**
40
	 * Insert service data.
41
	 */
42
	public function up()
43
	{
44
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddSupplierData. 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

44
		/** @scrutinizer ignore-call */ 
45
  $context = $this->context();
Loading history...
45
		$value = $context->config()->get( 'setup/default/demo', '' );
46
47
		if( $value === '' ) {
48
			return;
49
		}
50
51
52
		$this->info( 'Processing supplier demo data', 'vv' );
53
54
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
55
56
		$search = $manager->filter();
57
		$search->setConditions( $search->compare( '=~', 'supplier.code', 'demo-' ) );
58
		$services = $manager->search( $search );
59
60
		$manager->delete( $services->toArray() );
61
62
63
		if( $value === '1' )
64
		{
65
			$ds = DIRECTORY_SEPARATOR;
66
			$path = __DIR__ . $ds . 'data' . $ds . 'demo-supplier.php';
67
68
			if( ( $data = include( $path ) ) == false ) {
69
				throw new \RuntimeException( sprintf( 'No file "%1$s" found for supplier domain', $path ) );
70
			}
71
72
			$this->saveItems( $data );
73
		}
74
	}
75
76
77
	/**
78
	 * Stores the supplier items
79
	 *
80
	 * @param array $data List of arrays containing the supplier properties
81
	 */
82
	protected function saveItems( array $data )
83
	{
84
		$manager = \Aimeos\MShop::create( $this->context(), 'supplier' );
85
86
		foreach( $data as $idx => $entry )
87
		{
88
			$item = $manager->create()->fromArray( $entry, true );
89
90
			$item = $this->addRefItems( $item, $entry, $idx );
91
			$item = $this->addAddressItems( $item, $entry );
0 ignored issues
show
Bug introduced by
$item of type Aimeos\MShop\Common\Item\ListsRef\Iface is incompatible with the type Aimeos\MShop\Common\Item\AddressRef\Iface expected by parameter $item of Aimeos\Upscheme\Task\Dem...Data::addAddressItems(). ( Ignorable by Annotation )

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

91
			$item = $this->addAddressItems( /** @scrutinizer ignore-type */ $item, $entry );
Loading history...
92
93
			$manager->save( $item );
94
		}
95
	}
96
97
98
	/**
99
	 * Adds the referenced product items from the given entry data.
100
	 *
101
	 * @param \Aimeos\MShop\Common\Item\AddressRef\Iface $item Item with list items
102
	 * @param array $entry Associative list of data with product section
103
	 * @return \Aimeos\MShop\Common\Item\Iface $item Updated item
104
	 */
105
	protected function addAddressItems( \Aimeos\MShop\Common\Item\AddressRef\Iface $item, array $entry )
106
	{
107
		$manager = \Aimeos\MShop::create( $this->context(), 'supplier/address' );
108
109
		foreach( $entry['address'] ?? [] as $addr ) {
110
			$item->addAddressItem( $manager->create()->fromArray( $addr, true ) );
111
		}
112
113
		return $item;
114
	}
115
}
116