Passed
Push — master ( 63bc59...352b21 )
by Aimeos
148:01 queued 75:12
created

CatalogAddTestData   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreDependencies() 0 3 1
A process() 0 18 3
A getManager() 0 7 2
A migrate() 0 10 1
A getData() 0 9 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2020
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds catalog test data.
15
 */
16
class CatalogAddTestData extends \Aimeos\MW\Setup\Task\BaseAddTestData
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() : array
24
	{
25
		return ['MShopSetLocale'];
26
	}
27
28
29
	/**
30
	 * Adds catalog test data
31
	 */
32
	public function migrate()
33
	{
34
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $this->additional );
35
36
		$this->msg( 'Adding catalog test data', 0 );
37
38
		$this->additional->setEditor( 'core:lib/mshoplib' );
39
		$this->process( $this->getData() );
40
41
		$this->status( 'done' );
42
	}
43
44
45
	/**
46
	 * Returns the test data array
47
	 *
48
	 * @return array Multi-dimensional array of test data
49
	 */
50
	protected function getData()
51
	{
52
		$path = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'catalog.php';
53
54
		if( ( $testdata = include( $path ) ) == false ) {
55
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for catalog domain', $path ) );
56
		}
57
58
		return $testdata;
59
	}
60
61
62
	/**
63
	 * Returns the manager for the current setup task
64
	 *
65
	 * @param string $domain Domain name of the manager
66
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
67
	 */
68
	protected function getManager( $domain )
69
	{
70
		if( $domain === 'catalog' ) {
71
			return \Aimeos\MShop\Catalog\Manager\Factory::create( $this->additional, 'Standard' );
72
		}
73
74
		return parent::getManager( $domain );
75
	}
76
77
78
	/**
79
	 * Adds the catalog test data.
80
	 *
81
	 * @param array $testdata Associative list of key/list pairs
82
	 * @param string|null $parentId ID of parent category or null for root category
83
	 * @throws \Aimeos\MW\Setup\Exception If a required ID is not available
84
	 */
85
	protected function process( array $testdata, $parentId = null )
86
	{
87
		$manager = $this->getManager( 'catalog' );
88
		$listManager = $manager->getSubManager( 'lists' );
89
90
		$manager->begin();
91
		$this->storeTypes( $testdata, ['catalog/type', 'catalog/lists/type'] );
92
		$manager->commit();
93
94
		foreach( $testdata['catalog'] as $entry )
95
		{
96
			$item = $manager->create()->fromArray( $entry );
97
			$item = $this->addListData( $listManager, $item, $entry );
98
99
			$id = $manager->insert( $item, $parentId )->getId();
100
101
			if( isset( $entry['catalog'] ) ) {
102
				$this->process( $entry, $id );
103
			}
104
		}
105
	}
106
}
107