AccountCommand::addGroups()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 16
rs 10
cc 4
nc 8
nop 2
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 */
7
8
9
namespace Aimeos\Shop\Command;
10
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Input\InputArgument;
13
14
15
/**
16
 * Creates new accounts or resets their passwords
17
 */
18
class AccountCommand extends AbstractCommand
19
{
20
	/**
21
	 * The name and signature of the console command.
22
	 *
23
	 * @var string
24
	 */
25
	protected $signature = 'aimeos:account
26
		{email? : E-Mail adress of the (admin) user (will ask for if not given)}
27
		{site? : Site to create account for (will use default value if not given}
28
		{--password= : Secret password for the account (will ask for if not given)}
29
		{--super : If account should have super user privileges for all sites}
30
		{--admin : If account should have site administrator privileges}
31
		{--editor : If account should have limited editor privileges}
32
		{--api : If account should be able to access the APIs}
33
	';
34
35
	/**
36
	 * The console command description.
37
	 *
38
	 * @var string
39
	 */
40
	protected $description = 'Creates new (admin) accounts';
41
42
43
	/**
44
	 * Execute the console command.
45
	 *
46
	 * @return mixed
47
	 */
48
	public function handle()
49
	{
50
		$site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' );
0 ignored issues
show
Bug introduced by
'shop.mshop.locale.site' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

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

50
		$site = $this->argument( 'site' ) ?: config( /** @scrutinizer ignore-type */ 'shop.mshop.locale.site', 'default' );
Loading history...
Unused Code introduced by
The call to config() has too many arguments starting with 'default'. ( Ignorable by Annotation )

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

50
		$site = $this->argument( 'site' ) ?: /** @scrutinizer ignore-call */ config( 'shop.mshop.locale.site', 'default' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51
52
		if( ( $email = $this->argument( 'email' ) ) === null ) {
53
			$email = $this->ask( 'E-Mail' );
54
		}
55
56
		if( ( $password = $this->option( 'password' ) ) === null ) {
0 ignored issues
show
introduced by
The condition $password = $this->option('password') === null is always false.
Loading history...
57
			$password = $this->secret( 'Password' );
58
		}
59
60
		$context = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' );
61
		$context->setEditor( 'aimeos:account' );
62
63
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
64
		$localeItem = $localeManager->bootstrap( $site, '', '', false, null, true );
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 said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Coupon\Manager\Code\Iface or Aimeos\MShop\Product\Manager\Iface or Aimeos\MShop\Supplier\Manager\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\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\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\Attribute\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Customer\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Group\Manager\Iface or Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Stock\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MShop\Common\Manager\Type\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\Cms\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Media\Manager\Property\Type\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\Service\Manager\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Cms\Manager\Lists\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Index\Manager\Iface or Aimeos\MShop\Index\Manager\Attribute\Iface or Aimeos\MShop\Index\Manager\Text\Iface or Aimeos\MShop\Index\Manager\Supplier\Iface or Aimeos\MShop\Index\Manager\Catalog\Iface or Aimeos\MShop\Index\Manager\Price\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

64
		/** @scrutinizer ignore-call */ 
65
  $localeItem = $localeManager->bootstrap( $site, '', '', false, null, true );
Loading history...
65
		$context->setLocale( $localeItem );
66
67
		$manager = \Aimeos\MShop::create( $context, 'customer' );
68
69
		try {
70
			$item = $manager->find( $email );
0 ignored issues
show
Bug introduced by
The method find() 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\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\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\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\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Coupon\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\Service\Attribute\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\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\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\Cms\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\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

70
			/** @scrutinizer ignore-call */ 
71
   $item = $manager->find( $email );
Loading history...
71
		} catch( \Aimeos\MShop\Exception $e ) {
72
			$item = $manager->create();
73
		}
74
75
		$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 );
76
		$item->getPaymentAddress()->setEmail( $email );
77
78
		$item = $manager->save( $this->addGroups( $context, $item ) );
79
80
		\Illuminate\Foundation\Auth\User::findOrFail( $item->getId() )
81
			->forceFill( [
82
				'siteid' => $this->option( 'super' ) ? '' : $item->getSiteId(),
83
				'superuser' => ( $this->option( 'super' ) ? 1 : 0 ),
84
				'email_verified_at' => now(),
85
			] )->save();
86
	}
87
88
89
	/**
90
	 * Adds the group to the given user
91
	 *
92
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
93
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
94
	 * @return \Aimeos\MShop\Customer\Item\Iface Updated customer object
95
	 */
96
	protected function addGroups( \Aimeos\MShop\ContextIface $context,
97
		\Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface
98
	{
99
		if( $this->option( 'admin' ) ) {
100
			$user = $this->addGroup( $context, $user, 'admin' );
101
		}
102
103
		if( $this->option( 'editor' ) ) {
104
			$user = $this->addGroup( $context, $user, 'editor' );
105
		}
106
107
		if( $this->option( 'api' ) ) {
108
			$user = $this->addGroup( $context, $user, 'api' );
109
		}
110
111
		return $user;
112
	}
113
114
115
	/**
116
	 * Adds the group to the given user
117
	 *
118
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
119
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
120
	 * @param string $group Unique customer group code
121
	 */
122
	protected function addGroup( \Aimeos\MShop\ContextIface $context, \Aimeos\MShop\Customer\Item\Iface $user,
123
		string $group ) : \Aimeos\MShop\Customer\Item\Iface
124
	{
125
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
126
		$site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' );
0 ignored issues
show
Bug introduced by
'shop.mshop.locale.site' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

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

126
		$site = $this->argument( 'site' ) ?: config( /** @scrutinizer ignore-type */ 'shop.mshop.locale.site', 'default' );
Loading history...
Unused Code introduced by
The call to config() has too many arguments starting with 'default'. ( Ignorable by Annotation )

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

126
		$site = $this->argument( 'site' ) ?: /** @scrutinizer ignore-call */ config( 'shop.mshop.locale.site', 'default' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
127
		$this->info( sprintf( $msg, $group, $user->getCode(), $site ) );
128
129
		$item = $this->getGroupItem( $context, $group );
130
		return $user->setGroups( $user->getGroups() + [$item->getId() => $item->getCode()] );
131
	}
132
133
134
	/**
135
	 * Returns the customer group item for the given code
136
	 *
137
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
138
	 * @param string $code Unique customer group code
139
	 * @return \Aimeos\MShop\Group\Item\Iface Aimeos customer group item object
140
	 */
141
	protected function getGroupItem( \Aimeos\MShop\ContextIface $context, string $code ) : \Aimeos\MShop\Group\Item\Iface
142
	{
143
		$manager = \Aimeos\MShop::create( $context, 'group' );
144
145
		try
146
		{
147
			$item = $manager->find( $code );
148
		}
149
		catch( \Aimeos\MShop\Exception $e )
150
		{
151
			$item = $manager->create();
152
			$item->setLabel( $code );
153
			$item->setCode( $code );
154
155
			$manager->save( $item );
156
		}
157
158
		return $item;
159
	}
160
}
161