AccountCommand::handle()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 8
nop 0
dl 0
loc 38
rs 8.6026
c 0
b 0
f 0
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
	';
33
34
	/**
35
	 * The console command description.
36
	 *
37
	 * @var string
38
	 */
39
	protected $description = 'Creates new (admin) accounts';
40
41
42
	/**
43
	 * Execute the console command.
44
	 *
45
	 * @return mixed
46
	 */
47
	public function handle()
48
	{
49
		$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

49
		$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

49
		$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...
50
51
		if( ( $email = $this->argument( 'email' ) ) === null ) {
52
			$email = $this->ask( 'E-Mail' );
53
		}
54
55
		if( ( $password = $this->option( 'password' ) ) === null ) {
0 ignored issues
show
introduced by
The condition $password = $this->option('password') === null is always false.
Loading history...
56
			$password = $this->secret( 'Password' );
57
		}
58
59
		$context = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' );
60
		$context->setEditor( 'aimeos:account' );
61
62
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
63
		$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\Price\Manager\Iface or Aimeos\MShop\Type\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Coupon\Manager\Code\Iface or Aimeos\MShop\Product\Manager\Iface or Aimeos\MShop\Supplier\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Currency\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\Order\Manager\Address\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Customer\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\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\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\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\Basket\Manager\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\Service\Manager\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\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Lists\Type\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\Price\Manager\Property\Type\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Type\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\Plugin\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Customer\Manager\Lists\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

63
		/** @scrutinizer ignore-call */ 
64
  $localeItem = $localeManager->bootstrap( $site, '', '', false, null, true );
Loading history...
64
		$context->setLocale( $localeItem );
65
66
		$manager = \Aimeos\MShop::create( $context, 'customer' );
67
68
		try {
69
			$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\Price\Manager\Iface or Aimeos\MShop\Type\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\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\Basket\Manager\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\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\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Lists\Type\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\Price\Manager\Property\Type\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Type\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\Plugin\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Customer\Manager\Lists\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

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

121
		$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

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