From fd6618acb6aed0db6e9269a3861dee1516c34002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Tue, 12 Dec 2023 01:58:16 +0100 Subject: [PATCH 1/2] multiple changes, refacto for autowire and autoconfigure --- config/admin/services.yml | 19 +++++ config/common.yml | 75 +------------------ config/front/services.yml | 42 +++++------ is_favoriteproducts.php | 2 +- .../FavoriteProductsSearchProvider.php | 9 ++- ...hp => FavoriteProductLegacyRepository.php} | 16 ++-- src/Repository/FavoriteProductRepository.php | 10 ++- ...sitory.php => ProductLegacyRepository.php} | 16 ++-- src/Services/FavoriteProductService.php | 20 ++--- 9 files changed, 83 insertions(+), 126 deletions(-) rename src/Repository/{FavoriteProductRepositoryLegacy.php => FavoriteProductLegacyRepository.php} (79%) rename src/Repository/{ProductRepository.php => ProductLegacyRepository.php} (85%) diff --git a/config/admin/services.yml b/config/admin/services.yml index 0857664..6bf3741 100644 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -3,4 +3,23 @@ imports: services: _defaults: + public: false + autoconfigure: true + autowire: true + bind: + $module: '@oksydan.is_favoriteproducts.module' + $context: '@oksydan.is_favoriteproducts.context' + $dbPrefix: '%database_prefix%' + + Oksydan\IsFavoriteProducts\: + resource: '../../src/*' + exclude: + - '../../src/{DTO,Entity}' + - '../../src/index.php' + - '../../src/*/index.php' + + Oksydan\IsFavoriteProducts\Hook\: public: true + resource: '../../src/Hook/*' + exclude: + - '../../src/Hook/index.php' diff --git a/config/common.yml b/config/common.yml index fb3cb71..f2022fc 100644 --- a/config/common.yml +++ b/config/common.yml @@ -1,6 +1,6 @@ services: _defaults: - public: true + public: false oksydan.is_favoriteproducts.module: class: Is_favoriteproducts @@ -9,73 +9,6 @@ services: arguments: - 'is_favoriteproducts' - Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository: - class: Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository - public: true - factory: ['@doctrine.orm.default_entity_manager', getRepository] - arguments: - - Oksydan\IsFavoriteProducts\Entity\FavoriteProduct - - Oksydan\IsFavoriteProducts\Repository\ProductRepository: - class: Oksydan\IsFavoriteProducts\Repository\ProductRepository - public: true - arguments: - - '@doctrine.dbal.default_connection' - - '%database_prefix%' - - Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepositoryLegacy: - class: Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepositoryLegacy - public: true - arguments: - - '@doctrine.dbal.default_connection' - - '%database_prefix%' - - Oksydan\IsFavoriteProducts\Repository\FavoriteProductLegacyRepository: - class: Oksydan\IsFavoriteProducts\Repository\FavoriteProductLegacyRepository - public: true - arguments: - - '@doctrine.dbal.default_connection' - - '%database_prefix%' - - Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository: - class: Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository - public: true - - Oksydan\IsFavoriteProducts\Presenter\FavoriteProductJsonPresenter: - class: Oksydan\IsFavoriteProducts\Presenter\FavoriteProductJsonPresenter - public: true - - Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper: - class: Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper - public: true - - Oksydan\IsFavoriteProducts\Services\FavoriteProductService: - class: Oksydan\IsFavoriteProducts\Services\FavoriteProductService - public: true - arguments: - - "@=service('prestashop.adapter.legacy.context').getContext()" - - '@Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository' - - '@Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository' - - '@Oksydan\IsFavoriteProducts\Repository\ProductRepository' - - '@Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepositoryLegacy' - - '@Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper' - - Oksydan\IsFavoriteProducts\Hook\AbstractHook: - class: Oksydan\IsFavoriteProducts\Hook\AbstractHook - public: true - arguments: - - '@oksydan.is_favoriteproducts.module' - - "@=service('prestashop.adapter.legacy.context').getContext()" - - '@Oksydan\IsFavoriteProducts\Services\FavoriteProductService' - - Oksydan\IsFavoriteProducts\Hook\AbstractDisplayHook: - class: Oksydan\IsFavoriteProducts\Hook\AbstractDisplayHook - parent: Oksydan\IsFavoriteProducts\Hook\AbstractHook - public: true - - Oksydan\IsFavoriteProducts\ProductSearchProvider\FavoriteProductsSearchProvider: - class: Oksydan\IsFavoriteProducts\ProductSearchProvider\FavoriteProductsSearchProvider - public: true - arguments: - - '@Oksydan\IsFavoriteProducts\Services\FavoriteProductService' - - "@=service('prestashop.adapter.legacy.context').getContext().getTranslator()" + oksydan.is_favoriteproducts.context: + class: Context + factory: [ '\Context', getContext ] diff --git a/config/front/services.yml b/config/front/services.yml index 306aedd..be50066 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -3,31 +3,31 @@ imports: services: _defaults: - public: true - - Oksydan\IsFavoriteProducts\Hook\DisplayTop: - class: Oksydan\IsFavoriteProducts\Hook\DisplayTop - parent: Oksydan\IsFavoriteProducts\Hook\AbstractDisplayHook - public: true + public: false + autoconfigure: true + autowire: true + bind: + $module: '@oksydan.is_favoriteproducts.module' + $context: '@oksydan.is_favoriteproducts.context' + $dbPrefix: '%database_prefix%' - Oksydan\IsFavoriteProducts\Hook\DisplayProductListReviews: - class: Oksydan\IsFavoriteProducts\Hook\DisplayProductListReviews - parent: Oksydan\IsFavoriteProducts\Hook\AbstractDisplayHook - public: true + Oksydan\IsFavoriteProducts\: + resource: '../../src/*' + exclude: + - '../../src/{DTO,Entity}' + - '../../src/index.php' + - '../../src/*/index.php' - Oksydan\IsFavoriteProducts\Hook\DisplayProductActions: - class: Oksydan\IsFavoriteProducts\Hook\DisplayProductActions - parent: Oksydan\IsFavoriteProducts\Hook\AbstractDisplayHook + Oksydan\IsFavoriteProducts\Hook\: public: true + resource: '../../src/Hook/*' + exclude: + - '../../src/Hook/index.php' - Oksydan\IsFavoriteProducts\Hook\ActionFrontControllerSetMedia: - class: Oksydan\IsFavoriteProducts\Hook\ActionFrontControllerSetMedia - parent: Oksydan\IsFavoriteProducts\Hook\AbstractHook + Oksydan\IsFavoriteProducts\Services\FavoriteProductService: public: true - arguments: - - '@Oksydan\IsFavoriteProducts\Presenter\FavoriteProductJsonPresenter' + class: Oksydan\IsFavoriteProducts\Services\FavoriteProductService - Oksydan\IsFavoriteProducts\Hook\ActionAuthentication: - class: Oksydan\IsFavoriteProducts\Hook\ActionAuthentication - parent: Oksydan\IsFavoriteProducts\Hook\AbstractHook + Oksydan\IsFavoriteProducts\ProductSearchProvider\FavoriteProductsSearchProvider: public: true + class: Oksydan\IsFavoriteProducts\ProductSearchProvider\FavoriteProductsSearchProvider diff --git a/is_favoriteproducts.php b/is_favoriteproducts.php index c537940..6cdd265 100644 --- a/is_favoriteproducts.php +++ b/is_favoriteproducts.php @@ -33,7 +33,7 @@ public function __construct() $this->displayName = $this->trans('Favorite products module', [], 'Modules.Isfavoriteproducts.Admin'); $this->description = $this->trans('Falcon theme favorite products module', [], 'Modules.Isfavoriteproducts.Admin'); - $this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_]; + $this->ps_versions_compliancy = ['min' => '8.1.0', 'max' => _PS_VERSION_]; } public function isUsingNewTranslationSystem(): bool diff --git a/src/ProductSearchProvider/FavoriteProductsSearchProvider.php b/src/ProductSearchProvider/FavoriteProductsSearchProvider.php index 37d3ec0..04556de 100644 --- a/src/ProductSearchProvider/FavoriteProductsSearchProvider.php +++ b/src/ProductSearchProvider/FavoriteProductsSearchProvider.php @@ -8,20 +8,21 @@ use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult; use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; -use Symfony\Contracts\Translation\LocaleAwareInterface; +use PrestaShopBundle\Translation\TranslatorInterface; class FavoriteProductsSearchProvider implements ProductSearchProviderInterface { protected FavoriteProductService $favoriteService; - private LocaleAwareInterface $translator; + private TranslatorInterface $translator; public function __construct( FavoriteProductService $favoriteService, - LocaleAwareInterface $translator + \Context $context ) { - $this->translator = $translator; $this->favoriteService = $favoriteService; + // WE HAVE TO DO IT THIS WAY BECAUSE `TranslatorInterface` IS NOT AVAILABLE AS A SERVICE IN FRONT CONTAINER + $this->translator = $context->getTranslator(); } public function runQuery(ProductSearchContext $context, ProductSearchQuery $query): ProductSearchResult diff --git a/src/Repository/FavoriteProductRepositoryLegacy.php b/src/Repository/FavoriteProductLegacyRepository.php similarity index 79% rename from src/Repository/FavoriteProductRepositoryLegacy.php rename to src/Repository/FavoriteProductLegacyRepository.php index f38f8b1..38041d7 100644 --- a/src/Repository/FavoriteProductRepositoryLegacy.php +++ b/src/Repository/FavoriteProductLegacyRepository.php @@ -6,7 +6,7 @@ use Doctrine\DBAL\Connection; -class FavoriteProductRepositoryLegacy +class FavoriteProductLegacyRepository { /** * @var Connection @@ -16,7 +16,7 @@ class FavoriteProductRepositoryLegacy /** * @var string */ - private string $databasePrefix; + private string $dbPrefix; /** * @var string @@ -25,13 +25,13 @@ class FavoriteProductRepositoryLegacy /** * @param Connection $connection - * @param string $databasePrefix + * @param string $dbPrefix */ - public function __construct(Connection $connection, string $databasePrefix) + public function __construct(Connection $connection, string $dbPrefix) { $this->connection = $connection; - $this->databasePrefix = $databasePrefix; - $this->table = $this->databasePrefix . 'favorite_product'; + $this->dbPrefix = $dbPrefix; + $this->table = $this->dbPrefix . 'favorite_product'; } public function getFavoriteProductsForListing( @@ -50,7 +50,7 @@ public function getFavoriteProductsForListing( ->where('fp.id_customer = :id_customer') ->andWhere('fp.id_shop = :id_shop') ->orderBy('fp.' . $orderBy, $orderWay) - ->join('fp', $this->databasePrefix . 'product_shop', 'ps', 'ps.id_product = fp.id_product AND ps.id_shop = fp.id_shop') + ->join('fp', $this->dbPrefix . 'product_shop', 'ps', 'ps.id_product = fp.id_product AND ps.id_shop = fp.id_shop') ->andWhere('ps.active = 1') ->andWhere('ps.visibility != \'none\'') ->setFirstResult(($page - 1) * $limit) @@ -76,7 +76,7 @@ public function getCountFavoriteProductsForListing( ->andWhere('fp.id_shop = :id_shop') ->setParameter('id_customer', $id_customer) ->setParameter('id_shop', $id_shop) - ->join('fp', $this->databasePrefix . 'product_shop', 'ps', 'ps.id_product = fp.id_product AND ps.id_shop = fp.id_shop') + ->join('fp', $this->dbPrefix . 'product_shop', 'ps', 'ps.id_product = fp.id_product AND ps.id_shop = fp.id_shop') ->andWhere('ps.active = 1') ->andWhere('ps.visibility != \'none\''); diff --git a/src/Repository/FavoriteProductRepository.php b/src/Repository/FavoriteProductRepository.php index 52f5621..52ff230 100644 --- a/src/Repository/FavoriteProductRepository.php +++ b/src/Repository/FavoriteProductRepository.php @@ -4,12 +4,16 @@ namespace Oksydan\IsFavoriteProducts\Repository; -use Doctrine\ORM\EntityRepository; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\Persistence\ManagerRegistry; use Oksydan\IsFavoriteProducts\Entity\FavoriteProduct; -class FavoriteProductRepository extends EntityRepository +class FavoriteProductRepository extends ServiceEntityRepository { - private string $databasePrefix = _DB_PREFIX_; + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, FavoriteProduct::class); + } public function getFavoriteProductsByCustomer(int $id_customer, int $id_shop): array { diff --git a/src/Repository/ProductRepository.php b/src/Repository/ProductLegacyRepository.php similarity index 85% rename from src/Repository/ProductRepository.php rename to src/Repository/ProductLegacyRepository.php index 42c19e8..341709c 100644 --- a/src/Repository/ProductRepository.php +++ b/src/Repository/ProductLegacyRepository.php @@ -6,7 +6,7 @@ use Doctrine\DBAL\Connection; -class ProductRepository +class ProductLegacyRepository { /** * @var Connection @@ -16,7 +16,7 @@ class ProductRepository /** * @var string */ - private string $databasePrefix; + private string $dbPrefix; /** * @var string @@ -25,13 +25,13 @@ class ProductRepository /** * @param Connection $connection - * @param string $databasePrefix + * @param string $dbPrefix */ - public function __construct(Connection $connection, string $databasePrefix) + public function __construct(Connection $connection, string $dbPrefix) { $this->connection = $connection; - $this->databasePrefix = $databasePrefix; - $this->table = $this->databasePrefix . 'product_shop'; + $this->dbPrefix = $dbPrefix; + $this->table = $this->dbPrefix . 'product_shop'; } public function isProductExistsInStore(int $productId, int $productIdAttribute, int $storeId): bool @@ -48,7 +48,7 @@ public function isProductExistsInStore(int $productId, int $productIdAttribute, if ($productIdAttribute > 0) { $qb->join( 'p', - $this->databasePrefix . 'product_attribute_shop', 'pa', + $this->dbPrefix . 'product_attribute_shop', 'pa', 'pa.id_product = p.id_product AND pa.id_shop = p.id_shop AND pa.id_product_attribute = :id_product_attribute'); } @@ -76,7 +76,7 @@ public function checkProductActiveAndVisible( if ($productIdAttribute > 0) { $qb->join( 'p', - $this->databasePrefix . 'product_attribute_shop', 'pa', + $this->dbPrefix . 'product_attribute_shop', 'pa', 'pa.id_product = p.id_product AND pa.id_shop = p.id_shop AND pa.id_product_attribute = :id_product_attribute'); } diff --git a/src/Services/FavoriteProductService.php b/src/Services/FavoriteProductService.php index e28a494..440131a 100644 --- a/src/Services/FavoriteProductService.php +++ b/src/Services/FavoriteProductService.php @@ -10,8 +10,8 @@ use Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper; use Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository; use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository; -use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepositoryLegacy; -use Oksydan\IsFavoriteProducts\Repository\ProductRepository; +use Oksydan\IsFavoriteProducts\Repository\FavoriteProductLegacyRepository; +use Oksydan\IsFavoriteProducts\Repository\ProductLegacyRepository; class FavoriteProductService { @@ -26,9 +26,9 @@ class FavoriteProductService private FavoriteProductRepository $favoriteProductsRepository; /* - * @var FavoriteProductRepositoryLegacy + * @var FavoriteProductLegacyRepository */ - private FavoriteProductRepositoryLegacy $favoriteProductsRepositoryLegacy; + private FavoriteProductLegacyRepository $favoriteProductsRepositoryLegacy; /* * @var FavoriteProductsCookieRepository @@ -38,7 +38,7 @@ class FavoriteProductService /* * @var ProductRepository */ - private ProductRepository $productRepository; + private ProductLegacyRepository $productRepository; private FavoriteProductMapper $favoriteProductMapper; @@ -47,12 +47,12 @@ class FavoriteProductService const FAVORITE_LIMIT_FOR_GUEST = 20; public function __construct( - \Context $context, - FavoriteProductRepository $favoriteProductsRepository, + \Context $context, + FavoriteProductRepository $favoriteProductsRepository, FavoriteProductCookieRepository $favoriteProductsCookieRepository, - ProductRepository $productRepository, - FavoriteProductRepositoryLegacy $favoriteProductsRepositoryLegacy, - FavoriteProductMapper $favoriteProductMapper + ProductLegacyRepository $productRepository, + FavoriteProductLegacyRepository $favoriteProductsRepositoryLegacy, + FavoriteProductMapper $favoriteProductMapper ) { $this->context = $context; $this->favoriteProductsRepository = $favoriteProductsRepository; From 3f23f206762f0e897b2ce3fbcbbcacb73b4ef44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ste=CC=A8pien=CC=81?= Date: Tue, 12 Dec 2023 01:59:18 +0100 Subject: [PATCH 2/2] cs-fixer --- src/Services/FavoriteProductService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Services/FavoriteProductService.php b/src/Services/FavoriteProductService.php index 440131a..1eb4c1d 100644 --- a/src/Services/FavoriteProductService.php +++ b/src/Services/FavoriteProductService.php @@ -9,8 +9,8 @@ use Oksydan\IsFavoriteProducts\Entity\FavoriteProduct; use Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper; use Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository; -use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository; use Oksydan\IsFavoriteProducts\Repository\FavoriteProductLegacyRepository; +use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository; use Oksydan\IsFavoriteProducts\Repository\ProductLegacyRepository; class FavoriteProductService @@ -47,12 +47,12 @@ class FavoriteProductService const FAVORITE_LIMIT_FOR_GUEST = 20; public function __construct( - \Context $context, - FavoriteProductRepository $favoriteProductsRepository, + \Context $context, + FavoriteProductRepository $favoriteProductsRepository, FavoriteProductCookieRepository $favoriteProductsCookieRepository, - ProductLegacyRepository $productRepository, + ProductLegacyRepository $productRepository, FavoriteProductLegacyRepository $favoriteProductsRepositoryLegacy, - FavoriteProductMapper $favoriteProductMapper + FavoriteProductMapper $favoriteProductMapper ) { $this->context = $context; $this->favoriteProductsRepository = $favoriteProductsRepository;