Completed
Push — master ( 47a117...268cfa )
by Aimeos
03:32
created

Base   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 14
c 3
b 1
f 0
lcom 2
cbo 2
dl 0
loc 124
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addListItemDefaults() 0 12 3
A getContext() 0 4 1
A getMapping() 0 4 1
A getObject() 0 8 2
A getMappedChunk() 0 20 4
A getValue() 0 4 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
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
class Base
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
22
{
23
	private $context;
24
	private $mapping;
25
	private $object;
26
27
28
	/**
29
	 * Initializes the object
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
32
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
33
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
34
	 */
35
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
36
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
37
	{
38
		$this->context = $context;
39
		$this->mapping = $mapping;
40
		$this->object = $object;
41
	}
42
43
44
	/**
45
	 * Adds the list item default values and returns the resulting array
46
	 *
47
	 * @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1
48
	 * @param integer $pos Computed position of the list item in the associated list of items
49
	 * @return array Given associative list enriched by default values if they were not already set
50
	 */
51
	protected function addListItemDefaults( array $list, $pos )
52
	{
53
		if( !isset( $list['product.lists.position'] ) ) {
54
			$list['product.lists.position'] = $pos;
55
		}
56
57
		if( !isset( $list['product.lists.status'] ) ) {
58
			$list['product.lists.status'] = 1;
59
		}
60
61
		return $list;
62
	}
63
64
65
	/**
66
	 * Returns the context item
67
	 *
68
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
69
	 */
70
	protected function getContext()
71
	{
72
		return $this->context;
73
	}
74
75
76
	/**
77
	 * Returns the mapping list
78
	 *
79
	 * @return array Associative list of field positions in CSV as keys and domain item keys as values
80
	 */
81
	protected function getMapping()
82
	{
83
		return $this->mapping;
84
	}
85
86
87
	/**
88
	 * Returns the decorated processor object
89
	 *
90
	 * @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Processor object
91
	 * @throws \Aimeos\Controller\Jobs\Exception If no processor object is available
92
	 */
93
	protected function getObject()
94
	{
95
		if( $this->object === null ) {
96
			throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' );
97
		}
98
99
		return $this->object;
100
	}
101
102
103
	/**
104
	 * Returns the chunked data with text and product list properties in each chunk
105
	 *
106
	 * @param array $data List of CSV fields with position as key and domain item key as value
107
	 * @return array List of associative arrays containing the chunked properties
108
	 */
109
	protected function getMappedChunk( array &$data )
110
	{
111
		$idx = 0;
112
		$map = array();
113
114
		foreach( $this->getMapping() as $pos => $key )
115
		{
116
			if( isset( $map[$idx][$key] ) ) {
117
				$idx++;
118
			}
119
120
			if( isset( $data[$pos] ) )
121
			{
122
				$map[$idx][$key] = $data[$pos];
123
				unset( $data[$pos] );
124
			}
125
		}
126
127
		return $map;
128
	}
129
130
131
	/**
132
	 * Returns the value from the list or the default value
133
	 *
134
	 * @param array $list Associative list of key/value pairs
135
	 * @param string $key Key for the value to retrieve
136
	 * @param mixed $default Default value if key isn't found
137
	 * @param mixed Value for the key in the list of the default value
138
	 */
139
	protected function getValue( array $list, $key, $default )
140
	{
141
		return ( isset( $list[$key] ) ? $list[$key] : $default );
142
	}
143
}
144