|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Customer; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Customer\Api\AccountManagementInterface; |
|
6
|
|
|
use Magento\Framework\App\State\Proxy as AppState; |
|
7
|
|
|
use Magento\Framework\Exception\LocalizedException; |
|
8
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
class CreateCommand extends AbstractCustomerCommand |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var AccountManagementInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $accountManagement; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var AppState |
|
23
|
|
|
*/ |
|
24
|
|
|
private $appState; |
|
25
|
|
|
|
|
26
|
|
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this |
|
29
|
|
|
->setName('customer:create') |
|
30
|
|
|
->addArgument('email', InputArgument::OPTIONAL, 'Email') |
|
31
|
|
|
->addArgument('password', InputArgument::OPTIONAL, 'Password') |
|
32
|
|
|
->addArgument('firstname', InputArgument::OPTIONAL, 'Firstname') |
|
33
|
|
|
->addArgument('lastname', InputArgument::OPTIONAL, 'Lastname') |
|
34
|
|
|
->addArgument('website', InputArgument::OPTIONAL, 'Website') |
|
35
|
|
|
->addOption( |
|
36
|
|
|
'format', |
|
37
|
|
|
null, |
|
38
|
|
|
InputOption::VALUE_OPTIONAL, |
|
39
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
|
40
|
|
|
) |
|
41
|
|
|
->setDescription('Creates a new customer/user for shop frontend.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param AccountManagementInterface $accountManagement |
|
46
|
|
|
* @param AppState $appState |
|
47
|
|
|
*/ |
|
48
|
|
|
public function inject( |
|
49
|
|
|
AccountManagementInterface $accountManagement, |
|
50
|
|
|
AppState $appState |
|
51
|
|
|
) { |
|
52
|
|
|
$this->accountManagement = $accountManagement; |
|
53
|
|
|
$this->appState = $appState; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param InputInterface $input |
|
58
|
|
|
* @param OutputInterface $output |
|
59
|
|
|
* @return int |
|
60
|
|
|
* @throws \Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->detectMagento($output, true); |
|
65
|
|
|
if (!$this->initMagento()) { |
|
66
|
|
|
return 1; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
|
70
|
|
|
|
|
71
|
|
|
// Email |
|
72
|
|
|
$email = $this->getHelperSet()->get('parameter')->askEmail($input, $output); |
|
73
|
|
|
|
|
74
|
|
|
// Password |
|
75
|
|
|
if (($password = $input->getArgument('password')) == null) { |
|
|
|
|
|
|
76
|
|
|
$password = $dialog->askHiddenResponse($output, '<question>Password:</question>'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Firstname |
|
80
|
|
|
if (($firstname = $input->getArgument('firstname')) == null) { |
|
|
|
|
|
|
81
|
|
|
$firstname = $dialog->ask($output, '<question>Firstname:</question>'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Lastname |
|
85
|
|
|
if (($lastname = $input->getArgument('lastname')) == null) { |
|
|
|
|
|
|
86
|
|
|
$lastname = $dialog->ask($output, '<question>Lastname:</question>'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$website = $this->getHelperSet()->get('parameter')->askWebsite($input, $output); |
|
90
|
|
|
|
|
91
|
|
|
// create new customer |
|
92
|
|
|
$customer = $this->getCustomer(); |
|
93
|
|
|
$customer->setWebsiteId($website->getId()); |
|
94
|
|
|
$customer->loadByEmail($email); |
|
95
|
|
|
|
|
96
|
|
|
$outputPlain = $input->getOption('format') === null; |
|
97
|
|
|
|
|
98
|
|
|
$table = array(); |
|
99
|
|
|
$isError = false; |
|
100
|
|
|
|
|
101
|
|
|
if (!$customer->getId()) { |
|
102
|
|
|
$customer->setWebsiteId($website->getId()); |
|
103
|
|
|
$customer->setEmail($email); |
|
104
|
|
|
$customer->setFirstname($firstname); |
|
105
|
|
|
$customer->setLastname($lastname); |
|
106
|
|
|
$customer->setStoreId($website->getDefaultGroup()->getDefaultStore()->getId()); |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
$this->appState->setAreaCode('frontend'); |
|
110
|
|
|
$this->appState->emulateAreaCode( |
|
111
|
|
|
'frontend', |
|
112
|
|
|
[$this, 'createCustomer'], |
|
113
|
|
|
[$customer, $password] |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
if ($outputPlain) { |
|
117
|
|
|
$output->writeln( |
|
118
|
|
|
sprintf( |
|
119
|
|
|
'<info>Customer <comment>%s</comment> successfully created</info>', |
|
120
|
|
|
$email |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
} else { |
|
124
|
|
|
$table[] = array( |
|
125
|
|
|
$email, |
|
126
|
|
|
$password, |
|
127
|
|
|
$firstname, |
|
128
|
|
|
$lastname, |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
} catch (\Exception $e) { |
|
132
|
|
|
$isError = true; |
|
133
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
134
|
|
|
} |
|
135
|
|
|
} elseif ($outputPlain) { |
|
136
|
|
|
$output->writeln('<warning>Customer ' . $email . ' already exists</warning>'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (!$outputPlain) { |
|
140
|
|
|
$this->getHelper('table') |
|
|
|
|
|
|
141
|
|
|
->setHeaders(array('email', 'password', 'firstname', 'lastname')) |
|
142
|
|
|
->renderByFormat($output, $table, $input->getOption('format')); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $isError ? 1 : 0; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $customer |
|
150
|
|
|
* @param string $password |
|
151
|
|
|
* @throws \Magento\Framework\Exception\LocalizedException |
|
152
|
|
|
*/ |
|
153
|
|
|
public function createCustomer($customer, $password) |
|
154
|
|
|
{ |
|
155
|
|
|
try { |
|
156
|
|
|
// Fix for proxy which does not respect "emulateAreaCode". |
|
157
|
|
|
|
|
158
|
|
|
// @see \Magento\Framework\Session\SessionManager::isSessionExists Hack to prevent session problems |
|
159
|
|
|
@session_start(); |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
/** @var \Magento\Theme\Model\View\Design $design */ |
|
162
|
|
|
$design = $this->getObjectManager()->get(\Magento\Theme\Model\View\Design::class); |
|
163
|
|
|
$design->setArea('frontend'); |
|
164
|
|
|
$this->accountManagement->createAccount( |
|
165
|
|
|
$customer->getDataModel(), |
|
|
|
|
|
|
166
|
|
|
$password |
|
167
|
|
|
); |
|
168
|
|
|
} catch (LocalizedException $e) { |
|
|
|
|
|
|
169
|
|
|
if ($e->getRawMessage() !== 'Design config must have area and store.') { |
|
170
|
|
|
throw $e; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|