Passed
Push — master ( d4aa98...d407e5 )
by Aimeos
03:02
created

src/Aimeos/Shop/Base/Support.php (1 issue)

calls to methods that only exist in a sub-type.

Bug Major
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
use Illuminate\Support\Facades\Input;
14
use Illuminate\Support\Facades\Route;
15
16
17
/**
18
 * Service providing the supporting functionality
19
 *
20
 * @package laravel
21
 * @subpackage Base
22
 */
23
class Support
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\Context
27
	 */
28
	private $context;
29
30
	/**
31
	 * @var \Aimeos\Shop\Base\Locale
32
	 */
33
	private $locale;
34
35
	/**
36
	 * @var array
37
	 */
38
	private $cache = [];
39
40
41
	/**
42
	 * Initializes the object
43
	 *
44
	 * @param \Aimeos\Shop\Base\Context $context Context provider
45
	 * @param \Aimeos\Shop\Base\Locale $locale Locale provider
46
	 */
47
	public function __construct( \Aimeos\Shop\Base\Context $context, \Aimeos\Shop\Base\Locale $locale )
48
	{
49
		$this->context = $context;
50
		$this->locale = $locale;
51
	}
52
53
54
	/**
55
	 * Checks if the user is in the specified group and associatied to the site
56
	 *
57
	 * @param \Illuminate\Foundation\Auth\User $user Authenticated user
58
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
59
	 * @return boolean True if user is part of the group, false if not
60
	 */
61
	public function checkUserGroup( \Illuminate\Foundation\Auth\User $user, $groupcodes )
62
	{
63
		$groups = ( is_array( $groupcodes ) ? implode( ',', $groupcodes ) : $groupcodes );
64
65
		if( isset( $this->cache[$user->id][$groups] ) ) {
66
			return $this->cache[$user->id][$groups];
67
		}
68
69
		$this->cache[$user->id][$groups] = false;
70
		$context = $this->context->get( false );
71
72
		try {
73
			$site = \Aimeos\MShop::create( $context, 'locale/site' )->getItem( $user->siteid )->getCode();
0 ignored issues
show
The method getCode() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Item\Iface such as Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Base\Service\Iface or Aimeos\MShop\Customer\Item\Group\Iface or Aimeos\MShop\Order\Item\Base\Coupon\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Order\Item\...Service\Attribute\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Order\Item\...Product\Attribute\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Attribute\Item\Standard or Aimeos\MShop\Catalog\Item\Standard or Aimeos\MShop\Customer\Item\Base or Aimeos\MShop\Customer\Item\Group\Standard or Aimeos\MShop\Common\Item\Type\Standard or Aimeos\MShop\Locale\Item\Site\Standard or Aimeos\MShop\Locale\Item\Currency\Standard or Aimeos\MShop\Locale\Item\Language\Standard or Aimeos\MShop\Order\Item\Base\Service\Base or Aimeos\MShop\Service\Item\Standard or Aimeos\MShop\Order\Item\Base\Coupon\Standard or Aimeos\MShop\Coupon\Item\Code\Standard or Aimeos\MShop\Order\Item\...vice\Attribute\Standard or Aimeos\MShop\Supplier\Item\Standard or Aimeos\MShop\Product\Item\Standard or Aimeos\MShop\Order\Item\...duct\Attribute\Standard or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Common\Item\Tree\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\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

73
			$site = \Aimeos\MShop::create( $context, 'locale/site' )->getItem( $user->siteid )->/** @scrutinizer ignore-call */ getCode();
Loading history...
74
		} catch( \Exception $e ) {
75
			$site = ( Route::current() ? Route::input( 'site', Input::get( 'site', 'default' ) ) : 'default' );
76
		}
77
78
		$context->setLocale( $this->locale->getBackend( $context, $site ) );
79
80
		foreach( array_reverse( $context->getLocale()->getSitePath() ) as $siteid )
81
		{
82
			if( (string) $user->siteid === (string) $siteid ) {
83
				$this->cache[$user->id][$groups] = $this->checkGroups( $context, $user->id, $groupcodes );
84
			}
85
		}
86
87
		return $this->cache[$user->id][$groups];
88
	}
89
90
91
	/**
92
	 * Checks if the user with the given ID is in the specified group
93
	 *
94
	 * @param string $userid Unique user ID
95
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
96
	 * @return boolean True if user is part of the group, false if not
97
	 * @deprecated Use checkUserGroup() instead
98
	 */
99
	public function checkGroup( $userid, $groupcodes )
100
	{
101
		$groups = ( is_array( $groupcodes ) ? implode( ',', $groupcodes ) : $groupcodes );
102
103
		if( isset( $this->cache[$userid][$groups] ) ) {
104
			return $this->cache[$userid][$groups];
105
		}
106
107
		$site = ( Route::current() ? Route::input( 'site', Input::get( 'site', 'default' ) ) : 'default' );
108
109
		$context = $this->context->get( false );
110
		$context->setLocale( $this->locale->getBackend( $context, $site ) );
111
112
		return $this->cache[$userid][$groups] = $this->checkGroups( $context, $userid, $groupcodes );
113
	}
114
115
116
	/**
117
	 * Returns the available group codes
118
	 *
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item
120
	 * @return string[] List of group codes
121
	 */
122
	public function getGroups( \Aimeos\MShop\Context\Item\Iface $context )
123
	{
124
		$list = array();
125
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
126
127
		$search = $manager->createSearch();
128
		$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
129
130
		foreach( $manager->searchItems( $search ) as $item ) {
131
			$list[] = $item->getCode();
132
		}
133
134
		return $list;
135
	}
136
137
138
	/**
139
	 * Checks if one of the groups is associated to the given user ID
140
	 *
141
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item
142
	 * @param string $userid ID of the logged in user
143
	 * @param string[] $groupcodes List of group codes to check against
144
	 * @return boolean True if the user is in one of the groups, false if not
145
	 */
146
	protected function checkGroups( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupcodes )
147
	{
148
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
149
150
		$search = $manager->createSearch();
151
		$search->setConditions( $search->compare( '==', 'customer.group.code', (array) $groupcodes ) );
152
		$groupItems = $manager->searchItems( $search );
153
154
155
		$manager = \Aimeos\MShop::create( $context, 'customer/lists' );
156
157
		$search = $manager->createSearch();
158
		$expr = array(
159
			$search->compare( '==', 'customer.lists.parentid', $userid ),
160
			$search->compare( '==', 'customer.lists.refid', array_keys( $groupItems ) ),
161
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
162
		);
163
		$search->setConditions( $search->combine( '&&', $expr ) );
164
		$search->setSlice( 0, 1 );
165
166
		return (bool) count( $manager->searchItems( $search ) );
167
	}
168
}
169