Passed
Push — master ( 390920...14e72b )
by Aimeos
11:43
created

Command/AccountCommand.php (2 issues)

Labels
Severity
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
	protected static $defaultName = 'aimeos:account';
28
29
30
	/**
31
	 * Configures the command name and description.
32
	 */
33
	protected function configure()
34
	{
35
		$this->setName( self::$defaultName );
36
		$this->setDescription( 'Creates new (admin) accounts' );
37
		$this->addArgument( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' );
38
		$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default"', 'default' );
39
		$this->addOption( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' );
40
		$this->addOption( 'super', null, InputOption::VALUE_NONE, 'If account should have super user privileges' );
41
		$this->addOption( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' );
42
		$this->addOption( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' );
43
	}
44
45
46
	/**
47
	 * Execute the console command.
48
	 *
49
	 * @param InputInterface $input Input object
50
	 * @param OutputInterface $output Output object
51
	 */
52
	protected function execute( InputInterface $input, OutputInterface $output )
53
	{
54
		$email = $input->getArgument( 'email' );
55
56
		if( ( $password = $input->getOption( 'password' ) ) === null )
57
		{
58
			$helper = $this->getHelper( 'question' );
59
			$question = new Question( 'Password' );
60
			$question->setHidden( true );
61
62
			$password = $helper->ask( $input, $output, $question );
63
		}
64
65
		$context = $this->getContainer()->get( 'aimeos.context' )->get( false, 'command' );
66
		$context->setEditor( 'aimeos:account' );
67
68
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
69
		$localeItem = $localeManager->bootstrap( $input->getArgument( 'site' ) ?: 'default', '', '', false );
70
		$context->setLocale( $localeItem );
71
72
		$manager = \Aimeos\MShop::create( $context, 'customer' );
73
74
		try {
75
			$item = $manager->find( $email );
0 ignored issues
show
The method find() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Base\Coupon\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Order\Manager\Base\Service\Iface or Aimeos\MShop\Order\Manager\Base\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Order\Manag...Service\Attribute\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Order\Manag...Product\Attribute\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Address\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
			/** @scrutinizer ignore-call */ 
76
   $item = $manager->find( $email );
Loading history...
76
		} catch( \Aimeos\MShop\Exception $e ) {
77
			$item = $manager->createItem();
78
		}
79
80
		$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 );
81
		$item->getPaymentAddress()->setEmail( $email );
82
83
		$manager->saveItem( $this->addGroups( $input, $output, $context, $item ) );
0 ignored issues
show
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
		$manager->/** @scrutinizer ignore-call */ 
84
            saveItem( $this->addGroups( $input, $output, $context, $item ) );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
		$this->addRoles( $input, $email );
85
	}
86
87
88
	/**
89
	 * Adds the group to the given user
90
	 *
91
	 * @param InputInterface $input Input object
92
	 * @param OutputInterface $output Output object
93
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
94
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
95
	 */
96
	protected function addGroups( InputInterface $input, OutputInterface $output,
97
		\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface
98
	{
99
		if( $input->getOption( 'admin' ) ) {
100
			$this->addGroup( $input, $output, $context, $user, 'admin' );
101
		}
102
103
		if( $input->getOption( 'editor' ) ) {
104
			$this->addGroup( $input, $output, $context, $user, 'editor' );
105
		}
106
107
		return $user;
108
	}
109
110
111
	/**
112
	 * Adds the group to the given user
113
	 *
114
	 * @param InputInterface $input Input object
115
	 * @param OutputInterface $output Output object
116
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
117
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
118
	 * @param string $group Unique customer group code
119
	 */
120
	protected function addGroup( InputInterface $input, OutputInterface $output, \Aimeos\MShop\Context\Item\Iface $context,
121
		\Aimeos\MShop\Customer\Item\Iface $user, string $group ) : \Aimeos\MShop\Customer\Item\Iface
122
	{
123
		$site = $context->getLocale()->getSiteItem()->getCode();
124
		$output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for site "%3$s"', $group, $user->getCode(), $site ) );
125
126
		$groupId = $this->getGroupItem( $context, $group )->getId();
127
		return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) );
128
	}
129
130
131
	/**
132
	 * Adds required roles to user identified by his e-mail
133
	 *
134
	 * @param InputInterface $input Input object
135
	 * @param string $email Unique e-mail address
136
	 */
137
	protected function addRoles( InputInterface $input, string $email )
138
	{
139
		if( $this->getContainer()->has( 'fos_user.user_manager' ) )
140
		{
141
			$userManager = $this->getContainer()->get( 'fos_user.user_manager' );
142
143
			if( ( $fosUser = $userManager->findUserByUsername( $email ) ) === null ) {
144
				throw new \RuntimeException( 'No user created' );
145
			}
146
147
			$fosUser->setSuperAdmin( false );
148
149
			if( $input->getOption( 'super' ) ) {
150
				$fosUser->setSuperAdmin( true );
151
			}
152
153
			if( $input->getOption( 'admin' ) || $input->getOption( 'editor' ) ) {
154
				$fosUser->addRole( 'ROLE_ADMIN' );
155
			}
156
157
			$userManager->updateUser( $fosUser );
158
		}
159
	}
160
161
162
	/**
163
	 * Returns the customer group item for the given code
164
	 *
165
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
166
	 * @param string $code Unique customer group code
167
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
168
	 */
169
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context,
170
		string $code ) : \Aimeos\MShop\Customer\Item\Group\Iface
171
	{
172
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
173
174
		try
175
		{
176
			$item = $manager->find( $code );
177
		}
178
		catch( \Aimeos\MShop\Exception $e )
179
		{
180
			$item = $manager->createItem();
181
			$item->setLabel( $code );
182
			$item->setCode( $code );
183
184
			$manager->saveItem( $item );
185
		}
186
187
		return $item;
188
	}
189
}
190