Skip to content

Commit

Permalink
Handle image delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Feb 29, 2024
1 parent baa2575 commit 6ff84db
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 139 deletions.
9 changes: 0 additions & 9 deletions config/admin/command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

This file was deleted.

30 changes: 28 additions & 2 deletions src/Domain/ImageSlider/CommandHandler/DeleteImageSliderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion src/Entity/ImageSlider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
85 changes: 79 additions & 6 deletions src/Entity/ImageSliderImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,23 +18,34 @@ 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
*
* @ORM\Column(name="name", type="text")
*/
private string $name;

/**
* @return int
*/
public function __construct()
{
$this->imageSliderLangs = new ArrayCollection();
$this->imageMobileSliderLangs = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
Expand All @@ -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;
}
}
Loading

0 comments on commit 6ff84db

Please sign in to comment.