|
15 | 15 | use App\Repository\UserRepository;
|
16 | 16 | use App\Utils\Validator;
|
17 | 17 | use Doctrine\ORM\EntityManagerInterface;
|
| 18 | +use Psr\Log\LoggerInterface; |
18 | 19 | use Symfony\Component\Console\Attribute\AsCommand;
|
19 | 20 | use Symfony\Component\Console\Command\Command;
|
20 | 21 | use Symfony\Component\Console\Exception\RuntimeException;
|
@@ -49,7 +50,8 @@ class DeleteUserCommand extends Command
|
49 | 50 | public function __construct(
|
50 | 51 | private EntityManagerInterface $entityManager,
|
51 | 52 | private Validator $validator,
|
52 |
| - private UserRepository $users |
| 53 | + private UserRepository $users, |
| 54 | + private LoggerInterface $logger |
53 | 55 | ) {
|
54 | 56 | parent::__construct();
|
55 | 57 | }
|
@@ -114,15 +116,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
114 | 116 | throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
|
115 | 117 | }
|
116 | 118 |
|
117 |
| - // After an entity has been removed its in-memory state is the same |
| 119 | + // After an entity has been removed, its in-memory state is the same |
118 | 120 | // as before the removal, except for generated identifiers.
|
119 | 121 | // See https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#removing-entities
|
120 | 122 | $userId = $user->getId();
|
121 | 123 |
|
122 | 124 | $this->entityManager->remove($user);
|
123 | 125 | $this->entityManager->flush();
|
124 | 126 |
|
125 |
| - $this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail())); |
| 127 | + $userUsername = $user->getUsername(); |
| 128 | + $userEmail = $user->getEmail(); |
| 129 | + |
| 130 | + $this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $userUsername, $userId, $userEmail)); |
| 131 | + |
| 132 | + // Logging is helpful and important to keep a trace of what happened in the software runtime flow. |
| 133 | + // See https://symfony.com/doc/current/logging.html |
| 134 | + $this->logger->info('User "{username}" (ID: {id}, email: {email}) was successfully deleted.', ['username' => $userUsername, 'id' => $userId, 'email' => $userEmail]); |
126 | 135 |
|
127 | 136 | return Command::SUCCESS;
|
128 | 137 | }
|
|
0 commit comments