Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Traits::saveAddressItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Manager\AddressRef;
12
13
14
/**
15
 * Common trait for managers retrieving/storing address items
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	/**
23
	 * Returns the address items for the given parent IDs
24
	 *
25
	 * @param array $parentIds List of parent IDs
26
	 * @param string $domain Domain of the calling manager
27
	 * @return array Associative list of parent IDs / address IDs as keys and items implementing
28
	 * 	\Aimeos\MShop\Common\Item\Address\Iface as values
29
	 */
30
	protected function getAddressItems( array $parentIds, $domain )
31
	{
32
		$list = [];
33
34
		if( !empty( $parentIds ) )
35
		{
36
			$manager = $this->getObject()->getSubManager( 'address' );
0 ignored issues
show
Bug introduced by
It seems like getObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
38
			$search = $manager->createSearch();
39
			$search->setConditions( $search->compare( '==', $domain . '.address.parentid', $parentIds ) );
40
			$search->setSlice( 0, 0x7fffffff );
41
42
			foreach( $manager->searchItems( $search ) as $id => $addrItem ) {
43
				$list[$addrItem->getParentId()][$id] = $addrItem;
44
			}
45
		}
46
47
		return $list;
48
	}
49
50
51
	/**
52
	 * Adds new, updates existing and deletes removed address items
53
	 *
54
	 * @param \Aimeos\MShop\Common\Item\AddressRef\Iface $item Item with referenced items
55
	 * @param string $domain Domain of the calling manager
56
	 * @return \Aimeos\MShop\Common\Item\AddressRef\Iface Item with saved referenced items
57
	 */
58
	protected function saveAddressItems( \Aimeos\MShop\Common\Item\AddressRef\Iface $item, $domain )
59
	{
60
		$manager = $this->getObject()->getSubManager( 'address' );
0 ignored issues
show
Bug introduced by
It seems like getObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
		$manager->deleteItems( array_keys( $item->getAddressItemsDeleted() ) );
62
63
		foreach( $item->getAddressItems() as $addrItem )
64
		{
65
			if( $addrItem->getParentId() != $item->getId() ) {
66
				$addrItem->setId( null ); //create new address item if copied
67
			}
68
69
			$addrItem->setParentId( $item->getId() );
70
			$manager->saveItem( $addrItem );
71
		}
72
73
		return $item;
74
	}
75
}
76