Passed
Push — master ( 4b0b7f...07e2ee )
by Aimeos
05:23
created

LocaleCreateSite   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 32
c 1
b 0
f 0
dl 0
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A after() 0 3 1
A before() 0 3 1
A data() 0 10 2
A createSite() 0 16 2
A up() 0 19 3
A createLocale() 0 7 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2024
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Adds locale records to tables.
14
 */
15
class LocaleCreateSite extends Base
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on.
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function after() : array
23
	{
24
		return ['MShopAddLocaleLangCurData'];
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return string[] List of task names
32
	 */
33
	public function before() : array
34
	{
35
		return ['MShopAddLocaleData'];
36
	}
37
38
39
	/**
40
	 * Adds locale data.
41
	 */
42
	public function up()
43
	{
44
		$this->info( 'Create site and locale', 'vv' );
45
46
		$context = $this->context()->setEditor( 'core' ); // Set editor for further tasks
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\LocaleCreateSite. 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

46
		$context = $this->/** @scrutinizer ignore-call */ context()->setEditor( 'core' ); // Set editor for further tasks
Loading history...
47
48
		$config = $context->config();
49
		$site = $config->get( 'setup/site', 'default' );
50
		$lang = $config->get( 'setup/language', 'en' );
51
		$curr = $config->get( 'setup/currency', 'USD' );
52
		$demo = $config->get( 'setup/default/demo', '' );
53
54
		$siteId = $this->createSite( $site );
55
		$this->createLocale( $siteId, ['locale.languageid' => $lang, 'locale.currencyid' => $curr] );
56
57
		if( $demo === '1' )
58
		{
59
			foreach( $this->data()['locale'] ?? [] as $entry ) {
60
				$this->createLocale( $siteId, $entry );
61
			}
62
		}
63
	}
64
65
66
	protected function createSite( string $code ) : string
67
	{
68
		$manager = \Aimeos\MShop::create( $this->context(), 'locale', 'Standard' )->getSubManager( 'site', 'Standard' );
69
70
		try
71
		{
72
			$item = $manager->find( $code );
73
		}
74
		catch( \Aimeos\MShop\Exception $e )
75
		{
76
			$manager->begin();
77
			$item = $manager->insert( $manager->create()->setCode( $code )->setLabel( ucfirst( $code ) ) );
78
			$manager->commit();
79
		}
80
81
		return $item->getSiteId();
82
	}
83
84
85
	protected function createLocale( string $siteId, array $data )
86
	{
87
		$manager = \Aimeos\MShop::create( $this->context(), 'locale', 'Standard' );
88
89
		try {
90
			$manager->save( $manager->create()->fromArray( $data ) );
91
		} catch( \Aimeos\MShop\Exception $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
92
		}
93
	}
94
95
96
	protected function data() : array
97
	{
98
		$ds = DIRECTORY_SEPARATOR;
99
		$filename = __DIR__ . $ds . 'data' . $ds . 'locale.php';
100
101
		if( ( $data = include( $filename ) ) == false ) {
102
			throw new \RuntimeException( sprintf( 'No data file "%1$s" found', $filename ) );
103
		}
104
105
		return $data;
106
	}
107
}
108