Completed
Pull Request — master (#25)
by
unknown
04:29
created

Standard::getAddressItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 8
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 2018.04
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
0 ignored issues
show
introduced by
The private property $listTypes is not used, and could be removed.
Loading history...
36
	private $types;
0 ignored issues
show
introduced by
The private property $types is not used, and could be removed.
Loading history...
37
38
39
	/**
40
	 * Initializes the object
41
	 *
42
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
43
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
44
	 * @param \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $object Decorated processor
45
	 */
46
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
47
								 \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $object = null )
48
	{
49
		parent::__construct( $context, $mapping, $object );
50
	}
51
52
	/**
53
	 * Saves the supplier related data to the storage
54
	 *
55
	 * @param \Aimeos\MShop\Supplier\Item\Iface $supplier Supplier item with associated items
56
	 * @param array $data List of CSV fields with position as key and data as value
57
	 * @return array List of data which hasn't been imported
58
	 */
59
	public function process( \Aimeos\MShop\Supplier\Item\Iface $supplier, array $data ): array
60
	{
61
62
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier/address' );
63
		$manager->begin();
64
65
		try {
66
			$map = $this->getMappedChunk( $data, $this->getMapping() );
67
			$items = $this->getAddressItems( $supplier->getId() );
68
69
			foreach( $map as $pos => $list ) {
70
71
				if( $this->checkEntry( $list ) === false ) {
72
					continue;
73
				}
74
75
				if( ($item = $items->pop()) === null ) {
76
					$item = $manager->createItem();
77
					$item->fromArray( $list );
78
					$supplier->addAddressItem( $item );
79
				} else {
80
					$item->fromArray( $list );
81
					$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

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