Passed
Push — master ( 547952...cdc5f0 )
by Aimeos
04:14
created

Base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Xml\Processor;
12
13
14
/**
15
 * Abstract class with common methods for all XML import processors
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
abstract class Base
21
{
22
	private $context;
23
	private $types = [];
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
30
	 */
31
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
32
	{
33
		$this->context = $context;
34
	}
35
36
37
	/**
38
	 * Stores all types for which no type items exist yet
39
	 */
40
    public function __destruct()
41
    {
42
        foreach( $this->types as $path => $list )
43
        {
44
			$manager = \Aimeos\MShop::create( $this->context, $path );
45
			$prefix = str_replace( '/', '.', $path );
46
47
			foreach( $list as $domain => $codes )
48
            {
49
                $manager->begin();
50
51
                try
52
                {
53
					$search = $manager->createSearch()->setSlice( 0, 10000 );
54
					$expr = [
55
						$search->compare( '==', $prefix . '.domain', $domain ),
56
						$search->compare( '==', $prefix . '.code', $codes )
57
					];
58
					$search->setConditions( $search->combine( '&&', $expr ) );
59
60
					$types = $items = [];
61
62
					foreach( $manager->searchItems( $search ) as $item ) {
63
						$types[] = $item->getCode();
64
					}
65
66
					foreach( array_diff( $codes, $types ) as $code ) {
67
						$items[] = $manager->createItem()->setDomain( $domain )->setCode( $code )->setLabel( $code );
68
					}
69
70
					$manager->saveItems( $items, false );
71
                    $manager->commit();
72
                }
73
                catch( \Exception $e )
74
                {
75
                    $manager->rollback();
76
                    $this->context->getLogger()->log( 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
77
                }
78
            }
79
        }
80
    }
81
82
83
    /**
84
     * Registers a used type which is going to be saved if it doesn't exist yet
85
     *
86
     * @param string $path Manager path, e.g. "product/lists/type"
87
     * @param string $domain Domain name the type belongs to, e.g. "attribute"
88
     * @param string $code Type code
89
     */
90
    protected function addType( string $path, string $domain, string $code )
91
    {
92
        $this->types[$path][$domain][$code] = $code;
93
    }
94
95
96
	/**
97
	 * Returns the context item
98
	 *
99
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
100
	 */
101
	protected function getContext()
102
	{
103
		return $this->context;
104
	}
105
}
106