Completed
Push — master ( 912c72...dc8d00 )
by Aimeos
08:24
created

DemoAddSupplierData::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), 2017
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()
23
	{
24
		return array( '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 getPostDependencies()
34
	{
35
		return [];
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\Factory::createManager( $context, 'supplier' );
57
58
		$search = $manager->createSearch();
59
		$search->setConditions( $search->compare( '=~', 'supplier.code', 'demo-' ) );
60
		$services = $manager->searchItems( $search );
61
62
		$manager->deleteItems( array_keys( $services ) );
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->saveCustomerItems( $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 saveCustomerItems( array $data )
91
	{
92
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'supplier' );
93
94
		foreach( $data as $entry )
95
		{
96
			$item = $manager->createItem();
97
			$item->setCode( $entry['code'] );
98
			$item->setLabel( $entry['label'] );
99
			$item->setStatus( $entry['status'] );
100
101
			$manager->saveItem( $item );
102
103
			if( isset( $entry['delivery'] ) ) {
104
				$this->saveAddressItems( $entry['delivery'], $item->getId() );
105
			}
106
		}
107
	}
108
109
110
	/**
111
	 * Stores the supplier items
112
	 *
113
	 * @param array $data List of arrays containing the supplier properties
114
	 * @param string $id Unique ID of the supplier item
115
	 */
116
	protected function saveAddressItems( array $data, $id )
117
	{
118
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'supplier/address' );
119
120
		foreach( $data as $entry )
121
		{
122
			$addr = $manager->createItem();
123
			$addr->setParentId( $id );
124
			$addr->setTitle( $entry['title'] );
125
			$addr->setSalutation( $entry['salutation'] );
126
			$addr->setCompany( $entry['company'] );
127
			$addr->setVatID( $entry['vatid'] );
128
			$addr->setFirstname( $entry['firstname'] );
129
			$addr->setLastname( $entry['lastname'] );
130
			$addr->setAddress1( $entry['address1'] );
131
			$addr->setAddress2( $entry['address2'] );
132
			$addr->setAddress3( $entry['address3'] );
133
			$addr->setPostal( $entry['postal'] );
134
			$addr->setCity( $entry['city'] );
135
			$addr->setState( $entry['state'] );
136
			$addr->setLanguageId( $entry['langid'] );
137
			$addr->setCountryId( $entry['countryid'] );
138
			$addr->setTelephone( $entry['telephone'] );
139
			$addr->setEmail( $entry['email'] );
140
			$addr->setTelefax( $entry['telefax'] );
141
			$addr->setWebsite( $entry['website'] );
142
143
			$manager->saveItem( $addr );
144
		}
145
	}
146
}