Passed
Push — master ( 2fe25d...e89b12 )
by Aimeos
05:34
created

DemoAddServiceData::removeItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
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-2024
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 ['Service', 'Media', 'Price', 'Text', 'MShopAddTypeDataDefault'];
26
	}
27
28
29
	/**
30
	 * Insert service data.
31
	 */
32
	public function up()
33
	{
34
		$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

34
		/** @scrutinizer ignore-call */ 
35
  $context = $this->context();
Loading history...
35
		$value = $context->config()->get( 'setup/default/demo', '' );
36
37
		if( $value === '' ) {
38
			return;
39
		}
40
41
42
		$this->info( 'Processing service demo data', 'vv' );
43
44
		$items = $this->removeItems();
0 ignored issues
show
Unused Code introduced by
The assignment to $items is dead and can be removed.
Loading history...
45
46
47
		if( $value === '1' ) {
48
			$this->addDemoData();
49
		}
50
	}
51
52
53
	/**
54
	 * Adds the demo data to the database.
55
	 *
56
	 * @throws \RuntimeException If the file isn't found
57
	 */
58
	protected function addDemoData()
59
	{
60
		$ds = DIRECTORY_SEPARATOR;
61
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-service.php';
62
63
		if( ( $data = include( $path ) ) == false ) {
64
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for service domain', $path ) );
65
		}
66
67
		$manager = \Aimeos\MShop::create( $this->context(), 'service' );
68
69
		foreach( $data as $entry )
70
		{
71
			$item = $manager->create()->fromArray( $entry );
72
73
			$this->addRefItems( $item, $entry );
74
75
			$manager->save( $item );
76
		}
77
	}
78
79
80
	/**
81
	 * Deletes the demo service items
82
	 */
83
	protected function removeItems() : \Aimeos\Map
84
	{
85
		$context = $this->context();
86
		$domains = ['media', 'price', 'text'];
87
		$manager = \Aimeos\MShop::create( $context, 'service' );
88
89
		$filter = $manager->filter()->add( 'service.code', '=~', 'demo-' )->slice( 0, 0x7fffffff );
90
		$items = $manager->search( $filter, $domains );
91
92
		$this->removeRefItems( $items, $domains );
93
		$manager->delete( $items );
94
95
		return $items;
96
	}
97
}
98