1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Command\User; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use Cycle\ORM\ORMInterface; |
7
|
|
|
use Cycle\ORM\Transaction; |
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\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
class CreateCommand extends Command |
16
|
|
|
{ |
17
|
|
|
private const EXIT_CODE_FAILED_TO_PERSIST = 1; |
18
|
|
|
|
19
|
|
|
private $orm; |
20
|
|
|
|
21
|
|
|
protected static $defaultName = 'user/create'; |
22
|
|
|
|
23
|
|
|
public function __construct(ORMInterface $orm) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
$this->orm = $orm; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function configure(): void |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setDescription('Creates a user') |
33
|
|
|
->setHelp('This command allows you to create a user') |
34
|
|
|
->addArgument('login', InputArgument::REQUIRED, 'Login') |
35
|
|
|
->addArgument('password', InputArgument::REQUIRED, 'Password'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
$io = new SymfonyStyle($input, $output); |
41
|
|
|
|
42
|
|
|
$login = $input->getArgument('login'); |
43
|
|
|
$password = $input->getArgument('password'); |
44
|
|
|
|
45
|
|
|
$user = new User(); |
46
|
|
|
$user->setLogin($login); |
|
|
|
|
47
|
|
|
$user->setPassword($password); |
|
|
|
|
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$transaction = new Transaction($this->orm); |
51
|
|
|
$transaction->persist($user); |
52
|
|
|
$transaction->run(); |
53
|
|
|
$io->success('User created'); |
54
|
|
|
} catch (\Throwable $t) { |
55
|
|
|
$io->error($t->getMessage()); |
56
|
|
|
return self::EXIT_CODE_FAILED_TO_PERSIST; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|