Issues (68)

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\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 );
67
		$context->setLocale( $localeItem );
68
69
		$manager = \Aimeos\MShop::create( $context, 'customer' );
70
71
		try {
72
			$item = $manager->findItem( $email );
73
		} catch( \Aimeos\MShop\Exception $e ) {
74
			$item = $manager->createItem();
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( ['superuser' => ( $this->option( 'super' ) ? 1 : 0 )] )
84
			->save();
85
	}
86
87
88
	/**
89
	 * Adds the group to the given user
90
	 *
91
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
92
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
93
	 */
94
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user )
95
	{
96
		if( $this->option( 'admin' ) ) {
97
			$user = $this->addGroup( $context, $user, 'admin' );
98
		}
99
100
		if( $this->option( 'editor' ) ) {
101
			$user = $this->addGroup( $context, $user, 'editor' );
102
		}
103
104
		if( $this->option( 'api' ) ) {
105
			$user = $this->addGroup( $context, $user, 'api' );
106
		}
107
108
		return $user;
109
	}
110
111
112
	/**
113
	 * Adds the group to the given user
114
	 *
115
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
116
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
117
	 * @param string $group Unique customer group code
118
	 */
119
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group )
120
	{
121
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
122
		$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) );
123
124
		$groupId = $this->getGroupItem( $context, $group )->getId();
125
		return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) );
126
	}
127
128
129
	/**
130
	 * Returns the customer group item for the given code
131
	 *
132
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
133
	 * @param string $code Unique customer group code
134
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
135
	 */
136
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
137
	{
138
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
139
140
		try
141
		{
142
			$item = $manager->findItem( $code );
143
		}
144
		catch( \Aimeos\MShop\Exception $e )
145
		{
146
			$item = $manager->createItem();
147
			$item->setLabel( $code );
148
			$item->setCode( $code );
149
150
			$manager->saveItem( $item );
151
		}
152
153
		return $item;
154
	}
155
}
156