Passed
Push — master ( b5a62a...377249 )
by Aimeos
02:14
created

Bundle::getBundleProducts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Basket\Decorator;
12
13
14
/**
15
 * Bundle product handling
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Bundle
21
	extends \Aimeos\Controller\Frontend\Basket\Decorator\Base
22
	implements \Aimeos\Controller\Frontend\Basket\Iface, \Aimeos\Controller\Frontend\Common\Decorator\Iface
23
{
24
	/**
25
	 * Adds a product to the basket of the customer stored in the session
26
	 *
27
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product to add including texts, media, prices, attributes, etc.
28
	 * @param integer $quantity Amount of products that should by added
29
	 * @param array $variant List of variant-building attribute IDs that identify an article in a selection product
30
	 * @param array $config List of configurable attribute IDs the customer has chosen from
31
	 * @param array $custom Associative list of attribute IDs as keys and arbitrary values that will be added to the ordered product
32
	 * @param string $stocktype Unique code of the stock type to deliver the products from
33
	 * @param string|null $supplier Unique supplier code the product is from
34
	 * @return \Aimeos\Controller\Frontend\Basket\Iface Basket frontend object for fluent interface
35
	 * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available
36
	 */
37
	public function addProduct( \Aimeos\MShop\Product\Item\Iface $product, $quantity = 1,
38
		array $variant = [], array $config = [], array $custom = [], $stocktype = 'default', $supplier = null )
39
	{
40
		if( $product->getType() !== 'bundle' )
41
		{
42
			$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype, $supplier );
43
			return $this;
44
		}
45
46
		$attributeMap = ['custom' => array_keys( $custom ), 'config' => array_keys( $config )];
47
		$this->checkListRef( $product->getId(), 'attribute', $attributeMap );
48
49
		$prices = $product->getRefItems( 'price', 'default', 'default' );
50
		$hidden = $product->getRefItems( 'attribute', null, 'hidden' );
51
52
		$custAttr = $this->getOrderProductAttributes( 'custom', array_keys( $custom ), $custom );
53
		$confAttr = $this->getOrderProductAttributes( 'config', array_keys( $config ), [], $config );
54
		$hideAttr = $this->getOrderProductAttributes( 'hidden', array_keys( $hidden ) );
55
56
		$orderBaseProductItem = \Aimeos\MShop::create( $this->getContext(), 'order/base/product' )->createItem();
57
58
		$orderBaseProductItem = $orderBaseProductItem->copyFrom( $product )
0 ignored issues
show
Bug introduced by
The method copyFrom() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

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

58
		$orderBaseProductItem = $orderBaseProductItem->/** @scrutinizer ignore-call */ copyFrom( $product )

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...
59
			->setProducts( $this->getBundleProducts( $product, $quantity, $stocktype, $supplier ) )
60
			->setQuantity( $quantity )->setStockType( $stocktype )->setSupplierCode( $supplier )
61
			->setPrice( $this->calcPrice( $orderBaseProductItem, $prices, $quantity ) )
0 ignored issues
show
Bug introduced by
$orderBaseProductItem of type Aimeos\MShop\Attribute\Item\Iface is incompatible with the type Aimeos\MShop\Order\Item\Base\Product\Iface expected by parameter $product of Aimeos\Controller\Fronte...asket\Base::calcPrice(). ( Ignorable by Annotation )

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

61
			->setPrice( $this->calcPrice( /** @scrutinizer ignore-type */ $orderBaseProductItem, $prices, $quantity ) )
Loading history...
62
			->setAttributeItems( array_merge( $custAttr, $confAttr, $hideAttr ) );
63
64
		$this->getController()->get()->addProduct( $orderBaseProductItem );
65
		$this->getController()->save();
66
67
		return $this;
68
	}
69
70
71
	/**
72
	 * Adds the bundled products to the order product item.
73
	 *
74
	 * @param \Aimeos\MShop\Product\Item\Iface $product Bundle product item
75
	 * @param integer $quantity Amount of products that should by added
76
	 * @param string $stocktype Unique code of the stock type to deliver the products from
77
	 * @param string|null $supplier Unique supplier code the product is from
78
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Iface[] List of order product item from bundle
79
	 */
80
	protected function getBundleProducts( \Aimeos\MShop\Product\Item\Iface $product, $quantity, $stocktype, $supplier )
81
	{
82
		$orderProducts = [];
83
		$orderProductManager = \Aimeos\MShop::create( $this->getContext(), 'order/base/product' );
84
85
		foreach( $product->getRefItems( 'product', null, 'default' ) as $item )
86
		{
87
			$orderProduct = $orderProductManager->createItem()->copyFrom( $item );
88
			$prices = $item->getRefItems( 'price', 'default', 'default' );
89
90
			$orderProducts[] = $orderProduct->setStockType( $stocktype )->setSupplierCode( $supplier )
91
				->setPrice( $this->calcPrice( $orderProduct, $prices, $quantity ) );
92
		}
93
94
		return $orderProducts;
95
	}
96
}
97