Completed
Push — master ( 1eeaa3...38eb28 )
by Aimeos
02:31
created

AccountCommand::addListItem()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 2
eloc 19
nc 2
nop 3
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015
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 console command name.
22
	 *
23
	 * @var string
24
	 */
25
	protected $name = 'aimeos:account';
26
27
	/**
28
	 * The console command description.
29
	 *
30
	 * @var string
31
	 */
32
	protected $description = 'Creates new (admin) accounts';
33
34
35
	/**
36
	 * Execute the console command.
37
	 *
38
	 * @return mixed
39
	 */
40
	public function fire()
41
	{
42
		$code = $this->argument( 'email' );
43
		if( ( $password = $this->option( 'password' ) ) === null ) {
44
			$password = $this->secret( 'Password' );
45
		}
46
47
		$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false );
48
		$context->setEditor( 'aimeos:account' );
49
50
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
51
		$context->setLocale( $localeManager->createItem() );
52
53
		$user = $this->createCustomerItem( $context, $code, $password );
0 ignored issues
show
Bug introduced by
It seems like $code defined by $this->argument('email') on line 42 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 43 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...
54
55
		if( $this->option( 'admin' ) ) {
56
			$this->addPrivilege( $context, $user, 'admin' );
57
		}
58
	}
59
60
61
	/**
62
	 * Associates the user to the group by their given IDs
63
	 *
64
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
65
	 * @param string $userid Unique user ID
66
	 * @param string $groupid Unique group ID
67
	 */
68
	protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid )
69
	{
70
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'lists' );
71
		$typeid = $this->getTypeItem( $manager, 'default' )->getId();
72
73
		$search = $manager->createSearch();
74
		$expr = array(
75
			$search->compare( '==', 'customer.lists.parentid', $userid ),
76
			$search->compare( '==', 'customer.lists.refid', $groupid ),
77
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
78
			$search->compare( '==', 'customer.lists.typeid', $typeid ),
79
		);
80
		$search->setConditions( $search->combine( '&&', $expr ) );
81
		$search->setSlice( 0, 1 );
82
83
		if( count( $manager->searchItems( $search ) ) === 0 )
84
		{
85
			$item = $manager->createItem();
86
			$item->setDomain( 'customer/group' );
87
			$item->setParentId( $userid );
88
			$item->setTypeId( $typeid );
89
			$item->setRefId( $groupid );
90
			$item->setStatus( 1 );
91
92
			$manager->saveItem( $item, false );
93
		}
94
	}
95
96
97
	/**
98
	 * Adds the privilege to the given user
99
	 *
100
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
101
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
102
	 * @param string $privilege Unique customer group code
103
	 */
104
	protected function addPrivilege( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $privilege )
105
	{
106
		$this->info( sprintf( 'Add "%1$s" privilege to user "%2$s" for sites', $privilege, $user->getCode() ) );
107
108
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
109
110
		foreach( $this->getSiteItems( $context, $this->argument( 'site' ) ) as $siteItem )
111
		{
112
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
113
114
			$lcontext = clone $context;
115
			$lcontext->setLocale( $localeItem );
116
117
			$this->info( '- ' . $siteItem->getCode() );
118
119
			$groupItem = $this->getGroupItem( $lcontext, $privilege );
120
			$this->addListItem( $lcontext, $user->getId(), $groupItem->getId() );
121
		}
122
	}
123
124
125
	/**
126
	 * Returns the customer item for the given e-mail and set its password
127
	 *
128
	 * If the customer doesn't exist yet, it will be created.
129
	 *
130
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
131
	 * @param string $email Unique e-mail address
132
	 * @param string $password New user password
133
	 * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object
134
	 */
135
	protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password )
136
	{
137
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
138
139
		try {
140
			$item = $manager->getItem( $email );
141
		} catch( \Aimeos\MShop\Exception $e ) {
142
			$item = $manager->createItem();
143
		}
144
145
		$item->setCode( $email );
146
		$item->getPaymentAddress()->setEmail( $email );
147
		$item->setPassword( $password );
148
149
		$manager->saveItem( $item );
150
151
		return $item;
152
	}
153
154
155
	/**
156
	 * Get the console command arguments.
157
	 *
158
	 * @return array
159
	 */
160
	protected function getArguments()
161
	{
162
		return array(
163
			array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ),
164
			array( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default unittest" (none for all)' ),
165
		);
166
	}
167
168
169
	/**
170
	 * Returns the customer group item for the given code
171
	 *
172
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
173
	 * @param string $code Unique customer group code
174
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
175
	 */
176
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
177
	{
178
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' );
179
180
		try
181
		{
182
			$item = $manager->getItem( $code );
183
		}
184
		catch( \Aimeos\MShop\Exception $e )
185
		{
186
			$item = $manager->createItem();
187
			$item->setLabel( $code );
188
			$item->setCode( $code );
189
190
			$manager->saveItem( $item );
191
		}
192
193
		return $item;
194
	}
195
196
197
	/**
198
	 * Get the console command options.
199
	 *
200
	 * @return array
201
	 */
202
	protected function getOptions()
203
	{
204
		return array(
205
				array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ),
206
				array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ),
207
		);
208
	}
209
210
211
	/**
212
	 * Returns the customer group type item for the given code
213
	 *
214
	 * @param \Aimeos\MShop\Common\Manager\Iface $listManager Aimeos customer list manager object
215
	 * @param string $code Unique code for a customer list type
216
	 * @return \Aimeos\MShop\Common\Item\Type\Iface Aimeos customer list type item object
217
	 * @throws \Exception If no customer list type item for the given code is found
218
	 */
219
	protected function getTypeItem( \Aimeos\MShop\Common\Manager\Iface $listManager, $code )
220
	{
221
		$manager = $listManager->getSubmanager( 'type' );
222
223
		$search = $manager->createSearch();
224
		$expr = array(
225
			$search->compare( '==', 'customer.lists.type.domain', 'customer/group' ),
226
			$search->compare( '==', 'customer.lists.type.code', $code ),
227
		);
228
		$search->setConditions( $search->combine( '&&', $expr ) );
229
230
		$result = $manager->searchItems( $search );
231
232
		if( ( $item = reset( $result ) ) === false )
233
		{
234
			$msg = sprintf( 'No user list type item for domain "%1$s" and code "%2$s" found', 'customer/group', $code );
235
			throw new \Exception( $msg );
236
		}
237
238
		return $item;
239
	}
240
}
241