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

Base::__destruct()   B

Complexity

Conditions 6
Paths 30

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
c 0
b 0
f 0
nc 30
nop 0
dl 0
loc 37
rs 8.9617
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor;
12
13
14
/**
15
 * Abstract class with common methods for all CSV import processors
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
abstract class Base
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Base
22
{
23
	private $context;
24
	private $mapping;
25
	private $object;
26
	private $types = [];
27
28
29
	/**
30
	 * Initializes the object
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
33
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
34
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
37
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
38
	{
39
		$this->context = $context;
40
		$this->mapping = $mapping;
41
		$this->object = $object;
42
	}
43
44
45
	/**
46
	 * Stores all types for which no type items exist yet
47
	 */
48
    public function __destruct()
49
    {
50
        foreach( $this->types as $path => $list )
51
        {
52
			$manager = \Aimeos\MShop::create( $this->context, $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->context->getLogger()->log( 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
85
                }
86
            }
87
        }
88
    }
89
90
91
    /**
92
     * Registers a used type which is going to be saved if it doesn't exist yet
93
     *
94
     * @param string $path Manager path, e.g. "product/lists/type"
95
     * @param string $domain Domain name the type belongs to, e.g. "attribute"
96
     * @param string $code Type code
97
     */
98
    protected function addType( string $path, string $domain, string $code )
99
    {
100
        $this->types[$path][$domain][$code] = $code;
101
    }
102
103
104
	/**
105
	 * Adds the list item default values and returns the resulting array
106
	 *
107
	 * @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1
108
	 * @param integer $pos Computed position of the list item in the associated list of items
109
	 * @return array Given associative list enriched by default values if they were not already set
110
	 */
111
	protected function addListItemDefaults( array $list, $pos )
112
	{
113
		if( !isset( $list['product.lists.position'] ) ) {
114
			$list['product.lists.position'] = $pos;
115
		}
116
117
		if( !isset( $list['product.lists.status'] ) ) {
118
			$list['product.lists.status'] = 1;
119
		}
120
121
		return $list;
122
	}
123
124
125
	/**
126
	 * Returns the context item
127
	 *
128
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
129
	 */
130
	protected function getContext()
131
	{
132
		return $this->context;
133
	}
134
135
136
	/**
137
	 * Returns the mapping list
138
	 *
139
	 * @return array Associative list of field positions in CSV as keys and domain item keys as values
140
	 */
141
	protected function getMapping()
142
	{
143
		return $this->mapping;
144
	}
145
146
147
	/**
148
	 * Returns the decorated processor object
149
	 *
150
	 * @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Processor object
151
	 * @throws \Aimeos\Controller\Jobs\Exception If no processor object is available
152
	 */
153
	protected function getObject()
154
	{
155
		if( $this->object === null ) {
156
			throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' );
157
		}
158
159
		return $this->object;
160
	}
161
}
162