From 5511ebfe421c5ba695b52089bf9c78a057f4aad2 Mon Sep 17 00:00:00 2001 From: Isabelle Date: Thu, 5 Mar 2026 09:37:45 +0100 Subject: [PATCH] fix(webservice): Polished code --- composer.json | 2 +- config/services.yml | 11 ++-------- controllers/front/ajax.php | 9 ++++---- src/Config/Config.php | 18 ++++++++++++++++ src/Entity/OrderReference.php | 4 ++-- src/Module/Install.php | 23 +++++++++++++-------- src/Module/actionHooks.php | 6 ++++-- src/Module/displayHooks.php | 4 ++-- src/Repository/OrderReferenceRepository.php | 4 ++-- ws_orderreference.php | 15 +++++--------- 10 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 src/Config/Config.php diff --git a/composer.json b/composer.json index e8d5ac3..4380c0b 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ ], "autoload": { "psr-4": { - "Websmid\\WsOrderReference\\": "src/" + "Websmid\\OrderReference\\": "src/" }, "classmap": [ "ws_orderreference.php", diff --git a/config/services.yml b/config/services.yml index 05d9fb3..09f3b18 100644 --- a/config/services.yml +++ b/config/services.yml @@ -4,15 +4,8 @@ services: autoconfigure: true # Repository - this makes it available via dependency injection - Websmid\WsOrderReference\Repository\OrderReferenceRepository: + Websmid\OrderReference\Repository\OrderReferenceRepository: public: true factory: ["@doctrine.orm.default_entity_manager", getRepository] arguments: - - Websmid\WsOrderReference\Entity\OrderReference - - # Database installer - Websmid\WsOrderReference\Database\ReferenceInstaller: - public: true - arguments: - - "@doctrine.dbal.default_connection" - - "%database_prefix%" + - Websmid\OrderReference\Entity\OrderReference diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 01bdd84..149389c 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -1,7 +1,7 @@ getRepository(OrderReference::class); - $orderRef = $repository->findByCartId($idCart); + $orderRef = $repository->findByCartId((int)$idCart); if (!$orderRef) { $orderRef = new OrderReference(); - $orderRef->setIdCart($idCart); + $orderRef->setIdCart((int)$idCart); } $orderRef->setOrderReference(pSQL($orderReference)); diff --git a/src/Config/Config.php b/src/Config/Config.php new file mode 100644 index 0000000..9b9b79d --- /dev/null +++ b/src/Config/Config.php @@ -0,0 +1,18 @@ + 'displayShoppingCart', + 'displayPDFDeliverySlip' => 'displayPDFDeliverySlip', + ]; + + const HOOKS = [ + 'displayShoppingCart', + 'displayPDFDeliverySlip', + 'addWebserviceResources', + 'actionFrontControllerSetMedia', + ]; +} diff --git a/src/Entity/OrderReference.php b/src/Entity/OrderReference.php index 9eb0a29..da2fd78 100644 --- a/src/Entity/OrderReference.php +++ b/src/Entity/OrderReference.php @@ -10,13 +10,13 @@ declare(strict_types=1); -namespace Websmid\WsOrderReference\Entity; +namespace Websmid\OrderReference\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table() - * @ORM\Entity(repositoryClass="Websmid\WsOrderReference\Repository\OrderReferenceRepository") + * @ORM\Entity(repositoryClass="Websmid\OrderReference\Repository\OrderReferenceRepository") */ class OrderReference { diff --git a/src/Module/Install.php b/src/Module/Install.php index 55ef929..f5d8987 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -1,8 +1,9 @@ execute($sql); + return Db::getInstance()->execute($sql); } - public function registerHooks(){ - return $this->registerHook('displayShoppingCart') - && $this->registerHook('displayPDFDeliverySlip') - && $this->registerHook('addWebserviceResources') - && $this->registerHook('actionFrontControllerSetMedia'); + public function registerHooks(): bool + { + foreach (Config::HOOKS as $hook) { + if (!$this->registerHook($hook)) { + return false; + } + } + + return true; } } diff --git a/src/Module/actionHooks.php b/src/Module/actionHooks.php index ac3ec65..136e246 100644 --- a/src/Module/actionHooks.php +++ b/src/Module/actionHooks.php @@ -1,12 +1,14 @@ $this->context->link->getModuleLink($this->name, 'ajax'), ]); diff --git a/src/Module/displayHooks.php b/src/Module/displayHooks.php index 96a31af..865eb76 100644 --- a/src/Module/displayHooks.php +++ b/src/Module/displayHooks.php @@ -1,6 +1,6 @@ id_order; - $orderReferenceRepo = \PrestaShop\PrestaShop\Adapter\SymfonyContainer::getInstance()->get('Websmid\WsOrderReference\Repository\OrderReferenceRepository'); + $orderReferenceRepo = \PrestaShop\PrestaShop\Adapter\SymfonyContainer::getInstance()->get('Websmid\OrderReference\Repository\OrderReferenceRepository'); $orderReference = $orderReferenceRepo->customRefByOrderId($orderId); $this->context->smarty->assign('orderReference', $orderReference); diff --git a/src/Repository/OrderReferenceRepository.php b/src/Repository/OrderReferenceRepository.php index cf385ee..60f0b43 100644 --- a/src/Repository/OrderReferenceRepository.php +++ b/src/Repository/OrderReferenceRepository.php @@ -9,10 +9,10 @@ declare(strict_types=1); -namespace Websmid\WsOrderReference\Repository; +namespace Websmid\OrderReference\Repository; use Doctrine\ORM\EntityRepository; -use Websmid\WsOrderReference\Entity\OrderReference; +use Websmid\OrderReference\Entity\OrderReference; class OrderReferenceRepository extends EntityRepository { diff --git a/ws_orderreference.php b/ws_orderreference.php index a56fd65..04ee30e 100644 --- a/ws_orderreference.php +++ b/ws_orderreference.php @@ -4,9 +4,9 @@ if (!defined('_PS_VERSION_')) { exit; } -use Websmid\WsOrderReference\Module\Install; -use Websmid\WsOrderReference\Module\DisplayHooks; -use Websmid\WsOrderReference\Module\ActionHooks; +use Websmid\OrderReference\Module\Install; +use Websmid\OrderReference\Module\DisplayHooks; +use Websmid\OrderReference\Module\ActionHooks; if (file_exists(__DIR__ . '/vendor/autoload.php')) { require_once __DIR__ . '/vendor/autoload.php'; @@ -27,18 +27,13 @@ class Ws_OrderReference extends Module $this->name = 'ws_orderreference'; $this->tab = 'other'; $this->version = '1.0.0'; - $this->author = 'Isabelle Oving-Anno | De Websmid b.v.'; + $this->author = 'Isabelle Oving-Anno | De Websmid B.V.'; $this->need_instance = 0; - $this->ps_versions_compliancy = [ - 'min' => '9.0.0', - 'max' => '9.99.99', - ]; - $this -> bootstrap = true; + $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Order Reference'); $this->description = $this->l('Adds an optional order reference field to the shopping cart summary.'); } - }