Completed
Push — master ( fbd02f...e63e7d )
by Aimeos
02:56
created

Support   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 21.35 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 5
c 5
b 1
f 2
lcom 1
cbo 5
dl 19
loc 89
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkGroup() 0 16 1
A getGroups() 0 9 1
A setLocale() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\MShop\Context\Item\Iface
23
	 */
24
	private $context;
25
26
27
	/**
28
	 * Initializes the object
29
	 *
30
	 * @param \Aimeos\Shop\Base\Context $context Context provider
31
	 */
32
	public function __construct( \Aimeos\Shop\Base\Context $context, $site )
33
	{
34
		$this->context = $context->get( false );
35
		$this->context = $this->setLocale( $this->context, $site );
36
	}
37
38
39
	/**
40
	 * Checks if the user with the given ID is in the specified group
41
	 *
42
	 * @param string $userid Unique user ID
43
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
44
	 * @return boolean True if user is part of the group, false if not
45
	 */
46
	public function checkGroup( $userid, $groupcodes )
47
	{
48
		$groupItems = $this->getGroups( (array) $groupcodes );
49
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/lists' );
50
51
		$search = $manager->createSearch();
52
		$expr = array(
53
			$search->compare( '==', 'customer.lists.parentid', $userid ),
54
			$search->compare( '==', 'customer.lists.refid', array_keys( $groupItems ) ),
55
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
56
		);
57
		$search->setConditions( $search->combine( '&&', $expr ) );
58
		$search->setSlice( 0, 1 );
59
60
		return (bool) count( $manager->searchItems( $search ) );
61
	}
62
63
64
	/**
65
	 * Returns the groups items for the given codes
66
	 *
67
	 * @param array $codes List of group codes
68
	 * @return array Associative list of group IDs as keys and \Aimeos\MShop\Customer\Item\Group\Iface as values
69
	 */
70
	protected function getGroups( array $codes )
71
	{
72
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'customer/group' );
73
74
		$search = $manager->createSearch();
75
		$search->setConditions( $search->compare( '==', 'customer.group.code', $codes ) );
76
77
		return $manager->searchItems( $search );
78
	}
79
80
81
	/**
82
	 * Returns the context with the locale item set
83
	 *
84
	 * @param \Aimeos\MShop\Context\Item\Iface Context object
85
	 * @param string Unique site code
86
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
87
	 */
88 View Code Duplication
	protected function setLocale( \Aimeos\MShop\Context\Item\Iface $context, $site )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
	{
90
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
91
92
		try
93
		{
94
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
95
			$localeItem->setLanguageId( null );
96
			$localeItem->setCurrencyId( null );
97
		}
98
		catch( \Aimeos\MShop\Locale\Exception $e )
99
		{
100
			$localeItem = $localeManager->createItem();
101
		}
102
103
		$context->setLocale( $localeItem );
104
105
		return $context;
106
	}
107
}