From 8ff95791c074afc1224a0e28aeddf503154e8bcb Mon Sep 17 00:00:00 2001 From: lo4k Date: Tue, 13 May 2025 09:52:48 +0200 Subject: [PATCH 1/4] WIP: Thelia 2.6 --- Config/module.xml | 4 +- Config/routing.xml | 9 --- Controller/StockAlertBackOfficeController.php | 54 ++++++++------- .../StockAlertFrontOfficeController.php | 69 +++++++++++-------- Twig/Plugins/StockAlertPluginTwig.php | 55 +++++++++++++++ .../default/product-details-bottom.html | 29 -------- .../default/product-details-bottom.html.twig | 35 ++++++++++ ...oduct.javascript-initialization.html.twig} | 0 8 files changed, 162 insertions(+), 93 deletions(-) create mode 100644 Twig/Plugins/StockAlertPluginTwig.php delete mode 100644 templates/frontOffice/default/product-details-bottom.html create mode 100644 templates/frontOffice/default/product-details-bottom.html.twig rename templates/frontOffice/default/{product.javascript-initialization.html => product.javascript-initialization.html.twig} (100%) diff --git a/Config/module.xml b/Config/module.xml index 996524a..54a0ba5 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,12 +13,12 @@ en_US fr_FR - 2.0.2 + 2.0.3 Julien Chanséaume julien@thelia.net classic - 2.5.0 + 2.6.0 rc diff --git a/Config/routing.xml b/Config/routing.xml index bb4b0b7..b2ce795 100644 --- a/Config/routing.xml +++ b/Config/routing.xml @@ -3,13 +3,4 @@ - - - diff --git a/Controller/StockAlertBackOfficeController.php b/Controller/StockAlertBackOfficeController.php index 9edd4fc..df9bc04 100644 --- a/Controller/StockAlertBackOfficeController.php +++ b/Controller/StockAlertBackOfficeController.php @@ -12,7 +12,7 @@ namespace StockAlert\Controller; - +use Propel\Runtime\Exception\PropelException; use StockAlert\Form\StockAlertConfig; use StockAlert\Model\RestockingAlertQuery; use StockAlert\StockAlert; @@ -20,39 +20,34 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Session; use Thelia\Controller\Admin\BaseAdminController; +use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Template\ParserContext; use Thelia\Form\Exception\FormValidationException; use Thelia\Model\ConfigQuery; use Symfony\Component\Routing\Annotation\Route; use Thelia\Tools\URL; -/** - * @Route("/admin/module/stockalert", name="stockalert_back") - * Class StockAlertBackOfficeController - * @package StockAlert\Controller - * @author Baixas Alban - * @author Julien Chanséaume - */ +#[Route(path: '/admin/module/stockalert', name: 'stockalert_back')] class StockAlertBackOfficeController extends BaseAdminController { - /** - * @Route("/configuration", name="_configuration", methods="POST") + * @param ParserContext $parserContext + * @return Response|RedirectResponse|string|\Symfony\Component\HttpFoundation\Response|null */ - public function configuration(ParserContext $parserContext) + #[Route(path: '/configuration', name: '_configuration', methods: ['POST'])] + public function configuration(ParserContext $parserContext): Response|RedirectResponse|string|\Symfony\Component\HttpFoundation\Response|null { $errorMessage = null; - $form = $this->createForm(StockAlertConfig::getName()); try { $configForm = $this->validateForm($form)->getData(); - ConfigQuery::write(StockAlert::CONFIG_ENABLED, $configForm['enabled']); + ConfigQuery::write(StockAlert::CONFIG_ENABLED, $configForm['enabled']); ConfigQuery::write(StockAlert::CONFIG_THRESHOLD, $configForm['threshold']); $emails = str_replace(' ', '', $configForm['emails']); - ConfigQuery::write(StockAlert::CONFIG_EMAILS, $emails); - ConfigQuery::write(StockAlert::CONFIG_NOTIFY, $configForm['notify']); + ConfigQuery::write(StockAlert::CONFIG_EMAILS, $emails); + ConfigQuery::write(StockAlert::CONFIG_NOTIFY, $configForm['notify']); return $this->generateSuccessRedirect($form); } catch (FormValidationException $e) { @@ -68,23 +63,36 @@ public function configuration(ParserContext $parserContext) ->setGeneralError($errorMessage); return $this->render( - "module-configure", + 'module-configure', [ - "module_code" => StockAlert::getModuleCode() + 'module_code' => StockAlert::getModuleCode(), ] ); } /** - * @Route("/delete", name="_delete", methods="GET") + * @param RequestStack $requestStack + * @param Session $session + * @return RedirectResponse + * @throws PropelException */ - public function deleteEmail(RequestStack $requestStack, Session $session) + #[Route(path: '/delete', name: '_delete', methods: ['GET'])] + public function deleteEmail(RequestStack $requestStack, Session $session): RedirectResponse { - $restockingAlertId = $requestStack->getCurrentRequest()->get("id"); + $restockingAlertId = $requestStack->getCurrentRequest()->get('id'); + if ($restockingAlertId) { - $restockingAlert = RestockingAlertQuery::create()->filterById($restockingAlertId)->findOne(); - $restockingAlert->delete(); + $restockingAlert = RestockingAlertQuery::create() + ->filterById($restockingAlertId) + ->findOne(); + + if (null !== $restockingAlert) { + $restockingAlert->delete(); + } } - return new RedirectResponse(URL::getInstance()->absoluteUrl($session->getReturnToUrl())); + + return new RedirectResponse( + URL::getInstance()->absoluteUrl($session->getReturnToUrl()) + ); } } diff --git a/Controller/StockAlertFrontOfficeController.php b/Controller/StockAlertFrontOfficeController.php index 978cf64..7b93efa 100644 --- a/Controller/StockAlertFrontOfficeController.php +++ b/Controller/StockAlertFrontOfficeController.php @@ -20,41 +20,50 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Thelia\Controller\Front\BaseFrontController; +use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Translation\Translator; -use Thelia\Form\Exception\FormValidationException; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route("/module/stockalert", name="stockalert_front") - * Class RestockingAlertFrontOfficeController - * @package StockAlert\Controller - * @author Baixas Alban - * @author Julien Chanséaume - */ +#[Route(path: "/module/stockalert", name: "stockalert_front")] class StockAlertFrontOfficeController extends BaseFrontController { - /** - * @Route("/subscribe", name="_subscribe", methods="POST") + * @param EventDispatcherInterface $eventDispatcher + * @param RequestStack $requestStack + * @return RedirectResponse|Response + * @throws \JsonException */ - public function subscribe(EventDispatcherInterface $eventDispatcher, RequestStack $requestStack) + #[Route(path: "/subscribe", name: "_subscribe", methods: ["POST"])] + public function subscribe( + EventDispatcherInterface $eventDispatcher, + RequestStack $requestStack + ): Response|RedirectResponse { $success = true; + $request = $requestStack->getCurrentRequest(); - $form = $this->createForm(StockAlertSubscribe::getName(), FormType::class, [], ['csrf_protection' => false]); + $form = $this->createForm( + StockAlertSubscribe::getName(), + FormType::class, + [], + ['csrf_protection' => false] + ); try { - $subscribeForm = $this->validateForm($form)->getData(); + $data = $this->validateForm($form)->getData(); - $subscriberEvent = new StockAlertEvent( - $subscribeForm['product_sale_elements_id'], - $subscribeForm['email'], - $subscribeForm['newsletter'], - $requestStack->getCurrentRequest()->getSession()->getLang()->getLocale() + $event = new StockAlertEvent( + $data['product_sale_elements_id'], + $data['email'], + $data['newsletter'] ?? false, + $request?->getSession()->getLang()->getLocale() ); - $eventDispatcher->dispatch($subscriberEvent, StockAlertEvents::STOCK_ALERT_SUBSCRIBE); + $eventDispatcher->dispatch( + $event, + StockAlertEvents::STOCK_ALERT_SUBSCRIBE + ); $message = Translator::getInstance()->trans( "C’est noté ! Vous recevrez un e-mail dès que le produit sera de nouveau en stock.", @@ -66,18 +75,18 @@ public function subscribe(EventDispatcherInterface $eventDispatcher, RequestStac $message = $e->getMessage(); } - if (!$requestStack->getCurrentRequest()->isXmlHttpRequest()) { - $requestStack->getCurrentRequest()->getSession()->getFlashBag()->set('flashMessage', $message); - return RedirectResponse::create($requestStack->getCurrentRequest()->get('stockalert_subscribe_form')['success_url']); + if (!$request?->isXmlHttpRequest()) { + $request?->getSession() + ->getFlashBag() + ->set('flashMessage', $message); + + $redirectUrl = $data['success_url'] ?? $this->generateUrl('homepage'); + return new RedirectResponse($redirectUrl); } - return $this->jsonResponse( - json_encode( - [ - "success" => $success, - "message" => $message - ] - ) - ); + return $this->jsonResponse(json_encode([ + 'success' => $success, + 'message' => $message, + ], JSON_THROW_ON_ERROR)); } } diff --git a/Twig/Plugins/StockAlertPluginTwig.php b/Twig/Plugins/StockAlertPluginTwig.php new file mode 100644 index 0000000..9402df3 --- /dev/null +++ b/Twig/Plugins/StockAlertPluginTwig.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SEOne\Twig\Plugins; + +use Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class StockAlertPluginTwig extends AbstractExtension +{ + public function __construct( + private readonly Environment $twig + ) {} + + public function getFunctions(): array + { + return [ + new TwigFunction('onProductDetailsBottom', [$this, 'getOnProductDetailsBottom']), + new TwigFunction('onProductJavascriptInitialization', [$this, 'getOnProductJavascriptInitialization']) + ]; + } + + /** + * @throws RuntimeError + * @throws SyntaxError + * @throws LoaderError + */ + public function getOnProductDetailsBottom(): string + { + return $this->twig->render('product-details-bottom.html.twig'); + } + + /** + * @throws SyntaxError + * @throws RuntimeError + * @throws LoaderError + */ + public function getOnProductJavascriptInitialization(): string + { + return $this->twig->render('product-javascript-initialization.html.twig'); + } +} diff --git a/templates/frontOffice/default/product-details-bottom.html b/templates/frontOffice/default/product-details-bottom.html deleted file mode 100644 index e69fe9e..0000000 --- a/templates/frontOffice/default/product-details-bottom.html +++ /dev/null @@ -1,29 +0,0 @@ - diff --git a/templates/frontOffice/default/product-details-bottom.html.twig b/templates/frontOffice/default/product-details-bottom.html.twig new file mode 100644 index 0000000..d770b46 --- /dev/null +++ b/templates/frontOffice/default/product-details-bottom.html.twig @@ -0,0 +1,35 @@ + diff --git a/templates/frontOffice/default/product.javascript-initialization.html b/templates/frontOffice/default/product.javascript-initialization.html.twig similarity index 100% rename from templates/frontOffice/default/product.javascript-initialization.html rename to templates/frontOffice/default/product.javascript-initialization.html.twig From c320034c69a6754035f5ece27aee36a934b02849 Mon Sep 17 00:00:00 2001 From: lo4k Date: Tue, 13 May 2025 14:50:47 +0200 Subject: [PATCH 2/4] WIP : Add Resource API Platform --- Api/Resource/RestockingAlert.php | 156 +++++++++++++++++++++++++++++++ Loop/RestockingAlertLoop.php | 62 ++++++------ 2 files changed, 187 insertions(+), 31 deletions(-) create mode 100644 Api/Resource/RestockingAlert.php diff --git a/Api/Resource/RestockingAlert.php b/Api/Resource/RestockingAlert.php new file mode 100644 index 0000000..a16b361 --- /dev/null +++ b/Api/Resource/RestockingAlert.php @@ -0,0 +1,156 @@ + [self::GROUP_ADMIN_READ]], + denormalizationContext: ['groups' => [self::GROUP_ADMIN_WRITE]] +)] +#[ApiResource( + operations: [ + new Get( + uriTemplate: '/front/stock-alerts/{id}', + name: 'api_stock_alert_get_id_front', + provider: PropelItemProvider::class + ), + new GetCollection( + uriTemplate: '/front/stock-alerts', + name: 'api_stock_alert_get_collection_front', + provider: PropelCollectionProvider::class + ) + ], + normalizationContext: ['groups' => [self::GROUP_FRONT_READ]], + denormalizationContext: ['groups' => [self::GROUP_FRONT_WRITE]] +)] +class RestockingAlert +{ + public const GROUP_ADMIN_READ = 'admin:stock_alert:read'; + public const GROUP_ADMIN_WRITE = 'admin:stock_alert:write'; + public const GROUP_FRONT_READ = 'front:stock_alert:read'; + public const GROUP_FRONT_WRITE = 'front:stock_alert:write'; + + /** + * @var int|null + */ + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_FRONT_READ])] + public ?int $id = null; + + /** + * @var ProductSaleElements|null + */ + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + #[Relation(targetResource: ProductSaleElements::class)] + public ?ProductSaleElements $productSaleElements = null; + + /** + * @var string|null + */ + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + public ?string $email = null; + + /** + * @var string|null + */ + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + public ?string $locale = null; + + /** + * @return int|null + */ + public function getId(): ?int + { + return $this->id; + } + + /** + * @param int|null $id + * @return void + */ + public function setId(?int $id): void + { + $this->id = $id; + } + + /** + * @return ProductSaleElements|null + */ + public function getProductSaleElements(): ?ProductSaleElements + { + return $this->productSaleElements; + } + + /** + * @param ProductSaleElements|null $productSaleElements + * @return void + */ + public function setProductSaleElements(?ProductSaleElements $productSaleElements): void + { + $this->productSaleElements = $productSaleElements; + } + + /** + * @return string|null + */ + public function getEmail(): ?string + { + return $this->email; + } + + /** + * @param string|null $email + * @return void + */ + public function setEmail(?string $email): void + { + $this->email = $email; + } + + /** + * @return string|null + */ + public function getLocale(): ?string + { + return $this->locale; + } + + /** + * @param string|null $locale + * @return void + */ + public function setLocale(?string $locale): void + { + $this->locale = $locale; + } + + /** + * @return TableMap|null + */ + #[Ignore] public static function getPropelRelatedTableMap(): ?TableMap + { + return new RestockingAlertTableMap(); + } +} diff --git a/Loop/RestockingAlertLoop.php b/Loop/RestockingAlertLoop.php index 7e3807e..1700ce5 100644 --- a/Loop/RestockingAlertLoop.php +++ b/Loop/RestockingAlertLoop.php @@ -14,6 +14,7 @@ namespace StockAlert\Loop; use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\ActiveQuery\ModelCriteria; use StockAlert\Model\RestockingAlert; use StockAlert\Model\RestockingAlertQuery; use Thelia\Core\Template\Element\BaseLoop; @@ -32,36 +33,8 @@ */ class RestockingAlertLoop extends BaseLoop implements PropelSearchLoopInterface { - protected $timestampable = true; - /** - * @param LoopResult $loopResult - * - * @return LoopResult - */ - public function parseResults(LoopResult $loopResult) - { - /** @var RestockingAlert $item */ - foreach ($loopResult->getResultDataCollection() as $item) { - - $loopResultRow = new LoopResultRow($item); - - $loopResultRow - ->set("ID", $item->getId()) - ->set("PRODUCT_SALE_ELEMENTS_ID", $item->getProductSaleElementsId()) - ->set("EMAIL", $item->getEmail()) - ->set("LOCALE", $item->getLocale()) - ; - - $this->addOutputFields($loopResultRow, $item); - - $loopResult->addRow($loopResultRow); - } - - return $loopResult; - } - /** * Definition of loop arguments * @@ -86,7 +59,7 @@ public function parseResults(LoopResult $loopResult) * * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection */ - protected function getArgDefinitions() + protected function getArgDefinitions(): ArgumentCollection { return new ArgumentCollection( Argument::createIntListTypeArgument('id'), @@ -119,9 +92,9 @@ protected function getArgDefinitions() /** * this method returns a Propel ModelCriteria * - * @return \Propel\Runtime\ActiveQuery\ModelCriteria + * @return ModelCriteria */ - public function buildModelCriteria() + public function buildModelCriteria(): ModelCriteria { $query = RestockingAlertQuery::create(); @@ -183,4 +156,31 @@ public function buildModelCriteria() return $query; } + + /** + * @param LoopResult $loopResult + * + * @return LoopResult + */ + public function parseResults(LoopResult $loopResult): LoopResult + { + /** @var RestockingAlert $item */ + foreach ($loopResult->getResultDataCollection() as $item) { + + $loopResultRow = new LoopResultRow($item); + + $loopResultRow + ->set("ID", $item->getId()) + ->set("PRODUCT_SALE_ELEMENTS_ID", $item->getProductSaleElementsId()) + ->set("EMAIL", $item->getEmail()) + ->set("LOCALE", $item->getLocale()) + ; + + $this->addOutputFields($loopResultRow, $item); + + $loopResult->addRow($loopResultRow); + } + + return $loopResult; + } } From 0e15d6e44cb5661e0d35da673648838de848f0b0 Mon Sep 17 00:00:00 2001 From: lo4k Date: Wed, 11 Jun 2025 11:55:46 +0200 Subject: [PATCH 3/4] WIP : Thelia 2.6 v2 --- Api/Resource/RestockingAlert.php | 59 +++++++++++++++++++++++--------- Config/module.xml | 8 +++-- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Api/Resource/RestockingAlert.php b/Api/Resource/RestockingAlert.php index a16b361..d7289c5 100644 --- a/Api/Resource/RestockingAlert.php +++ b/Api/Resource/RestockingAlert.php @@ -3,28 +3,41 @@ namespace StockAlert\Api\Resource; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Delete; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Patch; +use ApiPlatform\Metadata\Post; use Propel\Runtime\Map\TableMap; use StockAlert\Model\Map\RestockingAlertTableMap; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Ignore; use Thelia\Api\Bridge\Propel\Attribute\Relation; -use Thelia\Api\Bridge\Propel\State\PropelCollectionProvider; -use Thelia\Api\Bridge\Propel\State\PropelItemProvider; use Thelia\Api\Resource\ProductSaleElements; +use Thelia\Api\Resource\PropelResourceInterface; +use Thelia\Api\Resource\PropelResourceTrait; #[ApiResource( operations: [ new Get( - uriTemplate: '/admin/stock-alerts/{id}', - name: 'api_stock_alert_get_id', - provider: PropelItemProvider::class + uriTemplate: '/admin/stock-alert/{id}', + name: 'api_stock_alert_get_id' ), new GetCollection( uriTemplate: '/admin/stock-alerts', - name: 'api_stock_alert_get_collection', - provider: PropelCollectionProvider::class + name: 'api_stock_alert_get_collection' + ), + new Post( + uriTemplate: '/admin/stock-alert', + name: 'api_stock_alert_post_id' + ), + new Delete( + uriTemplate: '/admin/stock-alert/{id}', + name: 'api_stock_alert_delete_id' + ), + new Patch( + uriTemplate: '/admin/stock-alert/{id}', + name: 'api_stock_alert_patch_id' ) ], normalizationContext: ['groups' => [self::GROUP_ADMIN_READ]], @@ -33,21 +46,33 @@ #[ApiResource( operations: [ new Get( - uriTemplate: '/front/stock-alerts/{id}', - name: 'api_stock_alert_get_id_front', - provider: PropelItemProvider::class + uriTemplate: '/front/stock-alert/{id}', + name: 'api_stock_alert_get_id_front' ), new GetCollection( uriTemplate: '/front/stock-alerts', - name: 'api_stock_alert_get_collection_front', - provider: PropelCollectionProvider::class + name: 'api_stock_alert_get_collection_front' + ), + new Post( + uriTemplate: '/front/stock-alert', + name: 'api_stock_alert_post_id_front' + ), + new Delete( + uriTemplate: '/front/stock-alert/{id}', + name: 'api_stock_alert_delete_id_front' + ), + new Patch( + uriTemplate: '/front/stock-alert/{id}', + name: 'api_stock_alert_patch_id_front' ) ], normalizationContext: ['groups' => [self::GROUP_FRONT_READ]], denormalizationContext: ['groups' => [self::GROUP_FRONT_WRITE]] )] -class RestockingAlert +class RestockingAlert implements PropelResourceInterface { + use PropelResourceTrait; + public const GROUP_ADMIN_READ = 'admin:stock_alert:read'; public const GROUP_ADMIN_WRITE = 'admin:stock_alert:write'; public const GROUP_FRONT_READ = 'front:stock_alert:read'; @@ -62,20 +87,20 @@ class RestockingAlert /** * @var ProductSaleElements|null */ - #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])] #[Relation(targetResource: ProductSaleElements::class)] public ?ProductSaleElements $productSaleElements = null; /** * @var string|null */ - #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])] public ?string $email = null; /** * @var string|null */ - #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ])] + #[Groups([self::GROUP_ADMIN_READ, self::GROUP_ADMIN_WRITE, self::GROUP_FRONT_READ, self::GROUP_FRONT_WRITE])] public ?string $locale = null; /** @@ -153,4 +178,4 @@ public function setLocale(?string $locale): void { return new RestockingAlertTableMap(); } -} +} \ No newline at end of file diff --git a/Config/module.xml b/Config/module.xml index 54a0ba5..0949e0e 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,12 +13,16 @@ en_US fr_FR - 2.0.3 + 2.0.4 Julien Chanséaume julien@thelia.net + + Loïc MO + lmo@openstudio.fr + classic - 2.6.0 + 2.5.0 rc From 72875241b416a9070c6ff3ecb032039b17d82ec4 Mon Sep 17 00:00:00 2001 From: lo4k Date: Wed, 11 Jun 2025 14:26:10 +0200 Subject: [PATCH 4/4] WIP : Thelia 2.6 #24 --- .../StockAlertFrontOfficeController.php | 36 +++++-------- Service/StockAlertSubscriptionService.php | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 Service/StockAlertSubscriptionService.php diff --git a/Controller/StockAlertFrontOfficeController.php b/Controller/StockAlertFrontOfficeController.php index 7b93efa..b15bba8 100644 --- a/Controller/StockAlertFrontOfficeController.php +++ b/Controller/StockAlertFrontOfficeController.php @@ -12,16 +12,12 @@ namespace StockAlert\Controller; -use StockAlert\Event\StockAlertEvent; -use StockAlert\Event\StockAlertEvents; use StockAlert\Form\StockAlertSubscribe; -use StockAlert\StockAlert; +use StockAlert\Service\StockAlertSubscriptionService; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Thelia\Controller\Front\BaseFrontController; use Thelia\Core\HttpFoundation\Response; -use Thelia\Core\Translation\Translator; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Annotation\Route; @@ -29,18 +25,17 @@ class StockAlertFrontOfficeController extends BaseFrontController { /** - * @param EventDispatcherInterface $eventDispatcher + * @param StockAlertSubscriptionService $subscriptionService * @param RequestStack $requestStack - * @return RedirectResponse|Response + * @return Response|RedirectResponse * @throws \JsonException */ #[Route(path: "/subscribe", name: "_subscribe", methods: ["POST"])] public function subscribe( - EventDispatcherInterface $eventDispatcher, + StockAlertSubscriptionService $subscriptionService, RequestStack $requestStack ): Response|RedirectResponse { - $success = true; $request = $requestStack->getCurrentRequest(); $form = $this->createForm( @@ -50,26 +45,19 @@ public function subscribe( ['csrf_protection' => false] ); + $data = []; + $success = true; + try { $data = $this->validateForm($form)->getData(); - $event = new StockAlertEvent( + $result = $subscriptionService->subscribe( $data['product_sale_elements_id'], $data['email'], - $data['newsletter'] ?? false, - $request?->getSession()->getLang()->getLocale() - ); - - $eventDispatcher->dispatch( - $event, - StockAlertEvents::STOCK_ALERT_SUBSCRIBE - ); - - $message = Translator::getInstance()->trans( - "C’est noté ! Vous recevrez un e-mail dès que le produit sera de nouveau en stock.", - [], - StockAlert::MESSAGE_DOMAIN + $data['newsletter'] ?? false ); + $success = $result['success']; + $message = $result['message']; } catch (\Exception $e) { $success = false; $message = $e->getMessage(); @@ -89,4 +77,4 @@ public function subscribe( 'message' => $message, ], JSON_THROW_ON_ERROR)); } -} +} \ No newline at end of file diff --git a/Service/StockAlertSubscriptionService.php b/Service/StockAlertSubscriptionService.php new file mode 100644 index 0000000..c66cd92 --- /dev/null +++ b/Service/StockAlertSubscriptionService.php @@ -0,0 +1,54 @@ +requestStack->getCurrentRequest(); + $locale = $request?->getSession()?->getLang()?->getLocale() ?? 'fr_FR'; + + $success = true; + + try { + $event = new StockAlertEvent( + $pseId, + $email, + $newsletter, + $locale + ); + + $this->eventDispatcher->dispatch($event, StockAlertEvents::STOCK_ALERT_SUBSCRIBE); + + $message = Translator::getInstance()->trans( + "C’est noté ! Vous recevrez un e-mail dès que le produit sera de nouveau en stock.", + [], + StockAlert::MESSAGE_DOMAIN + ); + } catch (\Exception $e) { + $success = false; + $message = $e->getMessage(); + } + + return [ + 'success' => $success, + 'message' => $message, + ]; + } +}