Completed
Push — master ( a78503...736f57 )
by Aimeos
18:25
created

AccountCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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 console command name.
26
	 *
27
	 * @var string
28
	 */
29
	protected $name = 'aimeos:account';
30
31
	/**
32
	 * The console command description.
33
	 *
34
	 * @var string
35
	 */
36
	protected $description = 'Creates new (admin) accounts';
37
38
39
	/**
40
	 * Execute the console command.
41
	 *
42
	 * @return mixed
43
	 */
44
	public function fire()
45
	{
46
		$code = $this->argument( 'email' );
47
		if( ( $password = $this->option( 'password' ) ) === null ) {
48
			$password = $this->secret( 'Password' );
49
		}
50
51
		$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false );
52
		$context->setEditor( 'aimeos:account' );
53
54
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
55
		$context->setLocale( $localeManager->createItem() );
56
57
		$user = $this->createCustomerItem( $context, $code, $password );
0 ignored issues
show
Bug introduced by
It seems like $code defined by $this->argument('email') on line 46 can also be of type array; however, Aimeos\Shop\Command\Acco...d::createCustomerItem() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $password defined by $this->option('password') on line 47 can also be of type array; however, Aimeos\Shop\Command\Acco...d::createCustomerItem() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
59
		if( $this->option( 'admin' ) ) {
60
			$this->addGroup( $context, $user, 'admin' );
61
		}
62
63
		if( $this->option( 'editor' ) ) {
64
			$this->addGroup( $context, $user, 'editor' );
65
		}
66
	}
67
68
69
	/**
70
	 * Associates the user to the group by their given IDs
71
	 *
72
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
73
	 * @param string $userid Unique user ID
74
	 * @param string $groupid Unique group ID
75
	 */
76
	protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid )
77
	{
78
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'lists' );
79
		$typeid = $manager->getSubmanager( 'type' )->findItem( 'default', array(), 'customer/group' )->getId();
80
81
		$search = $manager->createSearch();
82
		$expr = array(
83
			$search->compare( '==', 'customer.lists.parentid', $userid ),
84
			$search->compare( '==', 'customer.lists.refid', $groupid ),
85
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
86
			$search->compare( '==', 'customer.lists.typeid', $typeid ),
87
		);
88
		$search->setConditions( $search->combine( '&&', $expr ) );
89
		$search->setSlice( 0, 1 );
90
91
		if( count( $manager->searchItems( $search ) ) === 0 )
92
		{
93
			$item = $manager->createItem();
94
			$item->setDomain( 'customer/group' );
95
			$item->setParentId( $userid );
96
			$item->setTypeId( $typeid );
97
			$item->setRefId( $groupid );
98
			$item->setStatus( 1 );
99
100
			$manager->saveItem( $item, false );
101
		}
102
	}
103
104
105
	/**
106
	 * Adds the group to the given user
107
	 *
108
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
109
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
110
	 * @param string $group Unique customer group code
111
	 */
112
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group )
113
	{
114
		$this->info( sprintf( 'Add "%1$s" group to user "%2$s" for sites', $group, $user->getCode() ) );
115
116
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
117
118
		foreach( $this->getSiteItems( $context, $this->argument( 'site' ) ) as $siteItem )
119
		{
120
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
121
122
			$lcontext = clone $context;
123
			$lcontext->setLocale( $localeItem );
124
125
			$this->info( '- ' . $siteItem->getCode() );
126
127
			$groupItem = $this->getGroupItem( $lcontext, $group );
128
			$this->addListItem( $lcontext, $user->getId(), $groupItem->getId() );
129
		}
130
	}
131
132
133
	/**
134
	 * Returns the customer item for the given e-mail and set its password
135
	 *
136
	 * If the customer doesn't exist yet, it will be created.
137
	 *
138
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
139
	 * @param string $email Unique e-mail address
140
	 * @param string $password New user password
141
	 * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object
142
	 */
143 View Code Duplication
	protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
	{
145
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
146
147
		try {
148
			$item = $manager->findItem( $email );
149
		} catch( \Aimeos\MShop\Exception $e ) {
150
			$item = $manager->createItem();
151
		}
152
153
		$item->setCode( $email );
154
		$item->getPaymentAddress()->setEmail( $email );
155
		$item->setPassword( $password );
156
157
		$manager->saveItem( $item );
158
159
		return $item;
160
	}
161
162
163
	/**
164
	 * Get the console command arguments.
165
	 *
166
	 * @return array
167
	 */
168
	protected function getArguments()
169
	{
170
		return array(
171
			array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ),
172
			array( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default unittest" (none for all)' ),
173
		);
174
	}
175
176
177
	/**
178
	 * Returns the customer group item for the given code
179
	 *
180
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
181
	 * @param string $code Unique customer group code
182
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
183
	 */
184 View Code Duplication
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
	{
186
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' );
187
188
		try
189
		{
190
			$item = $manager->findItem( $code );
191
		}
192
		catch( \Aimeos\MShop\Exception $e )
193
		{
194
			$item = $manager->createItem();
195
			$item->setLabel( $code );
196
			$item->setCode( $code );
197
198
			$manager->saveItem( $item );
199
		}
200
201
		return $item;
202
	}
203
204
205
	/**
206
	 * Get the console command options.
207
	 *
208
	 * @return array
209
	 */
210
	protected function getOptions()
211
	{
212
		return array(
213
			array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ),
214
			array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ),
215
			array( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ),
216
		);
217
	}
218
}
219