-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeleteUserCommand.php
132 lines (110 loc) · 4.23 KB
/
DeleteUserCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Command;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Utils\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* A console command that deletes users from the database.
*
* To use this command, open a terminal window, enter into your project
* directory and execute the following:
*
* $ php bin/console app:delete-user
*
* Check out the code of the src/Command/AddUserCommand.php file for
* the full explanation about Symfony commands.
*
* See https://symfony.com/doc/current/console.html
*
* @author Oleg Voronkovich <[email protected]>
*/
class DeleteUserCommand extends Command
{
protected static $defaultName = 'app:delete-user';
/** @var SymfonyStyle */
private $io;
private $entityManager;
private $validator;
private $users;
public function __construct(EntityManagerInterface $em, Validator $validator, UserRepository $users)
{
parent::__construct();
$this->entityManager = $em;
$this->validator = $validator;
$this->users = $users;
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Deletes users from the database')
->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user')
->setHelp(<<<'HELP'
The <info>%command.name%</info> command deletes users from the database:
<info>php %command.full_name%</info> <comment>username</comment>
If you omit the argument, the command will ask you to
provide the missing value:
<info>php %command.full_name%</info>
HELP
);
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
// SymfonyStyle is an optional feature that Symfony provides so you can
// apply a consistent look to the commands of your application.
// See https://symfony.com/doc/current/console/style.html
$this->io = new SymfonyStyle($input, $output);
}
protected function interact(InputInterface $input, OutputInterface $output)
{
if (null !== $input->getArgument('username')) {
return;
}
$this->io->title('Delete User Command Interactive Wizard');
$this->io->text([
'If you prefer to not use this interactive wizard, provide the',
'arguments required by this command as follows:',
'',
' $ php bin/console app:delete-user username',
'',
'Now we\'ll ask you for the value of all the missing command arguments.',
'',
]);
$username = $this->io->ask('Username', null, [$this->validator, 'validateUsername']);
$input->setArgument('username', $username);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$username = $this->validator->validateUsername($input->getArgument('username'));
/** @var User $user */
$user = $this->users->findOneByUsername($username);
if (null === $user) {
throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
}
// After an entity has been removed its in-memory state is the same
// as before the removal, except for generated identifiers.
// See https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#removing-entities
$userId = $user->getId();
$this->entityManager->remove($user);
$this->entityManager->flush();
$this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail()));
return 0;
}
}