Completed
Push — master ( 57abee...452e12 )
by Aimeos
30s queued 14s
created

Standard::checkEntry()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 7
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address;
12
13
14
/**
15
 * Address processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/supplier/import/csv/processor/address/name
25
	 * Name of the address processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2020.07
32
	 * @category Developer
33
	 */
34
35
	/**
36
	 * Initializes the object
37
	 *
38
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
39
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
40
	 * @param \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $object Decorated processor
41
	 */
42
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
43
		\Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $object = null )
44
	{
45
		parent::__construct( $context, $mapping, $object );
46
	}
47
48
	/**
49
	 * Saves the supplier related data to the storage
50
	 *
51
	 * @param \Aimeos\MShop\Supplier\Item\Iface $supplier Supplier item with associated items
52
	 * @param array $data List of CSV fields with position as key and data as value
53
	 * @return array List of data which hasn't been imported
54
	 */
55
	public function process( \Aimeos\MShop\Supplier\Item\Iface $supplier, array $data ) : array
56
	{
57
58
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/address' );
59
		$manager->begin();
60
61
		try
62
		{
63
			$map = $this->getMappedChunk( $data, $this->getMapping() );
64
			$items = $supplier->getAddressItems();
65
66
			foreach( $map as $pos => $list )
67
			{
68
				if( $this->checkEntry( $list ) === false )
69
				{
70
					continue;
71
				}
72
73
				if( ($item = $items->pop()) === null )
74
				{
75
					$item = $manager->createItem();
76
				}
77
78
				$item->fromArray( $list );
79
				$supplier->addAddressItem( $item, $items->lastKey() );
80
			}
81
82
			$data = $this->getObject()->process( $supplier, $data );
83
84
			$manager->commit();
85
		} catch( \Exception $e )
86
		{
87
			$manager->rollback();
88
			throw $e;
89
		}
90
91
		return $data;
92
	}
93
94
	/**
95
	 * Checks if an entry can be used for updating a media item
96
	 *
97
	 * @param array $list Associative list of key/value pairs from the mapping
98
	 * @return bool True if valid, false if not
99
	 */
100
	protected function checkEntry( array $list ) : bool
101
	{
102
		if( $this->getValue( $list, 'supplier.address.languageid' ) === null )
103
		{
104
			return false;
105
		}
106
		if( $this->getValue( $list, 'supplier.address.countryid' ) === null )
107
		{
108
			return false;
109
		}
110
		if( $this->getValue( $list, 'supplier.address.city' ) === null )
111
		{
112
			return false;
113
		}
114
		return true;
115
	}
116
}
117