Passed
Push — master ( bba07b...c2f305 )
by Aimeos
02:28
created

Traits   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B saveTypes() 0 37 6
A addType() 0 4 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Common\Import;
12
13
14
/**
15
 * Shared class for XML importers
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	private $typeMap = [];
23
24
25
	abstract protected function getContext();
26
27
28
	/**
29
	 * Registers a used type which is going to be saved if it doesn't exist yet
30
	 *
31
	 * @param string $path Manager path, e.g. "product/lists/type"
32
	 * @param string $domain Domain name the type belongs to, e.g. "attribute"
33
	 * @param string $code Type code
34
	 * @param self Same object for method chaining
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Common\Common\Import\Same was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
	 */
36
	protected function addType( $path, $domain, $code )
37
	{
38
		$this->typeMap[$path][$domain][$code] = $code;
39
		return $this;
40
	}
41
42
43
	/**
44
	 * Stores all types for which no type items exist yet
45
	 *
46
	 * @param self Same object for method chaining
47
	 */
48
	protected function saveTypes()
49
	{
50
		foreach( $this->typeMap as $path => $list )
51
		{
52
			$manager = \Aimeos\MShop::create( $this->getContext(), $path );
53
			$prefix = str_replace( '/', '.', $path );
54
55
			foreach( $list as $domain => $codes )
56
			{
57
				$manager->begin();
58
59
				try
60
				{
61
					$search = $manager->createSearch()->setSlice( 0, 10000 );
62
					$expr = [
63
						$search->compare( '==', $prefix . '.domain', $domain ),
64
						$search->compare( '==', $prefix . '.code', $codes )
65
					];
66
					$search->setConditions( $search->combine( '&&', $expr ) );
67
68
					$types = $items = [];
69
70
					foreach( $manager->searchItems( $search ) as $item ) {
71
						$types[] = $item->getCode();
72
					}
73
74
					foreach( array_diff( $codes, $types ) as $code ) {
75
						$items[] = $manager->createItem()->setDomain( $domain )->setCode( $code )->setLabel( $code );
76
					}
77
78
					$manager->saveItems( $items, false );
79
					$manager->commit();
80
				}
81
				catch( \Exception $e )
82
				{
83
					$manager->rollback();
84
					$this->getContext()->getLogger()->log( 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
85
				}
86
			}
87
		}
88
	}
89
}
90