Passed
Push — master ( 6c6334...d4aa98 )
by Aimeos
06:24
created

AccountCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 73
dl 0
loc 216
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroupItem() 0 18 2
A addListItem() 0 24 2
A getOptions() 0 8 1
A handle() 0 18 3
A getArguments() 0 5 1
A createCustomerItem() 0 19 2
A addGroups() 0 16 5
A addGroup() 0 7 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
17
/**
18
 * Creates new accounts or resets their passwords
19
 * @package laravel
20
 * @subpackage Command
21
 */
22
class AccountCommand extends AbstractCommand
23
{
24
	/**
25
	 * The name and signature of the console command.
26
	 *
27
	 * @var string
28
	 */
29
	protected $signature = 'aimeos:account
30
		{email? : E-Mail adress of the (admin) user (will ask for if not given)}
31
		{site=default : Site to create account for}
32
		{--password= : Secret password for the account (will ask for if not given)}
33
		{--super : If account should have super user privileges for all sites}
34
		{--admin : If account should have site administrator privileges}
35
		{--editor : If account should have limited editor privileges}
36
		{--api : If account should be able to access the APIs}
37
	';
38
39
	/**
40
	 * The console command description.
41
	 *
42
	 * @var string
43
	 */
44
	protected $description = 'Creates new (admin) accounts';
45
46
47
	/**
48
	 * Execute the console command.
49
	 *
50
	 * @return mixed
51
	 */
52
	public function handle()
53
	{
54
		if( ( $code = $this->argument( 'email' ) ) === null ) {
55
			$code = $this->ask( 'E-Mail' );
56
		}
57
58
		if( ( $password = $this->option( 'password' ) ) === null ) {
0 ignored issues
show
introduced by
The condition $password = $this->option('password') === null is always false.
Loading history...
59
			$password = $this->secret( 'Password' );
60
		}
61
62
		$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' );
63
		$context->setEditor( 'aimeos:account' );
64
65
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
66
		$localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false );
0 ignored issues
show
Bug introduced by
The method bootstrap() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Manager\Iface such as Aimeos\MShop\Locale\Manager\Iface or Aimeos\MShop\Common\Manager\Decorator\Base or Aimeos\MShop\Service\Manager\Lists\Type\Standard or Aimeos\MShop\Price\Manager\Standard or Aimeos\MShop\Attribute\Manager\Type\Standard or Aimeos\MShop\Price\Manager\Lists\Type\Standard or Aimeos\MShop\Media\Manager\Type\Standard or Aimeos\MShop\Coupon\Manager\Code\Standard or Aimeos\MShop\Order\Manager\Base\Coupon\Standard or Aimeos\MShop\Index\Manager\DBBase or Aimeos\MShop\Product\Manager\Standard or Aimeos\MShop\Index\Manager\Standard or Aimeos\MShop\Index\Manager\Attribute\Standard or Aimeos\MShop\Index\Manager\Text\Standard or Aimeos\MShop\Index\Manager\Supplier\Standard or Aimeos\MShop\Index\Manager\Catalog\Standard or Aimeos\MShop\Index\Manager\Price\Standard or Aimeos\MShop\Supplier\Manager\Standard or Aimeos\MShop\Customer\Manager\Property\Standard or Aimeos\MShop\Order\Manager\Base\Service\Standard or Aimeos\MShop\Order\Manager\Base\Standard or Aimeos\MShop\Price\Manager\Lists\Standard or Aimeos\MShop\Supplier\Manager\Lists\Type\Standard or Aimeos\MShop\Order\Manag...vice\Attribute\Standard or Aimeos\MShop\Service\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Standard or Aimeos\MShop\Price\Manager\Type\Standard or Aimeos\MShop\Locale\Manager\Currency\Standard or Aimeos\MShop\Order\Manag...duct\Attribute\Standard or Aimeos\MShop\Media\Manager\Lists\Type\Standard or Aimeos\MShop\Catalog\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Standard or Aimeos\MShop\Coupon\Manager\Standard or Aimeos\MShop\Attribute\Manager\Standard or Aimeos\MShop\Attribute\M...\Property\Type\Standard or Aimeos\MShop\Service\Manager\Type\Standard or Aimeos\MShop\Product\Manager\Lists\Standard or Aimeos\MShop\Customer\Ma...\Property\Type\Standard or Aimeos\MShop\Order\Manager\Standard or Aimeos\MShop\Customer\Manager\Standard or Aimeos\MShop\Media\Manager\Standard or Aimeos\MShop\Customer\Manager\Lists\Type\Standard or Aimeos\MShop\Attribute\Manager\Lists\Standard or Aimeos\MShop\Product\Man...\Property\Type\Standard or Aimeos\MShop\Media\Manager\Lists\Standard or Aimeos\MShop\Plugin\Manager\Standard or Aimeos\MShop\Order\Manager\Base\Address\Standard or Aimeos\MShop\Catalog\Manager\Standard or Aimeos\MShop\Locale\Manager\Site\Standard or Aimeos\MShop\Product\Manager\Type\Standard or Aimeos\MShop\Supplier\Manager\Lists\Standard or Aimeos\MShop\Stock\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Standard or Aimeos\MAdmin\Job\Manager\Standard or Aimeos\MShop\Customer\Manager\Group\Standard or Aimeos\MShop\Product\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Type\Standard or Aimeos\MShop\Order\Manager\Status\Standard or Aimeos\MShop\Supplier\Manager\Address\Standard or Aimeos\MShop\Customer\Manager\Address\Standard or Aimeos\MShop\Plugin\Manager\Type\Standard or Aimeos\MShop\Stock\Manager\Standard or Aimeos\MShop\Stock\Manager\Nolimit or Aimeos\MShop\Attribute\Manager\Property\Standard or Aimeos\MShop\Subscription\Manager\Standard or Aimeos\MShop\Media\Manager\Property\Type\Standard or Aimeos\MShop\Product\Manager\Property\Standard or Aimeos\MShop\Locale\Manager\Language\Standard or Aimeos\MShop\Media\Manager\Property\Standard or Aimeos\MShop\Service\Manager\Standard or Aimeos\MShop\Attribute\Manager\Lists\Type\Standard or Aimeos\MAdmin\Log\Manager\Standard or Aimeos\MAdmin\Cache\Manager\Standard or Aimeos\MAdmin\Cache\Manager\None or Aimeos\MShop\Order\Manager\Base\Product\Standard or Aimeos\MShop\Customer\Manager\Lists\Standard or Aimeos\MShop\Catalog\Manager\Lists\Type\Standard. ( Ignorable by Annotation )

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

66
		/** @scrutinizer ignore-call */ 
67
  $localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false );
