From 59ce82ef434ca0cc0f49807b0888073f4268e7c7 Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Tue, 7 Jan 2025 17:58:43 +0100 Subject: [PATCH] refs #1319. Default menu logic --- migrations/Version20250107121226.php | 31 +++++++++++++++ migrations/Version20250107124654.php | 33 ++++++++++++++++ src/Command/LoadDefaultMenuCommand.php | 38 +++++++++++++++++++ .../Migration/MigrateImagesCommand.php | 12 +++++- src/Dto/Input/MenuInput.php | 6 +++ src/Dto/Output/MenuOutput.php | 4 ++ src/Entity/Client.php | 3 +- src/Entity/Menu.php | 33 +++------------- src/Entity/OrganizationalUnit.php | 37 ------------------ src/EventSubscriber/ClientSubscriber.php | 4 +- src/State/Processor/ClientProcessor.php | 23 +++++++---- 11 files changed, 149 insertions(+), 75 deletions(-) create mode 100644 migrations/Version20250107121226.php create mode 100644 migrations/Version20250107124654.php create mode 100644 src/Command/LoadDefaultMenuCommand.php diff --git a/migrations/Version20250107121226.php b/migrations/Version20250107121226.php new file mode 100644 index 0000000..2baf20c --- /dev/null +++ b/migrations/Version20250107121226.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE menu ADD is_default TINYINT(1) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE menu DROP is_default'); + } +} diff --git a/migrations/Version20250107124654.php b/migrations/Version20250107124654.php new file mode 100644 index 0000000..ce5bd6d --- /dev/null +++ b/migrations/Version20250107124654.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE client DROP FOREIGN KEY FK_C7440455CCD7E912'); + $this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455CCD7E912 FOREIGN KEY (menu_id) REFERENCES menu (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE client DROP FOREIGN KEY FK_C7440455CCD7E912'); + $this->addSql('ALTER TABLE client ADD CONSTRAINT FK_C7440455CCD7E912 FOREIGN KEY (menu_id) REFERENCES menu (id)'); + } +} diff --git a/src/Command/LoadDefaultMenuCommand.php b/src/Command/LoadDefaultMenuCommand.php new file mode 100644 index 0000000..148983b --- /dev/null +++ b/src/Command/LoadDefaultMenuCommand.php @@ -0,0 +1,38 @@ +setName('Default menu'); + $menu->setResolution('1920x1080'); + $menu->setComments('Default menu comments'); + $menu->setPublicUrl('main'); + $menu->setDefault(true); + + $this->entityManager->persist($menu); + $this->entityManager->flush(); + + return Command::SUCCESS; + } +} diff --git a/src/Command/Migration/MigrateImagesCommand.php b/src/Command/Migration/MigrateImagesCommand.php index 17de59c..9cb2ca4 100644 --- a/src/Command/Migration/MigrateImagesCommand.php +++ b/src/Command/Migration/MigrateImagesCommand.php @@ -6,7 +6,9 @@ namespace App\Command\Migration; use App\Entity\Client; use App\Entity\Image; +use App\Entity\ImageRepository; use App\Entity\OrganizationalUnit; +use App\Model\ImageStatus; use App\Model\OrganizationalUnitTypes; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Query\ResultSetMapping; @@ -70,18 +72,26 @@ class MigrateImagesCommand extends Command } $imageEntity = $imagesRepository->findOneBy(['migrationId' => $image['idimagen']]); + + $repository = $this->entityManager->getRepository(ImageRepository::class)->findAll()[0]; + if(!$imageEntity) { $imageEntity = new Image(); $imageEntity->setMigrationId((string) $image['idimagen']); $imageEntity->setName($image['nombreca']); $imageEntity->setClient($clientEntity); - $imageEntity->setOrganizationalUnit($ouEntity); + //$imageEntity->setOrganizationalUnit($ouEntity); + $imageEntity->setRemotePc(false); + $imageEntity->setStatus(ImageStatus::SUCCESS); + $imageEntity->setRepository($repository); $imageEntity->setRevision((string) $image['revision']); $imageEntity->setDescription($image['descripcion']); $imageEntity->setComments($image['comentarios']); } $this->entityManager->persist($imageEntity); + $this->entityManager->flush(); + } $this->entityManager->flush(); diff --git a/src/Dto/Input/MenuInput.php b/src/Dto/Input/MenuInput.php index 762d868..c1117f3 100644 --- a/src/Dto/Input/MenuInput.php +++ b/src/Dto/Input/MenuInput.php @@ -33,6 +33,10 @@ final class MenuInput #[ApiProperty(description: 'The private url of the menu', example: "http://example.com")] public ?string $privateUrl = null; + #[Groups(['menu:write'])] + #[ApiProperty(description: 'The default menu', example: "false")] + public ?bool $isDefault = false; + public function __construct(?Menu $menu = null) { if (!$menu) { @@ -44,6 +48,7 @@ final class MenuInput $this->resolution = $menu->getResolution(); $this->publicUrl = $menu->getPublicUrl(); $this->privateUrl = $menu->getPrivateUrl(); + $this->isDefault = $menu->isDefault(); } public function createOrUpdateEntity(?Menu $menu = null): Menu @@ -57,6 +62,7 @@ final class MenuInput $menu->setResolution($this->resolution); $menu->setPublicUrl($this->publicUrl); $menu->setPrivateUrl($this->privateUrl); + $menu->setDefault($this->isDefault); return $menu; } diff --git a/src/Dto/Output/MenuOutput.php b/src/Dto/Output/MenuOutput.php index 03b52f5..e9bc6fa 100644 --- a/src/Dto/Output/MenuOutput.php +++ b/src/Dto/Output/MenuOutput.php @@ -25,6 +25,9 @@ final class MenuOutput extends AbstractOutput #[Groups(['menu:read'])] public ?string $privateUrl = null; + #[Groups(['menu:read'])] + public ?bool $isDefault = false; + #[Groups(['menu:read'])] public \DateTime $createdAt; @@ -40,6 +43,7 @@ public function __construct(Menu $menu) $this->comments = $menu->getComments(); $this->publicUrl = $menu->getPublicUrl(); $this->privateUrl = $menu->getPrivateUrl(); + $this->isDefault = $menu->isDefault(); $this->createdAt = $menu->getCreatedAt(); $this->createdBy = $menu->getCreatedBy(); } diff --git a/src/Entity/Client.php b/src/Entity/Client.php index 776ee4f..54ae330 100644 --- a/src/Entity/Client.php +++ b/src/Entity/Client.php @@ -49,6 +49,7 @@ class Client extends AbstractEntity private Collection $partitions; #[ORM\ManyToOne] + #[ORM\JoinColumn( onDelete: 'SET NULL')] private ?Menu $menu = null; #[ORM\ManyToOne] @@ -63,7 +64,7 @@ class Client extends AbstractEntity #[ORM\ManyToOne(inversedBy: 'clients')] private ?PxeTemplate $template = null; - #[ORM\ManyToOne(inversedBy: 'clients')] + #[ORM\ManyToOne()] private ?ImageRepository $repository = null; #[ORM\ManyToOne(inversedBy: 'clients')] diff --git a/src/Entity/Menu.php b/src/Entity/Menu.php index 7798e14..ef43159 100644 --- a/src/Entity/Menu.php +++ b/src/Entity/Menu.php @@ -24,16 +24,12 @@ class Menu extends AbstractEntity #[ORM\Column(length: 255, nullable: true)] private ?string $privateUrl = null; - /** - * @var Collection - */ - #[ORM\OneToMany(mappedBy: 'menu', targetEntity: Client::class)] - private Collection $clients; + #[ORM\Column] + private ?bool $isDefault = null; public function __construct() { parent::__construct(); - $this->clients = new ArrayCollection(); } public function getResolution(): ?string @@ -84,31 +80,14 @@ class Menu extends AbstractEntity return $this; } - /** - * @return Collection - */ - public function getClients(): Collection + public function isDefault(): ?bool { - return $this->clients; + return $this->isDefault; } - public function addClient(Client $client): static + public function setDefault(bool $isDefault): static { - if (!$this->clients->contains($client)) { - $this->clients->add($client); - } - - return $this; - } - - public function removeClient(Client $client): static - { - if ($this->clients->removeElement($client)) { - // set the owning side to null (unless already changed) - if ($client->getMenu() === $this) { - $client->setMenu(null); - } - } + $this->isDefault = $isDefault; return $this; } diff --git a/src/Entity/OrganizationalUnit.php b/src/Entity/OrganizationalUnit.php index a7d2756..b0d5596 100644 --- a/src/Entity/OrganizationalUnit.php +++ b/src/Entity/OrganizationalUnit.php @@ -97,12 +97,6 @@ class OrganizationalUnit extends AbstractEntity #[ORM\Column] private ?bool $reserved = false; - /** - * @var Collection - */ - #[ORM\OneToMany(mappedBy: 'organizationalUnit', targetEntity: Image::class)] - private Collection $images; - public function __construct() { parent::__construct(); @@ -110,7 +104,6 @@ class OrganizationalUnit extends AbstractEntity $this->users = new ArrayCollection(); $this->clients = new ArrayCollection(); $this->softwareProfiles = new ArrayCollection(); - $this->images = new ArrayCollection(); } public function getDescription(): ?string @@ -436,34 +429,4 @@ class OrganizationalUnit extends AbstractEntity return $this; } - - /** - * @return Collection - */ - public function getImages(): Collection - { - return $this->images; - } - - public function addImage(Image $image): static - { - if (!$this->images->contains($image)) { - $this->images->add($image); - $image->setOrganizationalUnit($this); - } - - return $this; - } - - public function removeImage(Image $image): static - { - if ($this->images->removeElement($image)) { - // set the owning side to null (unless already changed) - if ($image->getOrganizationalUnit() === $this) { - $image->setOrganizationalUnit(null); - } - } - - return $this; - } } diff --git a/src/EventSubscriber/ClientSubscriber.php b/src/EventSubscriber/ClientSubscriber.php index 1a0274d..351e352 100644 --- a/src/EventSubscriber/ClientSubscriber.php +++ b/src/EventSubscriber/ClientSubscriber.php @@ -27,7 +27,7 @@ final readonly class ClientSubscriber implements EventSubscriberInterface public static function getSubscribedEvents(): array { return [ - KernelEvents::VIEW => ['sendMail', EventPriorities::POST_WRITE], + KernelEvents::VIEW => ['updatePxe', EventPriorities::POST_WRITE], ]; } @@ -37,7 +37,7 @@ final readonly class ClientSubscriber implements EventSubscriberInterface * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function sendMail(ViewEvent $event): void + public function updatePxe(ViewEvent $event): void { $clientOutput = $event->getControllerResult(); $method = $event->getRequest()->getMethod(); diff --git a/src/State/Processor/ClientProcessor.php b/src/State/Processor/ClientProcessor.php index 06eab7a..76605ff 100644 --- a/src/State/Processor/ClientProcessor.php +++ b/src/State/Processor/ClientProcessor.php @@ -13,12 +13,14 @@ use App\Dto\Input\ClientInput; use App\Dto\Output\ClientOutput; use App\Dto\Output\UserGroupOutput; use App\Repository\ClientRepository; +use App\Repository\MenuRepository; -class ClientProcessor implements ProcessorInterface +readonly class ClientProcessor implements ProcessorInterface { public function __construct( - private readonly ClientRepository $clientRepository, - private readonly ValidatorInterface $validator + private ClientRepository $clientRepository, + private MenuRepository $menuRepository, + private ValidatorInterface $validator ) { } @@ -52,11 +54,18 @@ class ClientProcessor implements ProcessorInterface $entity = $this->clientRepository->findOneByUuid($uriVariables['uuid']); } - $userGroup = $data->createOrUpdateEntity($entity); - $this->validator->validate($userGroup); - $this->clientRepository->save($userGroup); + $defaultMenu = $this->menuRepository->findOneBy(['isDefault' => true]); - return new ClientOutput($userGroup); + $client = $data->createOrUpdateEntity($entity); + + if ($defaultMenu) { + $client->setMenu($defaultMenu); + } + + $this->validator->validate($client); + $this->clientRepository->save($client); + + return new ClientOutput($client); } private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null