Passed
Push — master ( dfc165...94b240 )
by Aimeos
04:45
created

MShopAddLocaleData::addLocaleCurrencyData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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