Passed
Push — master ( a1f43f...eeb051 )
by Aimeos
07:11 queued 02:34
created

MShopAddAttributeDataDefault::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2024
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Adds code records to the tables
14
 */
15
class MShopAddAttributeDataDefault 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 ['MShopSetLocale', 'MShopAddTypeDataDefault'];
25
	}
26
27
28
	/**
29
	 * Executes the task
30
	 */
31
	public function up()
32
	{
33
		$site = $this->context()->locale()->getSiteItem()->getCode();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\MShopAddAttributeDataDefault. 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

33
		$site = $this->/** @scrutinizer ignore-call */ context()->locale()->getSiteItem()->getCode();
Loading history...
34
		$this->info( sprintf( 'Adding default attribute data for site "%1$s"', $site ), 'vv' );
35
36
		$ds = DIRECTORY_SEPARATOR;
37
		$path = __DIR__ . $ds . 'default' . $ds . 'data' . $ds . 'attribute.php';
38
39
		if( ( $data = include( $path ) ) == false ) {
40
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for default codes', $path ) );
41
		}
42
43
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
44
		$item = $manager->create();
45
46
		foreach( $data as $dataset )
47
		{
48
			try
49
			{
50
				$item = $item->setId( null )->fromArray( $dataset );
51
				$manager->save( $item );
52
			}
53
			catch( \Exception $e ) { ; } // if attribute was already available
54
		}
55
	}
56
}
57