Passed
Push — master ( 397af5...e6bac3 )
by Aimeos
06:04
created

DemoAddCatalogData::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
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 ['MShopAddCatalogDataDefault', 'DemoAddProductData'];
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
		$this->info( 'Processing catalog demo data' );
46
47
		$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

47
		/** @scrutinizer ignore-call */ 
48
  $context = $this->context();
Loading history...
48
		$value = $context->config()->get( 'setup/default/demo', '' );
49
50
		if( $value === '' )
51
		{
52
			$this->status( 'OK' );
0 ignored issues
show
Bug introduced by
The method status() 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

52
			$this->/** @scrutinizer ignore-call */ 
53
          status( 'OK' );
Loading history...
53
			return;
54
		}
55
56
57
		$item = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
58
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
59
60
		try
61
		{
62
			// Don't delete the catalog node because users are likely use it for production
63
			$item = $manager->getTree( null, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
64
65
			$this->removeItems( $item->getId(), 'catalog/lists', 'catalog', 'media' );
66
			$this->removeItems( $item->getId(), 'catalog/lists', 'catalog', 'text' );
67
			$this->removeListItems( $item->getId(), 'catalog/lists', 'product' );
68
		}
69
		catch( \Exception $e ) {; } // If no root node was already inserted into the database
70
71
		$search = $manager->filter();
72
		$search->setConditions( $search->compare( '=~', 'catalog.code', 'demo-' ) );
73
		$manager->delete( $manager->search( $search )->getId()->toArray() );
74
75
76
		if( $value === '1' )
77
		{
78
			$ds = DIRECTORY_SEPARATOR;
79
			$path = __DIR__ . $ds . 'data' . $ds . 'demo-catalog.php';
80
81
			if( ( $data = include( $path ) ) == false ) {
82
				throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for catalog domain', $path ) );
83
			}
84
85
			if( $item === null ) {
86
				$item = $manager->insert( $manager->create()->fromArray( $data ) );
87
			}
88
89
			if( isset( $data['media'] ) ) {
90
				$this->addMedia( $item->getId(), $data['media'], 'catalog' );
91
			}
92
93
			if( isset( $data['product'] ) ) {
94
				$this->addProducts( $item->getId(), $data['product'], 'catalog' );
95
			}
96
97
			if( isset( $data['text'] ) ) {
98
				$this->addTexts( $item->getId(), $data['text'], 'catalog' );
99
			}
100
101
			if( isset( $data['catalog'] ) ) {
102
				$this->addCatalog( $item->getId(), $data['catalog'], 'catalog' );
103
			}
104
		}
105
	}
106
107
108
	/**
109
	 * Adds the catalog items including referenced items
110
	 *
111
	 * @param string $id Unique ID of the parent category
112
	 * @param array $data List of category data
113
	 * @param string $domain Parent domain name (catalog)
114
	 */
115
	protected function addCatalog( string $id, array $data, string $domain )
116
	{
117
		$manager = \Aimeos\MShop::create( $this->context(), $domain );
118
119
		foreach( $data as $entry )
120
		{
121
			$item = $manager->create()->fromArray( $entry );
122
			$item = $manager->insert( $item, $id );
123
124
			if( isset( $entry['media'] ) ) {
125
				$this->addMedia( $item->getId(), $entry['media'], $domain );
126
			}
127
128
			if( isset( $entry['product'] ) ) {
129
				$this->addProducts( $item->getId(), $entry['product'], $domain );
130
			}
131
132
			if( isset( $entry['text'] ) ) {
133
				$this->addTexts( $item->getId(), $entry['text'], $domain );
134
			}
135
136
			if( isset( $data['catalog'] ) ) {
137
				$this->addCatalog( $item->getId(), $data['catalog'], $domain );
138
			}
139
		}
140
	}
141
}