Completed
Push — master ( dcdc51...16ea71 )
by Aimeos
03:11
created

AccountCommand::execute()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.5806
cc 4
eloc 16
nc 8
nop 2
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package symfony
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\ShopBundle\Command;
12
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\Question;
18
19
20
/**
21
 * Creates new accounts or resets their passwords
22
 * @package symfony
23
 * @subpackage Command
24
 */
25
class AccountCommand extends Command
26
{
27
	/**
28
	 * Configures the command name and description.
29
	 */
30
	protected function configure()
31
	{
32
		$this->setName( 'aimeos:account');
33
		$this->setDescription( 'Creates new (admin) accounts' );
34
		$this->addArgument( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' );
35
		$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default unittest" (none for all)' );
36
		$this->addOption( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' );
37
		$this->addOption( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' );
38
		$this->addOption( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' );
39
	}
40
41
42
	/**
43
	 * Execute the console command.
44
	 *
45
	 * @param InputInterface $input Input object
46
	 * @param OutputInterface $output Output object
47
	 */
48
	protected function execute( InputInterface $input, OutputInterface $output )
49
	{
50
		$code = $input->getArgument( 'email' );
51
		if( ( $password = $input->getOption( 'password' ) ) === null )
52
		{
53
			$helper = $this->getHelper( 'question' );
54
			$question = new Question( 'Password' );
55
			$question->setHidden( true );
56
57
			$password = $helper->ask( $input, $output, $question );
58
		}
59
60
		$context = $this->getContainer()->get( 'aimeos_context' )->get( false );
61
		$context->setEditor( 'aimeos:account' );
62
63
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
64
		$context->setLocale( $localeManager->createItem() );
65
66
		$user = $this->createCustomerItem( $context, $code, $password );
67
68
		if( $input->getOption( 'admin' ) ) {
69
			$this->addGroup( $input, $context, $user, 'admin' );
70
		}
71
72
		if( $input->getOption( 'editor' ) ) {
73
			$this->addGroup( $input, $context, $user, 'editor' );
74
		}
75
	}
76
77
78
	/**
79
	 * Adds the group to the given user
80
	 *
81
	 * @param InputInterface $input Input object
82
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
83
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
84
	 * @param string $group Unique customer group code
85
	 */
86
	protected function addGroup( InputInterface $input, \Aimeos\MShop\Context\Item\Iface $context,
87
		\Aimeos\MShop\Customer\Item\Iface $user, $group )
88
	{
89
		$output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for sites', $group, $user->getCode() ) );
0 ignored issues
show
Bug introduced by
The variable $output does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
90
91
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
92
93
		foreach( $this->getSiteItems( $context, $input ) as $siteItem )
94
		{
95
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
96
97
			$lcontext = clone $context;
98
			$lcontext->setLocale( $localeItem );
99
100
			$output->writeln( '- ' . $siteItem->getCode() );
101
102
			$groupItem = $this->getGroupItem( $lcontext, $group );
103
			$this->addListItem( $lcontext, $user->getId(), $groupItem->getId() );
104
		}
105
	}
106
107
108
	/**
109
	 * Associates the user to the group by their given IDs
110
	 *
111
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
112
	 * @param string $userid Unique user ID
113
	 * @param string $groupid Unique group ID
114
	 */
115
	protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid )
116
	{
117
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'lists' );
118
		$typeid = $manager->getSubmanager( 'type' )->findItem( 'default', array(), 'customer/group' )->getId();
119
120
		$search = $manager->createSearch();
121
		$expr = array(
122
			$search->compare( '==', 'customer.lists.parentid', $userid ),
123
			$search->compare( '==', 'customer.lists.refid', $groupid ),
124
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
125
			$search->compare( '==', 'customer.lists.typeid', $typeid ),
126
		);
127
		$search->setConditions( $search->combine( '&&', $expr ) );
128
		$search->setSlice( 0, 1 );
129
130
		if( count( $manager->searchItems( $search ) ) === 0 )
131
		{
132
			$item = $manager->createItem();
133
			$item->setDomain( 'customer/group' );
134
			$item->setParentId( $userid );
135
			$item->setTypeId( $typeid );
136
			$item->setRefId( $groupid );
137
			$item->setStatus( 1 );
138
139
			$manager->saveItem( $item, false );
140
		}
141
	}
142
143
144
	/**
145
	 * Returns the customer item for the given e-mail and set its password
146
	 *
147
	 * If the customer doesn't exist yet, it will be created.
148
	 *
149
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
150
	 * @param string $email Unique e-mail address
151
	 * @param string $password New user password
152
	 * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object
153
	 */
154
	protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password )
155
	{
156
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
157
158
		try {
159
			$item = $manager->findItem( $email );
160
		} catch( \Aimeos\MShop\Exception $e ) {
161
			$item = $manager->createItem();
162
		}
163
164
		$item->setCode( $email );
165
		$item->setLabel( $email );
166
		$item->getPaymentAddress()->setEmail( $email );
167
		$item->setPassword( $password );
168
169
		$manager->saveItem( $item );
170
171
		return $item;
172
	}
173
174
175
	/**
176
	 * Returns the customer group item for the given code
177
	 *
178
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
179
	 * @param string $code Unique customer group code
180
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
181
	 */
182
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code )
183
	{
184
		$manager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context )->getSubmanager( 'group' );
185
186
		try
187
		{
188
			$item = $manager->findItem( $code );
189
		}
190
		catch( \Aimeos\MShop\Exception $e )
191
		{
192
			$item = $manager->createItem();
193
			$item->setLabel( $code );
194
			$item->setCode( $code );
195
196
			$manager->saveItem( $item );
197
		}
198
199
		return $item;
200
	}
201
}
202