Passed
Push — master ( bada6f...d61b94 )
by Aimeos
04:42
created

LocaleAddTestData::getSites()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 2
nop 1
dl 0
loc 9
c 0
b 0
f 0
cc 2
rs 10
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-2021
7
 */
8
9
10
namespace Aimeos\Upscheme\Task;
11
12
13
/**
14
 * Adds locale test data.
15
 */
16
class LocaleAddTestData extends 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 after() : array
24
	{
25
		return ['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 before() : array
35
	{
36
		return ['MShopAddLocaleData'];
37
	}
38
39
40
	/**
41
	 * Adds locale test data.
42
	 */
43
	public function up()
44
	{
45
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $this->context() );
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\LocaleAddTestData. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Context\Item\Iface::class, $this->/** @scrutinizer ignore-call */ context() );
Loading history...
46
47
		$this->info( 'Adding test data for MShop locale domain', 'v' );
48
49
		// Set editor for further tasks
50
		$this->context()->setEditor( 'core:lib/mshoplib' );
51
52
53
		if( $this->context()->getConfig()->get( 'setup/site' ) === 'unittest' )
54
		{
55
			$ds = DIRECTORY_SEPARATOR;
56
			$filename = __DIR__ . $ds . 'data' . $ds . 'locale.php';
57
58
			if( ( $testdata = include( $filename ) ) == false ) {
59
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No data file "%1$s" found', $filename ) );
0 ignored issues
show
Bug introduced by
The type Aimeos\MW\Setup\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
			}
61
62
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::create( $this->context() );
63
64
			$this->cleanupSites( $localeManager );
65
66
			$siteIds = [];
67
			if( isset( $testdata['locale/site'] ) ) {
68
				$siteIds = $this->addLocaleSiteData( $localeManager, $testdata['locale/site'] );
69
			}
70
71
			if( isset( $testdata['locale/currency'] ) ) {
72
				$this->addLocaleCurrencyData( $localeManager, $testdata['locale/currency'] );
73
			}
74
75
			if( isset( $testdata['locale/language'] ) ) {
76
				$this->addLocaleLanguageData( $localeManager, $testdata['locale/language'] );
77
			}
78
79
			if( isset( $testdata['locale'] ) ) {
80
				$this->addLocaleData( $localeManager, $testdata['locale'], $siteIds );
81
			}
82
		}
83
	}
84
85
86
	/**
87
	 * Gets recursive all sub-sites of a site sorted on their level.
88
	 *
89
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $site Site which can contain sub-sites
90
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] $sites List with sites
91
	 */
92
	protected function getSites( \Aimeos\MShop\Locale\Item\Site\Iface $site )
93
	{
94
		$sites = [$site->getId() => $site];
95
96
		foreach( $site->getChildren() as $child ) {
97
			$sites += $this->getSites( $child );
98
		}
99
100
		return $sites;
101
	}
102
103
104
	/**
105
	 *
106
	 * Deletes old sites and their subsites.
107
	 *
108
	 * @param \Aimeos\MShop\Locale\Manager\Iface $localeManager
109
	 */
110
	protected function cleanupSites( $localeManager )
111
	{
112
		$localeSiteManager = $localeManager->getSubManager( 'site' );
113
114
		$search = $localeSiteManager->filter();
115
		$search->setConditions( $search->compare( '==', 'locale.site.code', array( 'unittest' ) ) );
116
117
		$sites = [];
118
119
		foreach( $localeSiteManager->search( $search ) as $site )
120
		{
121
			$site = $localeSiteManager->getTree( $site->getId() );
122
			$sites = array_merge( $sites, $this->getSites( $site ) );
123
		}
124
125
		foreach( array_reverse( $sites ) as $site )
126
		{
127
			$this->context()->setLocale( $localeManager->bootstrap( $site->getCode(), '', '', false ) );
128
			$localeSiteManager->delete( $site->getId() );
129
		}
130
	}
131
}
132