From 1a4926565836a2346c57bc45b553c0017c4358a9 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 9 Sep 2025 16:39:02 +0200 Subject: [PATCH] Ajout commande creation role --- src/Command/CreateRoleCommand.php | 64 +++++++++++++++++++++++++++++++ src/Entity/Roles.php | 4 ++ 2 files changed, 68 insertions(+) create mode 100644 src/Command/CreateRoleCommand.php diff --git a/src/Command/CreateRoleCommand.php b/src/Command/CreateRoleCommand.php new file mode 100644 index 0000000..6155951 --- /dev/null +++ b/src/Command/CreateRoleCommand.php @@ -0,0 +1,64 @@ +entityManager = $entityManager; + } + + protected function configure(): void + { + $this + ->addArgument('name', InputArgument::REQUIRED, 'The name of the role'); // role name required + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $roleName = trim($input->getArgument('name')); + $roleName = strtoupper($roleName); // Normalize to uppercase + + // Ensure not empty + if ($roleName === '') { + $output->writeln('The role name cannot be empty'); + return Command::FAILURE; + } + + // Check if role already exists + $existing = $this->entityManager->getRepository(Roles::class) + ->findOneBy(['name' => $roleName]); + + if ($existing) { + $output->writeln("Role '{$roleName}' already exists."); + return Command::SUCCESS; // not failure, just redundant + } + + // Create and persist new role + $role = new Roles(); + $role->setName($roleName); + + $this->entityManager->persist($role); + $this->entityManager->flush(); + + $output->writeln("Role '{$roleName}' created successfully!"); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Entity/Roles.php b/src/Entity/Roles.php index 47759b7..9147c55 100644 --- a/src/Entity/Roles.php +++ b/src/Entity/Roles.php @@ -19,6 +19,10 @@ class Roles #[ORM\Column(options: ['default' => 'CURRENT_TIMESTAMP'])] private ?\DateTimeImmutable $createdAt = null; + public function __construct() + { + $this->createdAt = new \DateTimeImmutable(); + } public function getId(): ?int { return $this->id;