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

DemoAddCatalogData::up()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
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 catalog tables.
15
 */
16
class DemoAddCatalogData 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 ['Catalog', 'Media', 'Product', 'Text', 'MShopSetLocale'];
26
	}
27
28
29
	/**
30
	 * Returns the list of task names which depends on this task.
31
	 *
32
	 * @return string[] List of task names
33
	 */
34
	public function before() : array
35
	{
36
		return ['DemoRebuildIndex'];
37
	}
38
39
40
	/**
41
	 * Insert catalog nodes and relations.
42
	 */
43
	public function up()
44
	{
45
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddCatalogData. 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

45
		/** @scrutinizer ignore-call */ 
46
  $context = $this->context();
Loading history...
46
47
		if( ( $value = $context->config()->get( 'setup/default/demo', '' ) ) === '' ) {
48
			return;
49
		}
50
51
52
		$this->info( 'Processing catalog demo data', 'vv' );
53
54
		$item = $this->removeItems();
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
55
56
57
		if( $value === '1' ) {
58
			$this->addDemoData();
59
		}
60
	}
61
62
63
	/**
64
	 * Adds the demo data to the database.
65
	 *
66
	 * @throws \RuntimeException If the file isn't found
67
	 */
68
	protected function addDemoData()
69
	{
70
		$ds = DIRECTORY_SEPARATOR;
71
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-catalog.php';
72
73
		if( ( $data = include( $path ) ) == false ) {
74
			throw new \RuntimeException( sprintf( 'No file "%1$s" found for catalog domain', $path ) );
75
		}
76
77
		$context = $this->context();
78
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
79
80
		try {
81
			$item = $manager->getTree( null, ['media', 'text'], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
82
		} catch( \Exception $e ) {
83
			$item = $manager->insert( $manager->create()->fromArray( $data ) );
84
		}
85
var_dump($item->getId(), $item->getLabel());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($item->getId(), $item->getLabel()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
86
87
		$this->addRefItems( $item, $data );
88
89
		$manager->save( $item );
90
	}
91
92
93
	protected function addCategories( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $data ) : \Aimeos\MShop\Common\Item\ListsRef\Iface
94
	{
95
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
96
97
		foreach( $data as $entry )
98
		{
99
			$catItem = $manager->create()->fromArray( $entry );
100
			$catItem = $manager->insert( $catItem, $item->getId() );
101
102
			$this->addRefItems( $catItem, $entry );
103
		}
104
105
		return $item;
106
	}
107
108
109
	/**
110
	 * Deletes the demo catalog items
111
	 */
112
	protected function removeItems() : ?\Aimeos\MShop\Catalog\Item\Iface
113
	{
114
		$context = $this->context();
115
		$domains = ['media', 'text'];
116
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
117
118
		try
119
		{
120
			// Don't delete the catalog node because users are likely use it for production
121
			$item = $manager->getTree( null, $domains, \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
122
			$this->removeRefItems( map( $item ), $domains );
123
			$manager->save( $item );
124
		}
125
		catch( \Exception $e ) { ; } // If no root node was already inserted into the database
126
127
		$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-')->add( 'catalog.level', '==', 1 )->slice( 0, 0x7fffffff );
128
		$items = $manager->search( $filter, $domains );
129
130
		$this->removeRefItems( $items, $domains );
131
132
		$manager->delete( $items );
133
134
		return $item ?? null;
135
	}
136
}
137