Loading history...
67
		$context->setLocale( $localeItem );
68
69
		$this->addGroups( $context, $this->createCustomerItem( $context, $code, $password ) );
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type string[]; however, parameter $email of Aimeos\Shop\Command\Acco...d::createCustomerItem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

69
		$this->addGroups( $context, $this->createCustomerItem( $context, /** @scrutinizer ignore-type */ $code, $password ) );
Loading history...
70
	}
71
72
73
	/**
74
	 * Associates the user to the group by their given IDs
75
	 *
76
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
77
	 * @param string $userid Unique user ID
78
	 * @param string $groupid Unique group ID
79
	 */
80
	protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid )
81
	{
82
		$manager = \Aimeos\MShop::create( $context, 'customer/lists' );
83
84
		$search = $manager->createSearch();
85
		$expr = array(
86
			$search->compare( '==', 'customer.lists.parentid', $userid ),
87
			$search->compare( '==', 'customer.lists.refid', $groupid ),
88
			$search->compare( '==', 'customer.lists.type', 'default' ),
89
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
90
		);
91
		$search->setConditions( $search->combine( '&&', $expr ) );
92
		$search->setSlice( 0, 1 );
93
94
		if( count( $manager->searchItems( $search ) ) === 0 )
95
		{
96
			$item = $manager->createItem();
97
			$item->setDomain( 'customer/group' );
98
			$item->setParentId( $userid );
0 ignored issues
show
Bug introduced by
The method setParentId() 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

98
			$item->/** @scrutinizer ignore-call */ 
99
          setParentId( $userid );

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...
99
			$item->setRefId( $groupid );
0 ignored issues
show
Bug introduced by
The method setRefId() does not exist on Aimeos\MShop\Attribute\Item\Iface. Did you maybe mean setId()? ( Ignorable by Annotation )

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

99
			$item->/** @scrutinizer ignore-call */ 
100
          setRefId( $groupid );

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...
100
			$item->setType( 'default' );
101
			$item->setStatus( 1 );
102
103
			$manager->saveItem( $item, false );
104
		}
105
	}
