Completed
Push — master ( 6f8588...d5f400 )
by Aimeos
19:59
created

AccountCommand::getGroupItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
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 handle()
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, 'command' );
52
		$context->setEditor( 'aimeos:account' );
53
54
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
55
		$localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false );
56
		$context->setLocale( $localeItem );
57
58
		$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...
59
60
		if( $this->option( 'admin' ) ) {
61
			$this->addGroup( $context, $user, 'admin' );
62
		}
63
64
		if( $this->option( 'api' ) ) {
65
			$this->addGroup( $context, $user, 'api' );
66
		}
67
68
		if( $this->option( 'editor' ) ) {
69
			$this->addGroup( $context, $user, 'editor' );
70
		}
71
72
		if( $this->option( 'viewer' ) ) {
73
			$this->addGroup( $context, $user, 'viewer' );
74
		}
75
	}
76
77
78
	/**
79
	 * Associates the user to the group by their given IDs
80
	 *
81
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
82
	 * @param string $userid Unique user ID
83
	 * @param string $groupid Unique group ID
84
	 */
85
	protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid )
86
	{
87
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
88
		$typeManager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists/type' );
89
90
		$typeid = $typeManager->findItem( 'default', array(), 'customer/group' )->getId();
91
92
		$search = $manager->createSearch();
93
		$expr = array(
94
			$search->compare( '==', 'customer.lists.parentid', $userid ),
95
			$search->compare( '==', 'customer.lists.refid', $groupid ),
96
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
97
			$search->compare( '==', 'customer.lists.typeid', $typeid ),
98
		);
99
		$search->setConditions( $search->combine( '&&', $expr ) );
100
		$search->setSlice( 0, 1 );
101
102
		if( count( $manager->searchItems( $search ) ) === 0 )
103
		{
104
			$item = $manager->createItem();
105
			$item->setDomain( 'customer/group' );
106
			$item->setParentId( $userid );
107
			$item->setTypeId( $typeid );
108
			$item->setRefId( $groupid );
109
			$item->setStatus( 1 );
110
111
			$manager->saveItem( $item, false );
112
		}
113
	}
114
115
116
	/**
117
	 * Adds the group to the given user
118
	 *
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
120
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
121
	 * @param string $group Unique customer group code
122
	 */
123
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group )
124
	{
125
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
126
		$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) );
127
128
		$groupItem = $this->getGroupItem( $context, $group );
129
		$this->addListItem( $context, $user->getId(), $groupItem->getId() );
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
	protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password )
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->setLabel( $email );
155
		$item->getPaymentAddress()->setEmail( $email );
156
		$item->setPassword( $password );
157
		$item->setStatus( 1 );
158
159
		$manager->saveItem( $item );
160
161
		return $item;
162
	}
163
164
165
	/**
166
	 * Get the console command arguments.
167
	 *
168
	 * @return array
169
	 */
170 View Code Duplication
	protected function getArguments()
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...
171
	{
172
		return array(
173
			array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ),
174
			array( 'site', InputArgument::OPTIONAL, 'Site code to create the account for', 'default' ),
175
		);
176
	}
177
178
179
	/**
180
	 * Returns the customer group item for the given code
181
	 *
182
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
183
	 * @param string $code Unique customer group code
184
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
185
	 */
186
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
187
	{
188
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' );
189
190
		try
191
		{
192
			$item = $manager->findItem( $code );
193
		}
194
		catch( \Aimeos\MShop\Exception $e )
195
		{
196
			$item = $manager->createItem();
197
			$item->setLabel( $code );
198
			$item->setCode( $code );
199
200
			$manager->saveItem( $item );
201
		}
202
203
		return $item;
204
	}
205
206
207
	/**
208
	 * Get the console command options.
209
	 *
210
	 * @return array
211
	 */
212
	protected function getOptions()
213
	{
214
		return array(
215
			array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ),
216
			array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ),
217
			array( 'api', null, InputOption::VALUE_NONE, 'If account should be able to access the APIs' ),
218
			array( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ),
219
			array( 'viewer', null, InputOption::VALUE_NONE, 'If account should only have view privileges' ),
220
		);
221
	}
222
}
223