Skip to content

Commit

Permalink
refacto part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Jan 4, 2024
1 parent 43aac42 commit 70c440a
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 128 deletions.
46 changes: 3 additions & 43 deletions config/admin/form.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,15 @@ services:
- '@prestashop.adapter.shop.context'
- '@prestashop.adapter.multistore_feature'

oksydan.is_imageslider.image_slider_configuration.form_provider:
class: 'Oksydan\IsImageslider\Form\Provider\ImageSliderConfigurationFormDataProvider'
arguments:
- '@oksydan.is_imageslider.image_slider.configuration'

oksydan.is_imageslider.image_slider_configuration.form_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@oksydan.is_imageslider.image_slider_configuration.form_provider'
- '@Oksydan\IsImageslider\Form\Provider\ImageSliderConfigurationFormDataProvider'
- 'Oksydan\IsImageslider\Form\ImageSliderConfigurationType'
- 'ConfigurationForm'

oksydan.is_imageslider.form.image_slider_type:
class: 'Oksydan\IsImageslider\Form\ImageSliderType'
parent: 'form.type.translatable.aware'
arguments:
- '@=service("prestashop.adapter.multistore_feature").isUsed()'
public: true
tags:
- { name: form.type }

oksydan.is_imageslider.form.image_slider_configuration_type:
class: 'Oksydan\IsImageslider\Form\ImageSliderConfigurationType'
parent: 'form.type.translatable.aware'
public: true
tags:
- { name: form.type }

Oksydan\IsImageslider\Type\TranslatableFile:
class: 'Oksydan\IsImageslider\Type\TranslatableFile'
Expand All @@ -43,41 +23,21 @@ services:
tags:
- { name: form.type }


oksydan.is_imageslider.provider.image_provider:
class: 'Oksydan\IsImageslider\Provider\ImageProvider'
public: true
arguments:
- '%is_imageslider_images_uri%'

oksydan.is_imageslider.form.identifiable_object.data_provider.image_slider_form_data_provider:
class: 'Oksydan\IsImageslider\Form\Provider\ImageSliderFormDataProvider'
arguments:
- '@oksydan.is_imageslider.repository.image_slider'
- '@oksydan.is_imageslider.provider.image_provider'
- '@prestashop.core.admin.lang.repository'
- '%is_imageslider_placeholder_img%'
- '@prestashop.adapter.shop.context'

oksydan.is_imageslider.form.identifiable_object.builder.image_slider_form_builder:
class: 'PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Builder\FormBuilder'
factory: 'prestashop.core.form.builder.form_builder_factory:create'
arguments:
- 'Oksydan\IsImageslider\Form\ImageSliderType'
- '@oksydan.is_imageslider.form.identifiable_object.data_provider.image_slider_form_data_provider'

oksydan.is_imageslider.form.identifiable_object.data_handler.image_slider_form_data_handler:
class: 'Oksydan\IsImageslider\Form\DataHandler\ImageSliderFormDataHandler'
arguments:
- '@oksydan.is_imageslider.repository.image_slider'
- '@prestashop.core.admin.lang.repository'
- '@doctrine.orm.default_entity_manager'
- '@oksydan.is_imageslider.handler.file_uploader'
- '@oksydan.is_imageslider.handler.file_eraser'
- "@=service('prestashop.adapter.legacy.context').getLanguages()"
- '@Oksydan\IsImageslider\Form\Provider\ImageSliderFormDataProvider'

oksydan.is_imageslider.form.identifiable_object.handler.image_slider_form_handler:
class: 'PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler\FormHandler'
factory: 'prestashop.core.form.identifiable_object.handler.form_handler_factory:create'
arguments:
- '@oksydan.is_imageslider.form.identifiable_object.data_handler.image_slider_form_data_handler'
- '@Oksydan\IsImageslider\Form\DataHandler\ImageSliderFormDataHandler'
10 changes: 1 addition & 9 deletions config/admin/grid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ services:
parent: 'prestashop.core.grid.definition.factory.abstract_grid_definition'
public: true

oksydan.is_imageslider.grid.query.image_slider_query_builder:
class: 'Oksydan\IsImageslider\Grid\Query\ImageSliderQueryBuilder'
parent: 'prestashop.core.grid.abstract_query_builder'
public: true
arguments:
- '@prestashop.adapter.shop.context'
- "@=service('prestashop.adapter.legacy.context').getContext().language.id"

oksydan.is_imageslider.grid.image_slider_grid_factory:
class: 'PrestaShop\PrestaShop\Core\Grid\GridFactory'
arguments:
Expand All @@ -25,7 +17,7 @@ services:
oksydan.is_imageslider.grid.data.factory.image_slider_data_factory:
class: '%prestashop.core.grid.data.factory.doctrine_grid_data_factory%'
arguments:
- '@oksydan.is_imageslider.grid.query.image_slider_query_builder'
- '@Oksydan\IsImageslider\Grid\Query\ImageSliderQueryBuilder'
- '@prestashop.core.hook.dispatcher'
- '@prestashop.core.grid.query.doctrine_query_parser'
- 'is_imageslider'
Expand Down
9 changes: 9 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ services:
- '../../src/*/index.php'
- '../../src/**/index.php'

