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