Completed
Pull Request — master (#25)
by
unknown
02:53
created

Standard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 4
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
			$map = $this->getMappedChunk( $data, $this->getMapping() );
63
			$items = $supplier->getAddressItems();
64
65
			foreach( $map as $pos => $list ) {
66
				if( $this->checkEntry( $list ) === false ) {
67
					continue;
68
				}
69
70
				if( ($item = $items->pop()) === null ) {
71
					$item = $manager->createItem();
72
				}
73
74
				$item->fromArray( $list );
75
				$supplier->addAddressItem( $item, $items->lastKey() );
76
			}
77
78
			$data = $this->getObject()->process( $supplier, $data );
79
80
			$manager->commit();
81
		} catch( \Exception $e ) {
82
			$manager->rollback();
83
			throw $e;
84
		}
85
86
		return $data;
87
	}
88
89
	/**
90
	 * Checks if an entry can be used for updating a media item
91
	 *
92
	 * @param array $list Associative list of key/value pairs from the mapping
93
	 * @return bool True if valid, false if not
94
	 */
95
	protected function checkEntry( array $list ): bool
96
	{
97
		if( $this->getValue( $list, 'supplier.address.languageid' ) === null ) {
98
			return false;
99
		}
100
		if( $this->getValue( $list, 'supplier.address.countryid' ) === null ) {
101
			return false;
102
		}
103
		if( $this->getValue( $list, 'supplier.address.city' ) === null ) {
104
			return false;
105
		}
106
		return true;
107
	}
108
}
109