Oksydan\IsImageslider\Controller\ImagesliderController:
class: Oksydan\IsImageslider\Controller\ImagesliderController
arguments:
$imagsliderGridFactory: '@oksydan.is_imageslider.grid.image_slider_grid_factory'
$imagesliderConfigurationFormHandler: '@oksydan.is_imageslider.image_slider_configuration.form_handler'
$imagesliderFormBuilder: '@oksydan.is_imageslider.form.identifiable_object.builder.image_slider_form_builder'
$imagesliderFormHandler: '@oksydan.is_imageslider.form.identifiable_object.handler.image_slider_form_handler'
$gridPresenter: '@prestashop.core.grid.presenter.grid_presenter'

1 change: 1 addition & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ services:
exclude:
- '../../src/index.php'
- '../../src/*/index.php'
- '../../src/**/index.php'
138 changes: 64 additions & 74 deletions src/Controller/ImagesliderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,65 @@
use Oksydan\IsImageslider\Entity\ImageSlider;
use Oksydan\IsImageslider\Exceptions\DateRangeNotValidException;
use Oksydan\IsImageslider\Filter\ImageSliderFileters;
use Oksydan\IsImageslider\Handler\FileEraser;
use Oksydan\IsImageslider\Handler\Slide\DeleteSlideHandler;
use Oksydan\IsImageslider\Translations\TranslationDomains;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Builder\FormBuilderInterface;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler\FormHandlerInterface as IdentifiableObjectFormHandlerInterface;
use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface;
use PrestaShop\PrestaShop\Core\Grid\GridInterface;
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionDataException;
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionUpdateException;
use PrestaShop\PrestaShop\Core\Grid\Presenter\GridPresenter;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Entity\Shop;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;

