|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Command\User; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\User; |
|
6
|
|
|
use Ronanchilvers\Orm\Orm; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
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
|
|
|
use Symfony\Component\Console\Question\Question; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Command to create users |
|
17
|
|
|
* |
|
18
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class CreateCommand extends Command |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
public function configure() |
|
26
|
|
|
{ |
|
27
|
|
|
$this |
|
28
|
|
|
->setName('user:create') |
|
29
|
|
|
->setDescription('Create a user') |
|
30
|
|
|
->addArgument( |
|
31
|
|
|
'name', |
|
32
|
|
|
InputArgument::REQUIRED, |
|
33
|
|
|
'The name for the user' |
|
34
|
|
|
) |
|
35
|
|
|
->addArgument( |
|
36
|
|
|
'email', |
|
37
|
|
|
InputArgument::REQUIRED, |
|
38
|
|
|
'The email address for the user' |
|
39
|
|
|
) |
|
40
|
|
|
; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
$name = trim($input->getArgument('name')); |
|
|
|
|
|
|
49
|
|
|
$email = trim($input->getArgument('email')); |
|
50
|
|
|
|
|
51
|
|
|
$output->writeln('Creating new user...'); |
|
52
|
|
|
$output->writeln('Name : ' . $name); |
|
53
|
|
|
$output->writeln('Email : ' . $email); |
|
54
|
|
|
$helper = $this->getHelper('question'); |
|
55
|
|
|
|
|
56
|
|
|
$existing = Orm::finder(User::class)->select() |
|
57
|
|
|
->where(User::prefix('email'), $email) |
|
58
|
|
|
->one(); |
|
59
|
|
|
if ($existing instanceof User) { |
|
60
|
|
|
throw new RuntimeException('User already exists with email ' . $email); |
|
61
|
|
|
// $output->writeln('!!! User already exists with email ' . $email); |
|
62
|
|
|
// return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$question = new Question('Enter the password for the new user : '); |
|
66
|
|
|
$question->setHidden(true); |
|
67
|
|
|
$question->setHiddenFallback(false); |
|
68
|
|
|
|
|
69
|
|
|
$confirm = new Question('Confirm the password : '); |
|
70
|
|
|
$confirm->setHidden(true); |
|
71
|
|
|
$confirm->setHiddenFallback(false); |
|
72
|
|
|
|
|
73
|
|
|
$password = $helper->ask($input, $output, $question); |
|
74
|
|
|
$confirmation = $helper->ask($input, $output, $confirm); |
|
75
|
|
|
|
|
76
|
|
|
$password = trim($password); |
|
77
|
|
|
$confirmation = trim($confirmation); |
|
78
|
|
|
if ($password !== $confirmation) { |
|
79
|
|
|
throw new RuntimeException('Password does not match confirmation'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$output->writeln('Creating user record...'); |
|
83
|
|
|
$user = new User(); |
|
84
|
|
|
$user->name = $name; |
|
85
|
|
|
$user->email = $email; |
|
86
|
|
|
$user->password = password_hash($password, PASSWORD_DEFAULT); |
|
87
|
|
|
if (!$user->saveWithValidation()) { |
|
88
|
|
|
$errors = []; |
|
89
|
|
|
foreach ($user->getErrors() as $fieldErrors) { |
|
90
|
|
|
$errors = array_merge($errors, $fieldErrors); |
|
91
|
|
|
} |
|
92
|
|
|
$errors = implode(',', $errors); |
|
93
|
|
|
throw new RuntimeException('Unable to create user - ' . $errors); |
|
94
|
|
|
} |
|
95
|
|
|
$output->writeln("User {$email} created"); |
|
96
|
|
|
|
|
97
|
|
|
return 0; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|