Completed
Push — master ( e4ad0a...d808c7 )
by Aimeos
10:17
created

CustomerAddPropertyTestData::getPreDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds customer property test data.
14
 */
15
class CustomerAddPropertyTestData extends \Aimeos\MW\Setup\Task\Base
16
{
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 getPreDependencies()
24
	{
25
		return array( 'MShopSetLocale', 'CustomerAddTestData' );
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 getPostDependencies()
35
	{
36
		return array( 'CatalogRebuildTestIndex' );
37
	}
38
39
40
	/**
41
	 * Adds customer test data.
42
	 */
43
	public function migrate()
44
	{
45
		$iface = '\\Aimeos\\MShop\\Context\\Item\\Iface';
46
		if( !( $this->additional instanceof $iface ) ) {
47
			throw new \Aimeos\MW\Setup\Exception( sprintf( 'Additionally provided object is not of type "%1$s"', $iface ) );
48
		}
49
50
		$this->msg( 'Adding customer property test data', 0 );
51
		$this->additional->setEditor( 'core:unittest' );
52
53
		$ds = DIRECTORY_SEPARATOR;
54
		$path = __DIR__ . $ds . 'data' . $ds . 'customer-property.php';
55
56
		if( ( $testdata = include( $path ) ) == false ) {
57
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for customer domain', $path ) );
58
		}
59
60
		$this->addCustomerPropertyData( $testdata );
61
62
		$this->status( 'done' );
63
	}
64
65
	/**
66
	 * Adds the customer property test data.
67
	 *
68
	 * @param array $testdata Associative list of key/list pairs
69
	 * @throws \Aimeos\MW\Setup\Exception If no type ID is found
70
	 */
71
	private function addCustomerPropertyData( array $testdata )
72
	{
73
		$customerManager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $this->additional, 'Standard' );
74
		$customerPropertyManager = $customerManager->getSubManager( 'property', 'Standard' );
75
		$customerPropertyTypeManager = $customerPropertyManager->getSubManager( 'type', 'Standard' );
76
77
		$typeIds = [];
78
		$type = $customerPropertyTypeManager->createItem();
79
		$custIds = $this->getCustomerIds( $customerManager );
80
81
		$this->conn->begin();
82
83
		foreach( $testdata['customer/property/type'] as $key => $dataset )
84
		{
85
			$type->setId( null );
86
			$type->setCode( $dataset['code'] );
87
			$type->setDomain( $dataset['domain'] );
88
			$type->setLabel( $dataset['label'] );
89
			$type->setStatus( $dataset['status'] );
90
91
			$customerPropertyTypeManager->saveItem( $type );
92
			$typeIds[ $key ] = $type->getId();
93
		}
94
95
		$custProperty = $customerPropertyManager->createItem();
96
		foreach( $testdata['customer/property'] as $key => $dataset )
97
		{
98
			if( !isset( $typeIds[ $dataset['typeid'] ] ) ) {
99
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No customer property type ID found for "%1$s"', $dataset['typeid'] ) );
100
			}
101
102
			$custProperty->setId( null );
103
			$custProperty->setParentId( $custIds[ $dataset['parentid'] ] );
104
			$custProperty->setTypeId( $typeIds[ $dataset['typeid'] ] );
105
			$custProperty->setLanguageId( $dataset['langid'] );
106
			$custProperty->setValue( $dataset['value'] );
107
108
			$customerPropertyManager->saveItem( $custProperty, false );
109
		}
110
111
		$this->conn->commit();
112
	}
113
114
115
	/**
116
	 * Retrieves the customer IDs for the used codes
117
	 *
118
	 * @param \Aimeos\MShop\Common\Manager\Iface $customerManager Customer manager object
119
	 * @return array Associative list of customer codes as key (e.g. customer/CNC) and IDs as value
120
	 */
121
	protected function getCustomerIds( \Aimeos\MShop\Common\Manager\Iface $customerManager )
122
	{
123
		$entry = [];
124
		$search = $customerManager->createSearch();
125
126
		foreach( $customerManager->searchItems( $search ) as $id => $item ) {
127
			$entry[ 'customer/' . $item->getCode() ] = $id;
128
		}
129
130
		return $entry;
131
132
	}
133
}