Passed
Push — master ( b3daa9...df431e )
by Aimeos
14:40 queued 03:57
created

DemoAddCmsData::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Upscheme\Task;
10
11
12
/**
13
 * Adds demo records to product tables.
14
 */
15
class DemoAddCmsData extends 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 after() : 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 before() : array
34
	{
35
		return ['DemoRebuildIndex'];
36
	}
37
38
39
	/**
40
	 * Insert product data.
41
	 */
42
	public function up()
43
	{
44
		$this->info( 'Processing CMS demo data', 'v' );
45
46
		$context = $this->context();
0 ignored issues
show
Bug introduced by
The method context() does not exist on Aimeos\Upscheme\Task\DemoAddCmsData. 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

46
		/** @scrutinizer ignore-call */ 
47
  $context = $this->context();
Loading history...
47
		$value = $context->getConfig()->get( 'setup/default/demo', '' );
48
49
		if( $value === '' ) {
50
			return;
51
		}
52
53
54
		$domains = ['media', 'text'];
55
		$manager = \Aimeos\MShop::create( $context, 'cms' );
56
57
		$search = $manager->filter();
58
		$search->setConditions( $search->compare( '=~', 'cms.label', 'Demo ' ) );
59
		$pages = $manager->search( $search, $domains );
60
61
		foreach( $domains as $domain )
62
		{
63
			$rmIds = map();
64
65
			foreach( $pages as $item ) {
66
				$rmIds = $rmIds->merge( $item->getRefItems( $domain, null, null, false )->keys() );
67
			}
68
69
			\Aimeos\MShop::create( $context, $domain )->delete( $rmIds->toArray() );
70
		}
71
72
		$manager->delete( $pages->toArray() );
73
74
75
		if( $value === '1' ) {
76
			$this->addDemoData();
77
		}
78
	}
79
80
81
	/**
82
	 * Adds the demo data to the database.
83
	 *
84
	 * @throws \Aimeos\MShop\Exception If the file isn't found
85
	 */
86
	protected function addDemoData()
87
	{
88
		$ds = DIRECTORY_SEPARATOR;
89
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-cms.php';
90
91
		if( ( $data = include( $path ) ) == false ) {
92
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for CMS domain', $path ) );
93
		}
94
95
		$data = $this->replaceIds( $data );
96
		$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
97
98
		foreach( $data as $entry )
99
		{
100
			try
101
			{
102
				$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

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