Issues (76)

src/Aimeos/Shop/Command/AccountCommand.php (1 issue)

php_analyzer.type_inference.infeasible_condition.generic

Unknown
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( ( $email = $this->argument( 'email' ) ) === null ) {
55
			$email = $this->ask( 'E-Mail' );
56
		}
57
58
		if( ( $password = $this->option( 'password' ) ) === null ) {
0 ignored issues
show
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.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, null, true );
67
		$context->setLocale( $localeItem );
68
69
		$manager = \Aimeos\MShop::create( $context, 'customer' );
70
71
		try {
72
			$item = $manager->find( $email );
73
		} catch( \Aimeos\MShop\Exception $e ) {
74
			$item = $manager->create();
75
		}
76
77
		$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 );
78
		$item->getPaymentAddress()->setEmail( $email );
79
80
		$item = $manager->saveItem( $this->addGroups( $context, $item ) );
81
82
		\Illuminate\Foundation\Auth\User::findOrFail( $item->getId() )
83
			->forceFill( [
84
				'siteid' => $this->option( 'super' ) ? '' : $item->getSiteId(),
85
				'superuser' => ( $this->option( 'super' ) ? 1 : 0 ),
86
				'email_verified_at' => now(),
87
			] )->save();
88
	}
89
90
91
	/**
92
	 * Adds the group to the given user
93
	 *
94
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
95
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
96
	 * @return \Aimeos\MShop\Customer\Item\Iface Updated customer object
97
	 */
98
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context,
99
		\Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface
100
	{
101
		if( $this->option( 'admin' ) ) {
102
			$user = $this->addGroup( $context, $user, 'admin' );
103
		}
104
105
		if( $this->option( 'editor' ) ) {
106
			$user = $this->addGroup( $context, $user, 'editor' );
107
		}
108
109
		if( $this->option( 'api' ) ) {
110
			$user = $this->addGroup( $context, $user, 'api' );
111
		}
112
113
		return $user;
114
	}
115
116
117
	/**
118
	 * Adds the group to the given user
119
	 *
120
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
121
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
122
	 * @param string $group Unique customer group code
123
	 */
124
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user,
125
		string $group ) : \Aimeos\MShop\Customer\Item\Iface
126
	{
127
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
128
		$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) );
129
130
		$groupId = $this->getGroupItem( $context, $group )->getId();
131
		return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) );
132
	}
133
134
135
	/**
136
	 * Returns the customer group item for the given code
137
	 *
138
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
139
	 * @param string $code Unique customer group code
140
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
141
	 */
142
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, string $code ) : \Aimeos\MShop\Customer\Item\Group\Iface
143
	{
144
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
145
146
		try
147
		{
148
			$item = $manager->find( $code );
149
		}
150
		catch( \Aimeos\MShop\Exception $e )
151
		{
152
			$item = $manager->create();
153
			$item->setLabel( $code );
154
			$item->setCode( $code );
155
156
			$manager->saveItem( $item );
157
		}
158
159
		return $item;
160
	}
161
}
162