From 31387c1840684e841ea0f6657b4e8e86c3147201 Mon Sep 17 00:00:00 2001 From: Vassyli Date: Tue, 26 Jan 2021 18:27:53 +0100 Subject: [PATCH] Adds a command to remove scenes. --- .../Command/Scene/SceneRemoveCommand.php | 83 +++++++++++++++++++ src/Console/Main.php | 14 +++- src/Models/Scene.php | 25 +++++- 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/Console/Command/Scene/SceneRemoveCommand.php diff --git a/src/Console/Command/Scene/SceneRemoveCommand.php b/src/Console/Command/Scene/SceneRemoveCommand.php new file mode 100644 index 0000000..88a433c --- /dev/null +++ b/src/Console/Command/Scene/SceneRemoveCommand.php @@ -0,0 +1,83 @@ +setName('scene:remove') + ->setDescription('Removes a scene.') + ->setDefinition( + new InputDefinition([ + new InputArgument( + "id", + mode: InputArgument::REQUIRED, + description: "Scene ID", + ), + ]) + ) + ; + } + + /** + * @inheritDoc + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $em = $this->game->getEntityManager(); + + $io = new SymfonyStyle($input, $output); + + $sceneId = $input->getArgument("id"); + + // Get scene + /** @var Scene $scene */ + $scene = $em->getRepository(Scene::class)->find($sceneId); + + if (!$scene) { + $io->error("The scene with the ID {$sceneId} was not found."); + return Command::FAILURE; + } + + if (!$scene->isRemovable()) { + $io->error("The scene with the ID {$sceneId} was marked as not removable. Please remove the responsible"); + return Command::FAILURE; + } + + // Mark for removal and flush + try { + $em->remove($scene); + $em->flush(); + } catch (\Exception $e) { + $io->error("Removal of {$sceneId} was not possible: {$e->getMessage()}"); + return Command::FAILURE; + } + + $io->success("Scene was successfully removed."); + + return Command::SUCCESS; + } +} diff --git a/src/Console/Main.php b/src/Console/Main.php index 7b4df49..f3ab554 100644 --- a/src/Console/Main.php +++ b/src/Console/Main.php @@ -13,6 +13,7 @@ use LotGD\Core\Console\Command\Database\DatabaseSchemaUpdateCommand; use LotGD\Core\Console\Command\Module\ModuleRegisterCommand; use LotGD\Core\Console\Command\Module\ModuleValidateCommand; use LotGD\Core\Console\Command\Scene\SceneAddCommand; +use LotGD\Core\Console\Command\Scene\SceneRemoveCommand; use LotGD\Core\Console\Command\Scene\SceneAddConnectionGroupCommand; use LotGD\Core\Console\Command\Scene\SceneConnectCommand; use LotGD\Core\Console\Command\Scene\SceneListCommand; @@ -53,15 +54,26 @@ class Main $this->application->add(new DatabaseInitCommand($this->game)); $this->application->add(new DatabaseSchemaUpdateCommand($this->game)); $this->application->add(new ConsoleCommand($this->game)); + + // Character commands $this->application->add(new CharacterListCommand($this->game)); $this->application->add(new CharacterResetViewpointCommand($this->game)); + + // Scene commands $this->application->add(new SceneListCommand($this->game)); $this->application->add(new SceneAddCommand($this->game)); + $this->application->add(new SceneRemoveCommand($this->game)); + $this->application->add(new SceneShowCommand($this->game)); + + // Scene connections $this->application->add(new SceneConnectCommand($this->game)); $this->application->add(new SceneDisconnectCommand($this->game)); - $this->application->add(new SceneShowCommand($this->game)); + + // Scene connection group $this->application->add(new SceneAddConnectionGroupCommand($this->game)); $this->application->add(new SceneRemoveConnectionGroupCommand($this->game)); + + // Scene templates $this->application->add(new SceneTemplateListCommand($this->game)); // Add additional ones diff --git a/src/Models/Scene.php b/src/Models/Scene.php index 2ff8ff6..06ea72c 100644 --- a/src/Models/Scene.php +++ b/src/Models/Scene.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\OneToMany; use Doctrine\ORM\Mapping\Table; +use Doctrine\ORM\Mapping\Column; use LotGD\Core\Exceptions\ArgumentException; use LotGD\Core\Tools\Model\Creator; @@ -31,9 +32,15 @@ class Scene implements CreateableInterface, SceneConnectable use SceneBasics; use PropertyManager; - /** @Id @Column(type="string", length=36, unique=True, name="id", options={"fixed"=true}) */ + /** + * @Id + * @Column(type="string", length=36, unique=True, name="id", options={"fixed"=true}) + */ protected string $id; + /** @Column(type="boolean", nullable=false, options={"default"=true}) */ + protected bool $removeable = true; + /** * @OneToMany(targetEntity="SceneConnectionGroup", mappedBy="scene", cascade={"persist", "remove"}) * @var ?Collection @@ -84,6 +91,22 @@ class Scene implements CreateableInterface, SceneConnectable $this->incomingConnections = new ArrayCollection(); } + /** + * @return bool + */ + public function isRemovable(): bool + { + return $this->removeable; + } + + /** + * @param bool $removable + */ + public function setRemovable(bool $removable) + { + $this->removeable = $removable; + } + /** * Returns the primary ID for this scene. * @return int