Passed
Push — master ( de4224...45eff3 )
by Aimeos
05:03
created

CouponAddTestData::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 the coupon test data.
15
 */
16
class CouponAddTestData extends Base
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 ['Coupon', 'Media', 'MShopSetLocale', 'ProductAddTestData'];
26
	}
27
28
29
	/**
30
	 * Adds coupon test data.
31
	 */
32
	public function up()
33
	{
34
		$this->info( 'Adding coupon test data', 'v' );
35
		$this->context()->setEditor( 'core:lib/mshoplib' );
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\CouponAddTestData. 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

35
		$this->/** @scrutinizer ignore-call */ 
36
         context()->setEditor( 'core:lib/mshoplib' );
Loading history...
36
37
		$path = __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'coupon.php';
38
39
		if( ( $testdata = include( $path ) ) == false ) {
40
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for coupon test data', $path ) );
41
		}
42
43
		$this->addCouponData( $testdata );
44
	}
45
46
47
	/**
48
	 * Adds the coupon test data.
49
	 *
50
	 * @param array $testdata Associative list of key/list pairs
51
	 */
52
	private function addCouponData( array $testdata )
53
	{
54
		$manager = \Aimeos\MShop\Coupon\Manager\Factory::create( $this->context(), 'Standard' );
55
		$codeManager = $manager->getSubmanager( 'code' );
56
57
		foreach( $testdata['coupon'] ?? [] as $entry )
58
		{
59
			$id = $manager->save( $manager->create()->fromArray( $entry ) )->getId();
60
61
			foreach( $entry['codes'] ?? [] as $values ) {
62
				$codeManager->save( $codeManager->create()->fromArray( $values )->setParentId( $id ), false );
63
			}
64
		}
65
	}
66
}
67