Completed
Push — master ( f34479...a6f6c0 )
by Aimeos
02:37
created

Support::checkGroup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 24
rs 8.9713
cc 1
eloc 15
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
/**
14
 * Service providing the supporting functionality
15
 *
16
 * @package laravel
17
 * @subpackage Base
18
 */
19
class Support
20
{
21
	/**
22
	 * @var \Aimeos\Shop\Base\Context
23
	 */
24
	private $context;
25
26
27
	/**
28
	 * Initializes the object
29
	 *
30
	 * @param \Aimeos\Shop\Base\Context $context Context object
31
	 */
32
	public function __construct( \Aimeos\Shop\Base\Context $context )
33
	{
34
		$this->context = $context;
35
	}
36
37
38
	/**
39
	 * Checks if the user with the given ID is in the specified group
40
	 *
41
	 * @param string $userid Unique user ID
42
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
43
	 * @return boolean True if user is part of the group, false if not
44
	 */
45
	public function checkGroup( $userid, $groupcodes )
46
	{
47
		$context = $this->context->get();
48
49
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
50
51
		$search = $manager->createSearch();
52
		$search->setConditions( $search->compare( '==', 'customer.group.code', $groupcodes ) );
53
		$groupIds = array_keys( $manager->searchItems( $search ) );
54
55
56
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
57
58
		$search = $manager->createSearch();
59
		$expr = array(
60
			$search->compare( '==', 'customer.lists.parentid', $userid ),
61
			$search->compare( '==', 'customer.lists.refid', $groupIds ),
62
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
63
		);
64
		$search->setConditions( $search->combine( '&&', $expr ) );
65
		$search->setSlice( 0, 1 );
66
67
		return (bool) count( $manager->searchItems( $search ) );
68
	}
69
}