1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Presentation\Console\Component\User; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\User\Application\Service\UserService; |
18
|
|
|
use Acme\App\Core\Component\User\Application\Validation\UserValidationService; |
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
use Symfony\Component\Console\Command\Command; |
21
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A console command that deletes users from the database. |
28
|
|
|
* |
29
|
|
|
* To use this command, open a terminal window, enter into your project |
30
|
|
|
* directory and execute the following: |
31
|
|
|
* |
32
|
|
|
* $ php bin/console app:delete-user |
33
|
|
|
* |
34
|
|
|
* Check out the code of the src/App/Command/AddUserCommand.php file for |
35
|
|
|
* the full explanation about Symfony commands. |
36
|
|
|
* |
37
|
|
|
* See https://symfony.com/doc/current/cookbook/console/console_command.html |
38
|
|
|
* For more advanced uses, commands can be defined as services too. See |
39
|
|
|
* https://symfony.com/doc/current/console/commands_as_services.html |
40
|
|
|
* |
41
|
|
|
* @author Oleg Voronkovich <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class DeleteUserCommand extends Command |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected static $defaultName = 'app:delete-user'; |
49
|
|
|
|
50
|
|
|
/** @var SymfonyStyle */ |
51
|
|
|
private $io; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var EntityManagerInterface |
55
|
|
|
*/ |
56
|
|
|
private $entityManager; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var UserValidationService |
60
|
|
|
*/ |
61
|
|
|
private $validator; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var UserService |
65
|
|
|
*/ |
66
|
|
|
private $userService; |
67
|
|
|
|
68
|
|
|
public function __construct(EntityManagerInterface $em, UserValidationService $validator, UserService $userService) |
69
|
|
|
{ |
70
|
|
|
parent::__construct(); |
71
|
|
|
|
72
|
|
|
$this->entityManager = $em; |
73
|
|
|
$this->validator = $validator; |
74
|
|
|
$this->userService = $userService; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
protected function configure(): void |
81
|
|
|
{ |
82
|
|
|
$this |
83
|
|
|
->setDescription('Deletes users from the database') |
84
|
|
|
->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user') |
85
|
|
|
->setHelp(<<<'HELP' |
86
|
|
|
The <info>%command.name%</info> command deletes users from the database: |
|
|
|
|
87
|
|
|
|
88
|
|
|
<info>php %command.full_name%</info> <comment>username</comment> |
89
|
|
|
|
90
|
|
|
If you omit the argument, the command will ask you to |
91
|
|
|
provide the missing value: |
92
|
|
|
|
93
|
|
|
<info>php %command.full_name%</info> |
94
|
|
|
HELP |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output): void |
99
|
|
|
{ |
100
|
|
|
// SymfonyStyle is an optional feature that Symfony provides so you can |
101
|
|
|
// apply a consistent look to the commands of your application. |
102
|
|
|
// See https://symfony.com/doc/current/console/style.html |
103
|
|
|
$this->io = new SymfonyStyle($input, $output); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function interact(InputInterface $input, OutputInterface $output): void |
107
|
|
|
{ |
108
|
|
|
if ($input->getArgument('username') !== null) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->io->title('Delete User Command Interactive Wizard'); |
113
|
|
|
$this->io->text([ |
114
|
|
|
'If you prefer to not use this interactive wizard, provide the', |
115
|
|
|
'arguments required by this command as follows:', |
116
|
|
|
'', |
117
|
|
|
' $ php bin/console app:delete-user username', |
118
|
|
|
'', |
119
|
|
|
'Now we\'ll ask you for the value of all the missing command arguments.', |
120
|
|
|
'', |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$username = $this->io->ask('Username', null, [$this->validator, 'validateUsername']); |
124
|
|
|
$input->setArgument('username', $username); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
128
|
|
|
{ |
129
|
|
|
$username = $input->getArgument('username'); |
130
|
|
|
|
131
|
|
|
$this->userService->deleteUser($username); |
132
|
|
|
|
133
|
|
|
$this->entityManager->flush(); |
134
|
|
|
|
135
|
|
|
$this->io->success(sprintf('User "%s" was successfully deleted.', $username)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|