Category   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addProduct() 0 25 4
A checkCategory() 0 9 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Basket\Decorator;
12
13
14
/**
15
 * Category check for basket controller
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Category
21
	extends 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 float $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 $siteId Unique ID of the site the product should be bought from or NULL for site 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, float $quantity = 1,
38
		array $variant = [], array $config = [], array $custom = [], string $stocktype = 'default', ?string $siteId = null
39
	) : \Aimeos\Controller\Frontend\Basket\Iface
40
	{
41
		if( $product->getListItems( 'catalog' )->isEmpty() )
42
		{
43
			$context = $this->context();
44
			$manager = \Aimeos\MShop::create( $context, 'product' );
45
46
			$filter = $manager->filter( true );
47
			$func = $filter->make( 'product:has', ['product', 'default', $product->getId()] );
48
			$filter->add( $filter->is( $func, '!=', null ) );
49
50
			$prodIds = $manager->search( $filter )->keys()->all();
51
52
			if( empty( $prodIds ) || !$this->checkCategory( $prodIds ) )
53
			{
54
				$msg = $context->translate( 'controller/frontend', 'Adding product with ID "%1$s" is not allowed' );
55
				throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $product->getId() ) );
56
			}
57
		}
58
59
		$this->getController()->addProduct( $product, $quantity, $variant, $config, $custom, $stocktype, $siteId );
60
61
		return $this;
62
	}
63
64
65
	/**
66
	 * Checks if the given product IDs are assigned to a category
67
	 *
68
	 * @param iterable $prodIds Unique product IDs to check for
69
	 * @return bool True if at least one product ID is assigned to a category, false if not
70
	 */
71
	protected function checkCategory( iterable $prodIds ) : bool
72
	{
73
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
74
75
		$filter = $manager->filter( true )->slice( 0, 1 );
76
		$func = $filter->make( 'product:has', ['catalog'] );
77
		$filter->add( $func, '!=', null )->add( 'product.id', '==', $prodIds );
78
79
		return !$manager->search( $filter )->isEmpty();
80
	}
81
}
82