Passed
Push — master ( 4c729e...dfc165 )
by Aimeos
04:56
created

MShopAddLocaleData::up()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Adds locale records to tables.
14
 */
15
class MShopAddLocaleData extends Base
16
{
17
	/**
18
	 * Adds locale site data.
19
	 *
20
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
21
	 * @param array $data Associative list of locale site data
22
	 * @param string $manager Manager implementation name
23
	 * @param string|null $parentId Parent id of the locale item
24
	 * @return array Associative list of keys from the data and generated site ID
25
	 */
26
	protected function addLocaleSiteData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data, $manager = 'Standard', $parentId = null )
27
	{
28
		$this->info( 'Adding data for MShop locale sites', 'vv', 1 );
29
30
		$siteIds = [];
31
		$manager = $localeManager->getSubManager( 'site', $manager );
32
33
		foreach( $data as $key => $dataset )
34
		{
35
			$manager->begin();
36
37
			try
38
			{
39
				$item = $manager->insert( $manager->create()->fromArray( $dataset ), $parentId );
40
				$manager->commit();
41
			}
42
			catch( \Aimeos\Base\DB\Exception $e )
43
			{
44
				$manager->rollback();
45
				$item = $manager->find( $key );
46
			}
47
48
			$siteIds[$key] = ['id' => $item->getId(), 'site' => $item->getSiteId()];
49
		}
50
51
		return $siteIds;
52
	}
53
54
55
	/**
56
	 * Adds locale currency data.
57
	 *
58
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
59
	 * @param array $data Associative list of locale currency data
60
	 */
61
	protected function addLocaleCurrencyData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data )
62
	{
63
		$this->info( 'Adding data for MShop locale currencies', 'vv', 1 );
64
65
		$manager = $localeManager->getSubManager( 'currency', 'Standard' );
66
		$items = $manager->search( $manager->filter()->slice( 0, 0x7fffffff ) );
67
68
		foreach( $data as $key => $dataset ) {
69
			$items->has( $key ) ?: $manager->save( $manager->create()->fromArray( $dataset, true ) );
70
		}
71
	}
72
73
74
	/**
75
	 * Adds locale language data.
76
	 *
77
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
78
	 * @param array $data Associative list of locale language data
79
	 */
80
	protected function addLocaleLanguageData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data )
81
	{
82
		$this->info( 'Adding data for MShop locale languages', 'vv', 1 );
83
84
		$manager = $localeManager->getSubManager( 'language', 'Standard' );
85
		$items = $manager->search( $manager->filter()->slice( 0, 0x7fffffff ) );
86
87
		foreach( $data as $key => $dataset ) {
88
			$items->has( $key ) ?: $manager->save( $manager->create()->fromArray( $dataset, true ) );
89
		}
90
	}
91
92
93
	/**
94
	 * Adds locale data.
95
	 *
96
	 * @param \Aimeos\MShop\Common\Manager\Iface $localeManager Locale manager
97
	 * @param array $data Associative list of locale data
98
	 */
99
	protected function addLocaleData( \Aimeos\MShop\Common\Manager\Iface $localeManager, array $data, array $siteIds )
100
	{
101
		$this->info( 'Adding data for MShop locales', 'vv', 1 );
102
103
		foreach( $data as $dataset )
104
		{
105
			if( !isset( $siteIds[$dataset['site']] ) ) {
106
				throw new \RuntimeException( sprintf( 'No ID for site for key "%1$s" found', $dataset['site'] ) );
107
			}
108
109
			$this->context()->setLocale( $localeManager->create()->setSiteId( $siteIds[$dataset['site']]['site'] ) );
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\MShopAddLocaleData. 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

109
			$this->/** @scrutinizer ignore-call */ 
110
          context()->setLocale( $localeManager->create()->setSiteId( $siteIds[$dataset['site']]['site'] ) );
Loading history...
110
			$item = $localeManager->create()->fromArray( $dataset, true );
111
112
			try {
113
				$localeManager->save( $item );
114
			} catch( \Aimeos\Base\DB\Exception $e ) { ; } // if locale combination was already available
115
		}
116
	}
117
}
118