106
107
108
	/**
109
	 * Adds the group to the given user
110
	 *
111
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
112
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
113
	 */
114
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user )
115
	{
116
		\Illuminate\Foundation\Auth\User::findOrFail( $user->getId() )
117
			->forceFill( ['superuser' => ( $this->option( 'super' ) ? 1 : 0 )] )
118
			->save();
119
120
		if( $this->option( 'admin' ) ) {
121
			$this->addGroup( $context, $user, 'admin' );
122
		}
123
124
		if( $this->option( 'editor' ) ) {
125
			$this->addGroup( $context, $user, 'editor' );
126
		}
127
128
		if( $this->option( 'api' ) ) {
129
			$this->addGroup( $context, $user, 'api' );
130
		}
131
	}
132
133
134
	/**
135
	 * Adds the group to the given user
136
	 *
137
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
138
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
139
	 * @param string $group Unique customer group code
140
	 */
141
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group )
142
	{
143
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
144
		$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) );
0 ignored issues
show
Bug introduced by
It seems like $this->argument('site') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

144
		$this->info( sprintf( $msg, $group, $user->getCode(), /** @scrutinizer ignore-type */ $this->argument( 'site' ) ) );
Loading history...
145
146
		$groupItem = $this->getGroupItem( $context, $group );
147
		$this->addListItem( $context, $user->getId(), $groupItem->getId() );
148
	}
149
150
151
	/**
152
	 * Returns the customer item for the given e-mail and set its password
153
	 *
154
	 * If the customer doesn't exist yet, it will be created.
155
	 *
156
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
157
	 * @param string $email Unique e-mail address
158
	 * @param string $password New user password
159
	 * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object
160
	 */
161
	protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password )
162
	{
163
		$manager = \Aimeos\MShop::create( $context, 'customer' );
164
165
		try {
166
			$item = $manager->findItem( $email );
0 ignored issues
show
Bug introduced by
The method findItem() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Base\Coupon\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Order\Manager\Base\Service\Iface or Aimeos\MShop\Order\Manager\Base\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Order\Manag...Service\Attribute\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Order\Manag...Product\Attribute\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Address\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Product\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

166
			/** @scrutinizer ignore-call */ 
167
   $item = $manager->findItem( $email );
Loading history...
167
		} catch( \Aimeos\MShop\Exception $e ) {
168
			$item = $manager->createItem();
169
		}
170
171
		$item->setCode( $email );
172
		$item->setLabel( $email );
173
		$item->getPaymentAddress()->setEmail( $email );
174
		$item->setPassword( $password );
175
		$item->setStatus( 1 );
176
177
		$manager->saveItem( $item );
178
179
		return $item;
180
	}
181
182
183
	/**
184
	 * Get the console command arguments.
185
	 *
186
	 * @return array
187
	 */
188
	protected function getArguments()
189
	{
190
		return array(
191
			array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ),
192
			array( 'site', InputArgument::OPTIONAL, 'Site code to create the account for', 'default' ),
193
		);
194
	}
195
196
197
	/**
198
	 * Returns the customer group item for the given code
199
	 *
200
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
201
	 * @param string $code Unique customer group code
202
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
203
	 */
204
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
205
	{
206
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
207
208
		try
209
		{
210
			$item = $manager->findItem( $code );
211
		}
212
		catch( \Aimeos\MShop\Exception $e )
213
		{
214
			$item = $manager->createItem();
215
			$item->setLabel( $code );
216
			$item->setCode( $code );
217
218
			$manager->saveItem( $item );
219
		}
220
221
		return $item;
222
	}
223
224
225
	/**
226
	 * Get the console command options.
227
	 *
228
	 * @return array
229
	 */
230
	protected function getOptions()
231
	{
232
		return array(
233
			array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ),
234
			array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ),
235
			array( 'api', null, InputOption::VALUE_NONE, 'If account should be able to access the APIs' ),
236
			array( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ),
237
			array( 'viewer', null, InputOption::VALUE_NONE, 'If account should only have view privileges' ),
238
		);
239
	}
240
}
241