Passed
Push — master ( bada6f...d61b94 )
by Aimeos
04:42
created

DemoAddServiceData::migrate()   B

Complexity

Conditions 9
Paths 23

Size

Total Lines 70
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 39
nc 23
nop 0
dl 0
loc 70
rs 7.7404
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
9
10
namespace Aimeos\Upscheme\Task;
11
12
13
/**
14
 * Adds demo records to service tables.
15
 */
16
class DemoAddServiceData extends MShopAddDataAbstract
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 ['MShopAddTypeDataDefault'];
26
	}
27
28
29
	/**
30
	 * Insert service data.
31
	 */
32
	public function up()
33
	{
34
		$this->info( 'Processing service demo data', 'v' );
35
36
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddServiceData. 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

36
		/** @scrutinizer ignore-call */ 
37
  $context = $this->context();
Loading history...
37
		$value = $context->getConfig()->get( 'setup/default/demo', '' );
38
39
		if( $value === '' ) {
40
			return;
41
		}
42
43
44
		$manager = \Aimeos\MShop::create( $context, 'service' );
45
46
		$search = $manager->filter();
47
		$search->setConditions( $search->compare( '=~', 'service.code', 'demo-' ) );
48
		$services = $manager->search( $search );
49
50
		foreach( $services as $item )
51
		{
52
			$this->removeItems( $item->getId(), 'service/lists', 'service', 'media' );
53
			$this->removeItems( $item->getId(), 'service/lists', 'service', 'price' );
54
			$this->removeItems( $item->getId(), 'service/lists', 'service', 'text' );
55
		}
56
57
		$manager->delete( $services->toArray() );
58
59
60
		if( $value === '1' )
61
		{
62
			$ds = DIRECTORY_SEPARATOR;
63
			$path = __DIR__ . $ds . 'data' . $ds . 'demo-service.php';
64
65
			if( ( $data = include( $path ) ) == false ) {
66
				throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for service domain', $path ) );
67
			}
68
69
			foreach( $data as $entry )
70
			{
71
				$item = $manager->create();
72
				$item->setType( $entry['type'] );
73
				$item->setCode( $entry['code'] );
74
				$item->setLabel( $entry['label'] );
75
				$item->setProvider( $entry['provider'] );
76
				$item->setPosition( $entry['position'] );
77
				$item->setConfig( $entry['config'] );
78
				$item->setStatus( $entry['status'] );
79
80
				$manager->save( $item );
81
82
				if( isset( $entry['media'] ) ) {
83
					$this->addMedia( $item->getId(), $entry['media'], 'service' );
84
				}
85
86
				if( isset( $entry['price'] ) ) {
87
					$this->addPrices( $item->getId(), $entry['price'], 'service' );
88
				}
89
90
				if( isset( $entry['text'] ) ) {
91
					$this->addTexts( $item->getId(), $entry['text'], 'service' );
92
				}
93
			}
94
		}
95
	}
96
}
97