Passed
Push — 2021.07 ( 38519a...a0cca1 )
by Aimeos
09:40 queued 03:17
created

DemoAddCmsData::replaceIds()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 10
nop 1
dl 0
loc 28
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds demo records to product tables.
14
 */
15
class DemoAddCmsData extends \Aimeos\MW\Setup\Task\MShopAddDataAbstract
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 getPreDependencies() : array
23
	{
24
		return ['MShopAddTypeDataCms', 'DemoAddTypeData'];
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return string[] List of task names
32
	 */
33
	public function getPostDependencies() : array
34
	{
35
		return ['DemoRebuildIndex'];
36
	}
37
38
39
	/**
40
	 * Insert product data.
41
	 */
42
	public function migrate()
43
	{
44
		$this->msg( 'Processing CMS demo data', 0 );
45
46
		$context = $this->getContext();
47
		$value = $context->getConfig()->get( 'setup/default/demo', '' );
48
49
		if( $value === '' )
50
		{
51
			$this->status( 'OK' );
52
			return;
53
		}
54
55
56
		$domains = ['media', 'text'];
57
		$manager = \Aimeos\MShop::create( $context, 'cms' );
58
59
		$search = $manager->filter();
60
		$search->setConditions( $search->compare( '=~', 'cms.label', 'Demo ' ) );
61
		$pages = $manager->search( $search, $domains );
62
63
		foreach( $domains as $domain )
64
		{
65
			$rmIds = map();
66
67
			foreach( $pages as $item ) {
68
				$rmIds = $rmIds->merge( $item->getRefItems( $domain, null, null, false )->keys() );
69
			}
70
71
			\Aimeos\MShop::create( $context, $domain )->delete( $rmIds->toArray() );
72
		}
73
74
		$manager->delete( $pages->toArray() );
75
76
77
		if( $value === '1' )
78
		{
79
			$this->addDemoData();
80
			$this->status( 'added' );
81
		}
82
		else
83
		{
84
			$this->status( 'removed' );
85
		}
86
	}
87
88
89
	/**
90
	 * Adds the demo data to the database.
91
	 *
92
	 * @throws \Aimeos\MShop\Exception If the file isn't found
93
	 */
94
	protected function addDemoData()
95
	{
96
		$ds = DIRECTORY_SEPARATOR;
97
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-cms.php';
98
99
		if( ( $data = include( $path ) ) == false ) {
100
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for CMS domain', $path ) );
101
		}
102
103
		$data = $this->replaceIds( $data );
104
		$manager = \Aimeos\MShop::create( $this->getContext(), 'cms' );
105
106
		foreach( $data as $entry )
107
		{
108
			try
109
			{
110
				$manager->find( $entry['cms.url'] );
0 ignored issues
show
Bug introduced by
The method find() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Base\Coupon\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Order\Manager\Base\Service\Iface or Aimeos\MShop\Order\Manager\Base\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Order\Manag...Service\Attribute\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Order\Manag...Product\Attribute\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Address\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Cms\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
				$manager->/** @scrutinizer ignore-call */ 
111
              find( $entry['cms.url'] );
Loading history...
111
			}
112
			catch( \Aimeos\MShop\Exception $e )
113
			{
114
				$item = $manager->create()->fromArray( $entry );
115
				$this->addRefItems( $item, $entry );
116
				$manager->save( $item );
117
			}
118
		}
119
	}
120
121
122
	/**
123
	 * Adds the referenced items from the given entry data.
124
	 *
125
	 * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with list items
126
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
127
	 * @return \Aimeos\MShop\Common\Item\ListsRef\Iface $item Updated item
128
	 */
129
	protected function addRefItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entry )
130
	{
131
		$context = $this->getContext();
132
		$domain = $item->getResourceType();
133
		$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' );
134
135
		foreach( ['media', 'text'] as $refDomain )
136
		{
137
			if( isset( $entry[$refDomain] ) )
138
			{
139
				$manager = \Aimeos\MShop::create( $context, $refDomain );
140
141
				foreach( $entry[$refDomain] as $data )
142
				{
143
					$listItem = $listManager->create()->fromArray( $data );
144
					$refItem = $manager->create()->fromArray( $data );
145
146
					$item->addListItem( $refDomain, $listItem, $refItem );
147
				}
148
			}
149
		}
150
151
		return $item;
152
	}
153
154
155
	/**
156
	 * Replaces the IDs in the demo data with the actual ones
157
	 *
158
	 * @param array $data Associative list of CMS demo data
159
	 * @return array Modfied CMS demo data
160
	 */
161
	protected function replaceIds( array $data ) : array
162
	{
163
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
164
		$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-' );
165
166
		$map = [];
167
		foreach( $manager->search( $filter ) as $id => $item ) {
168
			$map[$item->getCode()] = $id;
169
		}
170
171
		foreach( $data as $pos => $entry )
172
		{
173
			foreach( $entry['text'] ?? [] as $idx => $text )
174
			{
175
				$content = $text['text.content'] ?? '';
176
177
				foreach( ['2' => 'demo-best', '3' => 'demo-new', '4' => 'demo-deals'] as $id => $code )
178
				{
179
					if( $newId = $map[$code] ?? null ) {
180
						$content = str_replace( 'catid=\"' . $id . '\"', 'catid=\"' . $newId . '\"', $content );
181
					}
182
				}
183
184
				$data[$pos]['text'][$idx]['text.content'] = $content;
185
			}
186
		}
187
188
		return $data;
189
	}
190
}
191