Completed
Push — master ( a8b9af...50d698 )
by Aimeos
11:33
created

MShopAddAttributeData::getPostDependencies()   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 default attribute
14
 */
15
class MShopAddAttributeData extends MShopAddDataAbstract
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( 'MShopAddTypeData' );
26
	}
27
28
29
	/**
30
	 * Returns the list of task names which depends on this task.
31
	 *
32
	 * @return array List of task names
33
	 */
34
	public function getPostDependencies()
35
	{
36
		return [];
37
	}
38
39
40
	/**
41
	 * Adds the default codes
42
	 */
43
	protected function process()
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
		$site = $this->additional->getLocale()->getSite()->getCode();
51
		$this->msg( sprintf( 'Adding default attribute data for site "%1$s"', $site ), 0 );
52
53
		$ds = DIRECTORY_SEPARATOR;
54
		$path = __DIR__ . $ds . 'default' . $ds . 'data' . $ds . 'attribute.php';
55
56
		if( ( $data = include( $path ) ) == false ) {
57
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for default codes', $path ) );
58
		}
59
60
		$manager = \Aimeos\MShop\Factory::createManager( $this->additional, 'attribute' );
61
		$item = $manager->createItem();
62
		$num = $total = 0;
63
64
		foreach( $data as $dataset )
65
		{
66
			try
67
			{
68
				$total++;
69
				$item->setId( null )->fromArray( $dataset );
70
				$item->setTypeId( $this->getTypeId( 'attribute/type', $dataset['attribute.domain'], $dataset['attribute.type'] ) );
71
72
				$manager->saveItem( $item );
73
				$num++;
74
			}
75
			catch( \Exception $e ) { ; } // if attribute was already available
76
		}
77
78
		$this->status( $num > 0 ? $num . '/' . $total : 'OK' );
79
	}
80
}