Completed
Push — master ( 7f402b...a6068b )
by Aimeos
10:08
created

Traits::savePropertyItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
rs 9.4285
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\PropertyRef;
12
13
14
/**
15
 * Common trait for managers retrieving/storing property items
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	/**
23
	 * Returns the property 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 / property IDs as keys and items implementing
28
	 * 	\Aimeos\MShop\Common\Item\Property\Iface as values
29
	 */
30
	protected function getPropertyItems( array $parentIds, $domain )
31
	{
32
		$list = [];
33
34
		if( !empty( $parentIds ) )
35
		{
36
			$propManager = $this->getObject()->getSubManager( 'property' );
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
			$propSearch = $propManager->createSearch();
39
			$propSearch->setConditions( $propSearch->compare( '==', $domain . '.property.parentid', $parentIds ) );
40
			$propSearch->setSortations( [$propSearch->sort( '+', $domain . '.property.type.position' )] );
41
			$propSearch->setSlice( 0, 0x7fffffff );
42
43
			foreach( $propManager->searchItems( $propSearch ) as $id => $propItem ) {
44
				$list[$propItem->getParentId()][$id] = $propItem;
45
			}
46
		}
47
48
		return $list;
49
	}
50
51
52
	/**
53
	 * Adds new, updates existing and deletes removed property items
54
	 *
55
	 * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with referenced items
0 ignored issues
show
Documentation introduced by
Should the type for parameter $item not be \Aimeos\MShop\Common\Item\PropertyRef\Iface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
	 * @param string $domain Domain of the calling manager
57
	 * @return \Aimeos\MShop\Common\Item\ListRef\Iface Item with saved referenced items
0 ignored issues
show
Documentation introduced by
Should the return type not be \Aimeos\MShop\Common\Item\PropertyRef\Iface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
	 */
59
	protected function savePropertyItems( \Aimeos\MShop\Common\Item\PropertyRef\Iface $item, $domain )
60
	{
61
		$propManager = $this->getObject()->getSubManager( 'property' );
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...
62
		$propManager->deleteItems( array_keys( $item->getPropertyItemsDeleted() ) );
63
64
		foreach( $item->getPropertyItems( null, false ) as $propItem )
65
		{
66
			if( $propItem->getParentId() != $item->getId() ) {
67
				$propItem->setId( null ); //create new property item if copied
68
			}
69
70
			$propItem->setParentId( $item->getId() );
71
			$propManager->saveItem( $propItem );
72
		}
73
74
		return $item;
75
	}
76
}
77