Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

LocaleAddTestData::migrate()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 19
nop 0
dl 0
loc 46
rs 7.9337
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds locale test data.
15
 */
16
class LocaleAddTestData extends \Aimeos\MW\Setup\Task\MShopAddLocaleData
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( 'MShopAddLocaleLangCurData' );
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( 'MShopAddLocaleData' );
37
	}
38
39
40
	/**
41
	 * Adds locale 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 test data for MShop locale domain', 0 );
51
		$this->status( '' );
52
53
54
		// Set editor for further tasks
55
		$this->additional->setEditor( 'core:unittest' );
56
57
58
		if( $this->additional->getConfig()->get( 'setup/site' ) === 'unittest' )
59
		{
60
			$ds = DIRECTORY_SEPARATOR;
61
			$filename = __DIR__ . $ds . 'data' . $ds . 'locale.php';
62
63
			if( ( $testdata = include( $filename ) ) == false ) {
64
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No data file "%1$s" found', $filename ) );
65
			}
66
67
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $this->additional );
68
69
			$this->cleanupSites( $localeManager );
70
71
			$siteIds = [];
72
			if( isset( $testdata['locale/site'] ) ) {
73
				$siteIds = $this->addLocaleSiteData( $localeManager, $testdata['locale/site'] );
74
			}
75
76
			if( isset( $testdata['locale/currency'] ) ) {
77
				$this->addLocaleCurrencyData( $localeManager, $testdata['locale/currency'] );
78
			}
79
80
			if( isset( $testdata['locale/language'] ) ) {
81
				$this->addLocaleLanguageData( $localeManager, $testdata['locale/language'] );
82
			}
83
84
			if( isset( $testdata['locale'] ) ) {
85
				$this->addLocaleData( $localeManager, $testdata['locale'], $siteIds );
86
			}
87
		}
88
	}
89
90
91
	/**
92
	 * Gets recursive all sub-sites of a site sorted on their level.
93
	 *
94
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $site Site which can contain sub-sites
95
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] $sites List with sites
96
	 */
97
	private function getSites( \Aimeos\MShop\Locale\Item\Site\Iface $site )
98
	{
99
		$sites = array( $site );
100
101
		foreach( $site->getChildren() as $child ) {
102
			$sites = array_merge( $sites, $this->getSites( $child ) );
103
		}
104
105
		return $sites;
106
	}
107
108
109
	/**
110
	 *
111
	 * Deletes old sites and their subsites.
112
	 *
113
	 * @param \Aimeos\MShop\Locale\Manager\Iface $localeManager
114
	 */
115
	private function cleanupSites( $localeManager )
116
	{
117
		$localeSiteManager = $localeManager->getSubManager( 'site' );
118
119
		$search = $localeSiteManager->createSearch();
120
		$search->setConditions( $search->compare( '==', 'locale.site.code', array( 'unittest' ) ) );
121
122
		$sites = [];
123
124
		foreach( $localeSiteManager->searchItems( $search ) as $site )
125
		{
126
			$site = $localeSiteManager->getTree( $site->getId() );
127
			$sites = array_merge( $sites, $this->getSites( $site ) );
128
		}
129
130
		foreach( array_reverse( $sites ) as $site )
131
		{
132
			$this->additional->setLocale( $localeManager->bootstrap( $site->getCode(), '', '', false ) );
133
			$localeSiteManager->deleteItem( $site->getId() );
134
		}
135
	}
136
}