From 6ff84dbe8da2b4de1d4f14ade6c1de6b683521a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Fri, 1 Mar 2024 00:46:58 +0100 Subject: [PATCH] Handle image delete --- config/admin/command.yml | 9 -- config/common.yml | 6 -- .../DeleteImageSliderImageFileCommand.php | 25 ------ .../DeleteImageSliderHandler.php | 30 ++++++- .../DeleteImageSliderImageFileHandler.php | 25 ------ ...teImageSliderImageFileHandlerInterface.php | 12 --- .../ImageSliderImageEventListener.php | 25 ------ src/Entity/ImageSlider.php | 2 +- src/Entity/ImageSliderImage.php | 85 +++++++++++++++++-- src/Entity/ImageSliderLang.php | 49 +++++------ 10 files changed, 129 insertions(+), 139 deletions(-) delete mode 100644 src/Domain/ImageSlider/Command/DeleteImageSliderImageFileCommand.php delete mode 100644 src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandler.php delete mode 100644 src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandlerInterface.php delete mode 100644 src/Domain/ImageSlider/EventListener/ImageSliderImageEventListener.php diff --git a/config/admin/command.yml b/config/admin/command.yml index ed75fe0..6897225 100644 --- a/config/admin/command.yml +++ b/config/admin/command.yml @@ -38,12 +38,3 @@ services: tags: - name: tactician.handler command: Oksydan\IsImageslider\Domain\ImageSlider\Command\ToggleStatusImageSliderCommand - - - Oksydan\IsImageslider\Domain\ImageSlider\CommandHandler\DeleteImageSliderImageFileHandlerInterface: - alias: Oksydan\IsImageslider\Domain\ImageSlider\CommandHandler\DeleteImageSliderImageFileHandler - - Oksydan\IsImageslider\Domain\ImageSlider\CommandHandler\DeleteImageSliderImageFileHandler: - tags: - - name: tactician.handler - command: Oksydan\IsImageslider\Domain\ImageSlider\Command\DeleteImageSliderImageFileCommand diff --git a/config/common.yml b/config/common.yml index ec0884b..6d8c1cf 100644 --- a/config/common.yml +++ b/config/common.yml @@ -16,9 +16,3 @@ services: oksydan.is_imageslider.context: class: Context factory: [ '\Context', getContext ] - - Oksydan\IsImageslider\Domain\ImageSlider\EventListener\ImageSliderImageEventListener: - class: Oksydan\IsImageslider\Domain\ImageSlider\EventListener\ImageSliderImageEventListener - autowire: true - tags: - - { name: 'doctrine.orm.entity_listener', event: 'postRemove', entity: 'Oksydan\IsImageslider\Domain\ImageSlider\Entity\ImageSliderImage' } diff --git a/src/Domain/ImageSlider/Command/DeleteImageSliderImageFileCommand.php b/src/Domain/ImageSlider/Command/DeleteImageSliderImageFileCommand.php deleted file mode 100644 index 36d247b..0000000 --- a/src/Domain/ImageSlider/Command/DeleteImageSliderImageFileCommand.php +++ /dev/null @@ -1,25 +0,0 @@ -imageSliderImage = $imageSliderImage; - } - - public function getImageSliderImage(): ImageSliderImage - { - return $this->imageSliderImage; - } -} diff --git a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderHandler.php b/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderHandler.php index f146fb0..fe03ffc 100644 --- a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderHandler.php +++ b/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderHandler.php @@ -5,20 +5,46 @@ namespace Oksydan\IsImageslider\Domain\ImageSlider\CommandHandler; use Oksydan\IsImageslider\Domain\ImageSlider\Command\DeleteImageSliderCommand; +use Oksydan\IsImageslider\Domain\ImageSlider\Command\DeleteImageSliderImageFileCommand; +use Oksydan\IsImageslider\Entity\ImageSliderImage; +use Oksydan\IsImageslider\Helper\EraseHelper; use Oksydan\IsImageslider\Repository\ImageSliderRepository; final class DeleteImageSliderHandler implements DeleteImageSliderHandlerInterface { private ImageSliderRepository $imageSliderRepository; - public function __construct(ImageSliderRepository $imageSliderRepository) - { + private EraseHelper $eraseHelper; + + public function __construct( + ImageSliderRepository $imageSliderRepository, + EraseHelper $eraseHelper + ) { $this->imageSliderRepository = $imageSliderRepository; + $this->eraseHelper = $eraseHelper; } public function handle(DeleteImageSliderCommand $command) { $imageSlider = $command->getImageSlider(); + + $sliderLangs = $imageSlider->getSliderLangs(); + + foreach ($sliderLangs as $sliderLang) { + if (null !== $sliderLang->getImage()) { + $this->deleteImage($sliderLang->getImage()); + } + + if (null !== $sliderLang->getImageMobile()) { + $this->deleteImage($sliderLang->getImageMobile()); + } + } + $this->imageSliderRepository->delete($imageSlider); } + + private function deleteImage(ImageSliderImage $image) + { + $this->eraseHelper->remove($image->getName()); + } } diff --git a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandler.php b/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandler.php deleted file mode 100644 index 1914c3b..0000000 --- a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandler.php +++ /dev/null @@ -1,25 +0,0 @@ -eraseHelper = $eraseHelper; - } - - public function handle(DeleteImageSliderImageFileCommand $command) - { - $imageSliderImage = $command->getImageSliderImage(); - - $this->eraseHelper->remove($imageSliderImage->getName()); - } -} diff --git a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandlerInterface.php b/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandlerInterface.php deleted file mode 100644 index c0dea2e..0000000 --- a/src/Domain/ImageSlider/CommandHandler/DeleteImageSliderImageFileHandlerInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -commandBus = $commandBus; - } - - public function postRemove(ImageSliderImage $imageSliderImage, LifecycleEventArgs $args): void - { - $this->commandBus->handle(new DeleteImageSliderImageFileCommand($imageSliderImage)); - } -} diff --git a/src/Entity/ImageSlider.php b/src/Entity/ImageSlider.php index fc7d10e..9d3fb1c 100644 --- a/src/Entity/ImageSlider.php +++ b/src/Entity/ImageSlider.php @@ -63,7 +63,7 @@ class ImageSlider private bool $image_to_all_langs; /** - * @ORM\OneToMany(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderLang", cascade={"persist", "remove"}, mappedBy="imageSlide") + * @ORM\OneToMany(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderLang", mappedBy="imageSlider", cascade={"persist", "remove"}) */ private Collection $sliderLang; diff --git a/src/Entity/ImageSliderImage.php b/src/Entity/ImageSliderImage.php index 743e541..ba715e4 100644 --- a/src/Entity/ImageSliderImage.php +++ b/src/Entity/ImageSliderImage.php @@ -4,11 +4,12 @@ namespace Oksydan\IsImageslider\Entity; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity - * * @ORM\Table() */ class ImageSliderImage @@ -17,13 +18,21 @@ class ImageSliderImage * @var int * * @ORM\Id - * * @ORM\Column(name="id_image", type="integer") - * * @ORM\GeneratedValue(strategy="AUTO") */ private int $id; + /** + * @ORM\OneToMany(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderLang", mappedBy="image", fetch="EAGER", cascade={"persist", "remove"}) + */ + private Collection $imageSliderLangs; + + /** + * @ORM\OneToMany(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderLang", mappedBy="imageMobile", fetch="EAGER", cascade={"persist", "remove"}) + */ + private Collection $imageMobileSliderLangs; + /** * @var string * @@ -31,9 +40,12 @@ class ImageSliderImage */ private string $name; - /** - * @return int - */ + public function __construct() + { + $this->imageSliderLangs = new ArrayCollection(); + $this->imageMobileSliderLangs = new ArrayCollection(); + } + public function getId(): int { return $this->id; @@ -53,4 +65,65 @@ public function setName(?string $name): void { $this->name = $name ?? ''; } + + + /** + * @return Collection|ImageSliderLang[] + */ + public function getImageSliderLangs(): Collection + { + return $this->imageSliderLangs; + } + + public function addImageSliderLang(ImageSliderLang $imageSliderLang): self + { + if (!$this->imageSliderLangs->contains($imageSliderLang)) { + $this->imageSliderLangs[] = $imageSliderLang; + $imageSliderLang->setImage($this); + } + + return $this; + } + + public function removeImageSliderLang(ImageSliderLang $imageSliderLang): self + { + if ($this->imageSliderLangs->removeElement($imageSliderLang)) { + // set the owning side to null (unless already changed) + if ($imageSliderLang->getImage() === $this) { + $imageSliderLang->setImage(null); + } + } + + return $this; + } + + /** + * @return Collection|ImageSliderLang[] + */ + public function getImageMobileSliderLangs(): Collection + { + return $this->imageMobileSliderLangs; + } + + public function addImageMobileSliderLang(ImageSliderLang $imageMobileSliderLang): self + { + if (!$this->imageMobileSliderLangs->contains($imageMobileSliderLang)) { + $this->imageMobileSliderLangs[] = $imageMobileSliderLang; + $imageMobileSliderLang->setImageMobile($this); + } + + return $this; + } + + public function removeImageMobileSliderLang(ImageSliderLang $imageMobileSliderLang): self + { + if ($this->imageMobileSliderLangs->removeElement($imageMobileSliderLang)) { + // set the owning side to null (unless already changed) + if ($imageMobileSliderLang->getImageMobile() === $this) { + $imageMobileSliderLang->setImageMobile(null); + } + } + + return $this; + } } diff --git a/src/Entity/ImageSliderLang.php b/src/Entity/ImageSliderLang.php index 9e43dc0..cc8b571 100644 --- a/src/Entity/ImageSliderLang.php +++ b/src/Entity/ImageSliderLang.php @@ -9,7 +9,6 @@ /** * @ORM\Table() - * * @ORM\Entity */ class ImageSliderLang @@ -18,20 +17,16 @@ class ImageSliderLang * @var ImageSlider * * @ORM\Id - * - * @ORM\ManyToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSlider", inversedBy="imageSlideLang") - * + * @ORM\ManyToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSlider", inversedBy="sliderLang") * @ORM\JoinColumn(name="id_slide", referencedColumnName="id_slide", nullable=false) */ - private ImageSlider $imageSlide; + private ImageSlider $imageSlider; /** * @var Lang * * @ORM\Id - * * @ORM\ManyToOne(targetEntity="PrestaShopBundle\Entity\Lang") - * * @ORM\JoinColumn(name="id_lang", referencedColumnName="id_lang", nullable=false, onDelete="CASCADE") */ private Lang $lang; @@ -65,27 +60,25 @@ class ImageSliderLang private string $description; /** - * @ORM\OneToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderImage", cascade={"persist", "remove"}, mappedBy="imageSlideLang", fetch="EAGER") - * + * @ORM\ManyToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderImage", inversedBy="imageSliderLangs", cascade={"persist", "remove"}, fetch="EAGER") * @ORM\JoinColumn(name="image", referencedColumnName="id_image", nullable=true) */ - private ImageSliderImage $image; + private ?ImageSliderImage $image = null; /** - * @ORM\OneToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderImage", cascade={"persist", "remove"}, mappedBy="imageSlideLang", fetch="EAGER") - * + * @ORM\ManyToOne(targetEntity="Oksydan\IsImageslider\Entity\ImageSliderImage", inversedBy="imageMobileSliderLangs", cascade={"persist", "remove"}, fetch="EAGER") * @ORM\JoinColumn(name="image_mobile", referencedColumnName="id_image", nullable=true) */ - private ImageSliderImage $imageMobile; + private ?ImageSliderImage $imageMobile = null; public function getImageSlider(): ImageSlider { - return $this->imageSlide; + return $this->imageSlider; } - public function setImageSlider(ImageSlider $imageSlide): void + public function setImageSlider(ImageSlider $imageSlider): void { - $this->imageSlide = $imageSlide; + $this->imageSlider = $imageSlider; } public function getTitle(): string @@ -131,33 +124,33 @@ public function setDescription(?string $description): void $this->description = $description; } - public function getImage(): ImageSliderImage + public function getLang(): Lang { - return $this->image; + return $this->lang; } - public function setImage(ImageSliderImage $image): void + public function setLang(Lang $lang): void { - $this->image = $image; + $this->lang = $lang; } - public function getImageMobile(): ImageSliderImage + public function getImage(): ?ImageSliderImage { - return $this->imageMobile; + return $this->image; } - public function setImageMobile(ImageSliderImage $imageMobile): void + public function setImage(?ImageSliderImage $image): void { - $this->imageMobile = $imageMobile; + $this->image = $image; } - public function getLang(): Lang + public function getImageMobile(): ?ImageSliderImage { - return $this->lang; + return $this->imageMobile; } - public function setLang(Lang $lang): void + public function setImageMobile(?ImageSliderImage $imageMobile): void { - $this->lang = $lang; + $this->imageMobile = $imageMobile; } }