DemoAddCmsData::addRefItems()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 23
rs 9.9
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-2022
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
		$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

44
		/** @scrutinizer ignore-call */ 
45
  $context = $this->context();
Loading history...
45
		$value = $context->config()->get( 'setup/default/demo', '' );
46
47
		if( $value === '' ) {
48
			return;
49
		}
50
51
52
		$this->info( 'Processing CMS demo data', 'vv' );
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( $pages as $item )
62
		{
63
			foreach( $domains as $domain ) {
64
				$item->deleteListItems( $item->getListItems( $domain, null, null, false ), true );
65
			}
66
		}
67
68
		$manager->delete( $pages );
69
70
71
		if( $value === '1' ) {
72
			$this->addDemoData();
73
		}
74
	}
75
76
77
	/**
78
	 * Adds the demo data to the database.
79
	 *
80
	 * @throws \Aimeos\MShop\Exception If the file isn't found
81
	 */
82
	protected function addDemoData()
83
	{
84
		$ds = DIRECTORY_SEPARATOR;
85
		$path = __DIR__ . $ds . 'data' . $ds . 'demo-cms.php';
86
87
		if( ( $data = include( $path ) ) == false ) {
88
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for CMS domain', $path ) );
89
		}
90
91
		$data = $this->replaceIds( $data );
92
		$manager = \Aimeos\MShop::create( $this->context(), 'cms' );
93
94
		foreach( $data as $entry )
95
		{
96
			try
97
			{
98
				$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\Basket\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

98
				$manager->/** @scrutinizer ignore-call */ 
99
              find( $entry['cms.url'] );
Loading history...
99
			}
100
			catch( \Aimeos\MShop\Exception $e )
101
			{
102
				$item = $manager->create()->fromArray( $entry );
103
				$this->addRefItems( $item, $entry );
104
				$manager->save( $item );
105
			}
106
		}
107
	}
108
109
110
	/**
111
	 * Adds the referenced items from the given entry data.
112
	 *
113
	 * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with list items
114
	 * @param array $entry Associative list of data with stock, attribute, media, price, text and product sections
115
	 * @return \Aimeos\MShop\Common\Item\ListsRef\Iface $item Updated item
116
	 */
117
	protected function addRefItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entry )
118
	{
119
		$context = $this->context();
120
		$domain = $item->getResourceType();
121
		$manager = \Aimeos\MShop::create( $context, $domain );
122
123
		foreach( ['media', 'text'] as $refDomain )
124
		{
125
			if( isset( $entry[$refDomain] ) )
126
			{
127
				$refManager = \Aimeos\MShop::create( $context, $refDomain );
128
129
				foreach( $entry[$refDomain] as $data )
130
				{
131
					$listItem = $manager->createListItem()->fromArray( $data );
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

131
					$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->fromArray( $data );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
					$refItem = $refManager->create()->fromArray( $data );
133
134
					$item->addListItem( $refDomain, $listItem, $refItem );
135
				}
136
			}
137
		}
138
139
		return $item;
140
	}
141
142
143
	/**
144
	 * Replaces the IDs in the demo data with the actual ones
145
	 *
146
	 * @param array $data Associative list of CMS demo data
147
	 * @return array Modfied CMS demo data
148
	 */
149
	protected function replaceIds( array $data ) : array
150
	{
151
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
152
		$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-' );
153
154
		$map = [];
155
		foreach( $manager->search( $filter ) as $id => $item ) {
156
			$map[$item->getCode()] = $id;
157
		}
158
159
		foreach( $data as $pos => $entry )
160
		{
161
			foreach( $entry['text'] ?? [] as $idx => $text )
162
			{
163
				$content = $text['text.content'] ?? '';
164
165
				foreach( ['2' => 'demo-best', '3' => 'demo-new', '4' => 'demo-deals'] as $id => $code )
166
				{
167
					if( $newId = $map[$code] ?? null ) {
168
						$content = str_replace( 'catid=\"' . $id . '\"', 'catid=\"' . $newId . '\"', $content );
169
					}
170
				}
171
172
				$data[$pos]['text'][$idx]['text.content'] = $content;
173
			}
174
		}
175
176
		return $data;
177
	}
178
}
179