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

Standard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 36
c 1
b 1
f 0
dl 0
loc 106
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddressItems() 0 8 1
A checkEntry() 0 12 4
A process() 0 35 5
A __construct() 0 4 1
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 = $this->getAddressItems( $supplier->getId() );
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
					$item->fromArray( $list );
73
					$supplier->addAddressItem( $item );
74
				} else {
75
					$item->fromArray( $list );
76
					$manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
					$manager->/** @scrutinizer ignore-call */ 
77
               saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
				}
78
			}
79
80
			$data = $this->getObject()->process( $supplier, $data );
81
82
			$manager->commit();
83
		} catch ( \Exception $e ) {
84
			$manager->rollback();
85
			throw $e;
86
		}
87
88
89
		return $data;
90
	}
91
92
	/**
93
	 * Returns the address items for the given supplier code
94
	 *
95
	 * @param string $id Supplier's id
96
	 * @return \Aimeos\Map List of stock items implementing \Aimeos\MShop\Stock\Item\Iface
97
	 */
98
	protected function getAddressItems( $id ): \Aimeos\Map
99
	{
100
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/address' );
101
102
		$search = $manager->createSearch();
103
		$search->setConditions( $search->compare( '==', 'supplier.address.parentid', $id ) );
104
105
		return $manager->searchItems( $search );
106
	}
107
108
	/**
109
	 * Checks if an entry can be used for updating a media item
110
	 *
111
	 * @param array $list Associative list of key/value pairs from the mapping
112
	 * @return bool True if valid, false if not
113
	 */
114
	protected function checkEntry( array $list ): bool
115
	{
116
		if( $this->getValue( $list, 'supplier.address.languageid' ) === null ) {
117
			return false;
118
		}
119
		if( $this->getValue( $list, 'supplier.address.countryid' ) === null ) {
120
			return false;
121
		}
122
		if( $this->getValue( $list, 'supplier.address.city' ) === null ) {
123
			return false;
124
		}
125
		return true;
126
	}
127
}
128