DemoAddCmsData::up()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 7
nop 0
dl 0
loc 31
rs 9.4222
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-2025
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\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Type\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Basket\Manager\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\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\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Type\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\Plugin\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Customer\Manager\Lists\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
	 * Replaces the IDs in the demo data with the actual ones
112
	 *
113
	 * @param array $data Associative list of CMS demo data
114
	 * @return array Modfied CMS demo data
115
	 */
116
	protected function replaceIds( array $data ) : array
117
	{
118
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
119
		$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-' );
120
121
		$map = [];
122
		foreach( $manager->search( $filter ) as $id => $item ) {
123
			$map[$item->getCode()] = $id;
124
		}
125
126
		foreach( $data as $pos => $entry )
127
		{
128
			foreach( $entry['text'] ?? [] as $idx => $text )
129
			{
130
				$content = $text['text.content'] ?? '';
131
132
				foreach( ['2' => 'demo-best', '3' => 'demo-new', '4' => 'demo-deals'] as $id => $code )
133
				{
134
					if( $newId = $map[$code] ?? null ) {
135
						$content = str_replace( 'catid=\"' . $id . '\"', 'catid=\"' . $newId . '\"', $content );
136
					}
137
				}
138
139
				$data[$pos]['text'][$idx]['text.content'] = $content;
140
			}
141
		}
142
143
		return $data;
144
	}
145
}
146