class ImagesliderController extends FrameworkBundleAdminController
{
/**
* @var FileEraser
*/
private $fileEraser;
private TemplateCache $templateCache;

/**
* @var array
*/
private $languages;
private DeleteSlideHandler $deleteSlideHandler;

/**
* @var TemplateCache
*/
private $templateCache;
private GridFactoryInterface $imagsliderGridFactory;

public function __construct(FileEraser $fileEraser, $languages, TemplateCache $templateCache)
{
$this->fileEraser = $fileEraser;
$this->languages = $languages;
private FormHandlerInterface $imagesliderConfigurationFormHandler;

private GridPresenter $gridPresenter;

private FormBuilderInterface $imagesliderFormBuilder;

private IdentifiableObjectFormHandlerInterface $imagesliderFormHandler;

private TranslatorInterface $translator;

public function __construct(
TemplateCache $templateCache,
DeleteSlideHandler $deleteSlideHandler,
GridFactoryInterface $imagsliderGridFactory,
FormHandlerInterface $imagesliderConfigurationFormHandler,
GridPresenter $gridPresenter,
FormBuilderInterface $imagesliderFormBuilder,
IdentifiableObjectFormHandlerInterface $imagesliderFormHandler,
TranslatorInterface $translator
) {
parent::__construct();
$this->templateCache = $templateCache;
$this->deleteSlideHandler = $deleteSlideHandler;
$this->imagsliderGridFactory = $imagsliderGridFactory;
$this->imagesliderConfigurationFormHandler = $imagesliderConfigurationFormHandler;
$this->gridPresenter = $gridPresenter;
$this->imagesliderFormBuilder = $imagesliderFormBuilder;
$this->imagesliderFormHandler = $imagesliderFormHandler;
$this->translator = $translator;
}

public function index(ImageSliderFileters $filters): Response
{
$imageSliderGridFactory = $this->get('oksydan.is_imageslider.grid.image_slider_grid_factory');
$imageSliderGrid = $imageSliderGridFactory->getGrid($filters);
$imageSliderGrid = $this->imagsliderGridFactory->getGrid($filters);

$configurationForm = $this->get('oksydan.is_imageslider.image_slider_configuration.form_handler')->getForm();
$configurationForm = $this->imagesliderConfigurationFormHandler->getForm();

return $this->render('@Modules/is_imageslider/views/templates/admin/index.html.twig', [
'translationDomain' => TranslationDomains::TRANSLATION_DOMAIN_ADMIN,
Expand All @@ -58,14 +78,11 @@ public function index(ImageSliderFileters $filters): Response

public function create(Request $request): Response
{
$formDataHandler = $this->get('oksydan.is_imageslider.form.identifiable_object.builder.image_slider_form_builder');
$form = $formDataHandler->getForm();
$form = $this->imagesliderFormBuilder->getForm();
$form->handleRequest($request);

$formHandler = $this->get('oksydan.is_imageslider.form.identifiable_object.handler.image_slider_form_handler');

try {
$result = $formHandler->handle($form);
$result = $this->imagesliderFormHandler->handle($form);

if (null !== $result->getIdentifiableObjectId()) {
$this->addFlash(
Expand All @@ -90,14 +107,11 @@ public function create(Request $request): Response

public function edit(Request $request, int $slideId): Response
{
$formBuilder = $this->get('oksydan.is_imageslider.form.identifiable_object.builder.image_slider_form_builder');
$form = $formBuilder->getFormFor((int) $slideId);
$form = $this->imagesliderFormBuilder->getFormFor($slideId);
$form->handleRequest($request);

$formHandler = $this->get('oksydan.is_imageslider.form.identifiable_object.handler.image_slider_form_handler');

try {
$result = $formHandler->handleFor($slideId, $form);
$result = $this->imagesliderFormHandler->handleFor($slideId, $form);

if (null !== $result->getIdentifiableObjectId()) {
$this->addFlash(
Expand Down Expand Up @@ -127,43 +141,8 @@ public function delete(Request $request, int $slideId): Response
->find($slideId);

if (!empty($imageSlide)) {
$multistoreContext = $this->get('prestashop.adapter.shop.context');
$entityManager = $this->get('doctrine.orm.entity_manager');

if ($multistoreContext->isAllShopContext()) {
$imageSlide->clearShops();

foreach ($this->languages as $language) {
$langId = (int) $language['id_lang'];
$imageSliderLang = $imageSlide->getImageSliderLangByLangId($langId);

if ($imageSliderLang->getImage()) {
$this->eraseFile($imageSliderLang->getImage());
}

if ($imageSliderLang->getImageMobile()) {
$this->eraseFile($imageSliderLang->getImageMobile());
}
}

$entityManager->remove($imageSlide);
} else {
$shopList = $this->getDoctrine()
->getRepository(Shop::class)
->findBy(['id' => $multistoreContext->getContextListShopID()]);

foreach ($shopList as $shop) {
$imageSlide->removeShop($shop);
$entityManager->flush();
}

if (count($imageSlide->getShops()) === 0) {
$entityManager->remove($imageSlide);
}
}
$this->deleteSlideHandler->handle($imageSlide);

$this->clearTemplateCache();
$entityManager->flush();
$this->addFlash(
'success',
$this->trans('Successful deletion.', 'Admin.Notifications.Success')
Expand All @@ -189,7 +168,7 @@ public function saveConfiguration(Request $request): Response
{
$redirectResponse = $this->redirectToRoute('is_imageslider_controller');

$form = $this->get('oksydan.is_imageslider.image_slider_configuration.form_handler')->getForm();
$form = $this->imagesliderConfigurationFormHandler->getForm();
$form->handleRequest($request);

if (!$form->isSubmitted()) {
Expand All @@ -198,7 +177,7 @@ public function saveConfiguration(Request $request): Response

if ($form->isValid()) {
$data = $form->getData();
$saveErrors = $this->get('oksydan.is_imageslider.image_slider_configuration.form_handler')->save($data);
$saveErrors = $this->imagesliderConfigurationFormHandler->save($data);

if (0 === count($saveErrors)) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
Expand Down Expand Up @@ -228,7 +207,7 @@ public function saveConfiguration(Request $request): Response
public function toggleStatus(Request $request, int $slideId): Response
{
$entityManager = $this->get('doctrine.orm.entity_manager');
$imageSlide = $entityManager
$imageSlide = $this->getDoctrine()
->getRepository(ImageSlider::class)
->findOneBy(['id' => $slideId]);

Expand Down Expand Up @@ -288,14 +267,25 @@ public function updatePositionAction(Request $request): Response
return $this->redirectToRoute('is_imageslider_controller');
}

private function eraseFile(string $fileName): bool
private function clearTemplateCache()
{
return $this->fileEraser->remove($fileName);
$this->templateCache->clearTemplateCache();
}

private function clearTemplateCache()
/**
* @inerhitDoc
*/
protected function presentGrid(GridInterface $grid)
{
$this->templateCache->clearTemplateCache();
return $this->gridPresenter->present($grid);
}

/**
* @inerhitDoc
*/
protected function trans($key, $domain, array $parameters = [])
{
return $this->translator->trans($key, $parameters, $domain);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Grid/Query/ImageSliderQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct(
Connection $connection,
$dbPrefix,
Context $shopContext,
\Context $context)
{
\Context $context
) {
parent::__construct($connection, $dbPrefix);

$this->shopContext = $shopContext;
Expand Down
Loading

0 comments on commit 70c440a

Please sign in to comment.