Passed
Push — master ( 1adebf...0cda2e )
by Aimeos
04:50
created

SubscriptionAddTestData::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.9
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2021
6
 */
7
8
9
namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Adds subscription test data
14
 */
15
class SubscriptionAddTestData 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 ['Subscription', 'OrderAddTestData', 'SubscriptionMigratePeriod', 'SubscriptionMigrateProdcode'];
25
	}
26
27
28
	/**
29
	 * Adds subscription test data
30
	 */
31
	public function up()
32
	{
33
		$this->info( 'Adding subscription test data', 'v' );
34
		$this->context()->setEditor( 'core:lib/mshoplib' );
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\SubscriptionAddTestData. 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

34
		$this->/** @scrutinizer ignore-call */ 
35
         context()->setEditor( 'core:lib/mshoplib' );
Loading history...
35
36
		$ds = DIRECTORY_SEPARATOR;
37
		$path = __DIR__ . $ds . 'data' . $ds . 'subscription.php';
38
39
		if( ( $testdata = include( $path ) ) == false ) {
40
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for subscription domain', $path ) );
41
		}
42
43
		$this->import( $testdata );
44
	}
45
46
47
	/**
48
	 * Adds the required subscription base data.
49
	 *
50
	 * @param array $data List of arrays
51
	 */
52
	protected function import( array $data )
53
	{
54
		$list = [];
55
		$manager = \Aimeos\MShop\Subscription\Manager\Factory::create( $this->context(), 'Standard' );
56
57
		foreach( $data as $entry )
58
		{
59
			$ordProdItem = $this->getOrderProductItem( $entry['ordprodid'] );
60
61
			$list[] = $manager->create()->fromArray( $entry, true )
62
				->setOrderBaseId( $ordProdItem->getBaseId() )
63
				->setOrderProductId( $ordProdItem->getId() )
64
				->setProductId( $ordProdItem->getProductId() );
65
		}
66
67
		$manager->begin();
68
		$manager->save( $list );
69
		$manager->commit();
70
	}
71
72
73
	/**
74
	 * Returns the order product ID for the given test data key
75
	 *
76
	 * @param string $key Test data key
77
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Iface Order product item
78
	 */
79
	protected function getOrderProductItem( $key )
80
	{
81
		$manager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context(), 'Standard' )
82
			->getSubManager( 'base' )->getSubManager( 'product' );
83
84
		$parts = explode( '/', $key );
85
86
		if( count( $parts ) !== 2 ) {
87
			throw new \RuntimeException( sprintf( 'Invalid order product key "%1$s"', $key ) );
88
		}
89
90
		$search = $manager->filter()->add( [
91
			'order.base.product.prodcode' => $parts[0],
92
			'order.base.product.price' => $parts[1],
93
		] );
94
95
		return $manager->search( $search )->first( new \RuntimeException( 'No order product item found for ' . $key ) );
96
	}
97
}
98