From 02870a93f4251d2e9ca4987984e8d1f86ee22f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Fri, 5 Jan 2024 23:53:58 +0100 Subject: [PATCH] slider toggle activity refacto --- src/Controller/ImagesliderController.php | 26 ++++++++------- .../Slide/ToggleSlideActivityHandler.php | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 src/Handler/Slide/ToggleSlideActivityHandler.php diff --git a/src/Controller/ImagesliderController.php b/src/Controller/ImagesliderController.php index 2456ad7..885ded1 100644 --- a/src/Controller/ImagesliderController.php +++ b/src/Controller/ImagesliderController.php @@ -5,10 +5,11 @@ namespace Oksydan\IsImageslider\Controller; use Oksydan\IsImageslider\Cache\TemplateCache; -use Oksydan\IsImageslider\Entity\ImageSlider; use Oksydan\IsImageslider\Exceptions\DateRangeNotValidException; use Oksydan\IsImageslider\Filter\ImageSliderFileters; use Oksydan\IsImageslider\Handler\Slide\DeleteSlideHandler; +use Oksydan\IsImageslider\Handler\Slide\ToggleSlideActivityHandler; +use Oksydan\IsImageslider\Repository\ImageSliderRepository; use Oksydan\IsImageslider\Translations\TranslationDomains; use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface; use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Builder\FormBuilderInterface; @@ -41,6 +42,10 @@ class ImagesliderController extends FrameworkBundleAdminController private TranslatorInterface $translator; + private ImageSliderRepository $imageSliderRepository; + + private ToggleSlideActivityHandler $toggleSlideActivityHandler; + public function __construct( TemplateCache $templateCache, DeleteSlideHandler $deleteSlideHandler, @@ -49,7 +54,9 @@ public function __construct( GridPresenter $gridPresenter, FormBuilderInterface $imagesliderFormBuilder, IdentifiableObjectFormHandlerInterface $imagesliderFormHandler, - TranslatorInterface $translator + TranslatorInterface $translator, + ImageSliderRepository $imageSliderRepository, + ToggleSlideActivityHandler $toggleSlideActivityHandler ) { parent::__construct(); $this->templateCache = $templateCache; @@ -60,6 +67,8 @@ public function __construct( $this->imagesliderFormBuilder = $imagesliderFormBuilder; $this->imagesliderFormHandler = $imagesliderFormHandler; $this->translator = $translator; + $this->imageSliderRepository = $imageSliderRepository; + $this->toggleSlideActivityHandler = $toggleSlideActivityHandler; } public function index(ImageSliderFileters $filters): Response @@ -136,9 +145,7 @@ public function edit(Request $request, int $slideId): Response public function delete(Request $request, int $slideId): Response { - $imageSlide = $this->getDoctrine() - ->getRepository(ImageSlider::class) - ->find($slideId); + $imageSlide = $this->imageSliderRepository->find($slideId); if (!empty($imageSlide)) { $this->deleteSlideHandler->handle($imageSlide); @@ -206,10 +213,7 @@ public function saveConfiguration(Request $request): Response */ public function toggleStatus(Request $request, int $slideId): Response { - $entityManager = $this->get('doctrine.orm.entity_manager'); - $imageSlide = $this->getDoctrine() - ->getRepository(ImageSlider::class) - ->findOneBy(['id' => $slideId]); + $imageSlide = $this->imageSliderRepository->find($slideId); if (empty($imageSlide)) { return $this->json([ @@ -219,9 +223,7 @@ public function toggleStatus(Request $request, int $slideId): Response } try { - $imageSlide->setActive(!$imageSlide->getActive()); - $entityManager->flush(); - $this->clearTemplateCache(); + $this->toggleSlideActivityHandler->handle($imageSlide); $response = [ 'status' => true, diff --git a/src/Handler/Slide/ToggleSlideActivityHandler.php b/src/Handler/Slide/ToggleSlideActivityHandler.php new file mode 100644 index 0000000..8a08480 --- /dev/null +++ b/src/Handler/Slide/ToggleSlideActivityHandler.php @@ -0,0 +1,33 @@ +entityManager = $entityManager; + $this->templateCache = $templateCache; + } + + public function handle(ImageSlider $imageSlider): void + { + $imageSlider->setActive(!$imageSlider->getActive()); + + $this->entityManager->persist($imageSlider); + $this->entityManager->flush(); + $this->templateCache->clearTemplateCache(); + } +}