Skip to content

Commit

Permalink
Merge pull request #9 from Oksydan/module-refacto
Browse files Browse the repository at this point in the history
Multiple changes, refacto for autowire and autoconfigure
  • Loading branch information
Oksydan authored Dec 12, 2023
2 parents 6fca07b + 3f23f20 commit 304c40d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 123 deletions.
19 changes: 19 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
75 changes: 4 additions & 71 deletions config/common.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
_defaults:
public: true
public: false

oksydan.is_favoriteproducts.module:
class: Is_favoriteproducts
Expand All @@ -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 ]
42 changes: 21 additions & 21 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion is_favoriteproducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/ProductSearchProvider/FavoriteProductsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Doctrine\DBAL\Connection;

class FavoriteProductRepositoryLegacy
class FavoriteProductLegacyRepository
{
/**
* @var Connection
Expand All @@ -16,7 +16,7 @@ class FavoriteProductRepositoryLegacy
/**
* @var string
*/
private string $databasePrefix;
private string $dbPrefix;

/**
* @var string
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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\'');

Expand Down
10 changes: 7 additions & 3 deletions src/Repository/FavoriteProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Doctrine\DBAL\Connection;

class ProductRepository
class ProductLegacyRepository
{
/**
* @var Connection
Expand All @@ -16,7 +16,7 @@ class ProductRepository
/**
* @var string
*/
private string $databasePrefix;
private string $dbPrefix;

/**
* @var string
Expand All @@ -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
Expand All @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down
14 changes: 7 additions & 7 deletions src/Services/FavoriteProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Oksydan\IsFavoriteProducts\Entity\FavoriteProduct;
use Oksydan\IsFavoriteProducts\Mapper\FavoriteProductMapper;
use Oksydan\IsFavoriteProducts\Repository\FavoriteProductCookieRepository;
use Oksydan\IsFavoriteProducts\Repository\FavoriteProductLegacyRepository;
use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepository;
use Oksydan\IsFavoriteProducts\Repository\FavoriteProductRepositoryLegacy;
use Oksydan\IsFavoriteProducts\Repository\ProductRepository;
use Oksydan\IsFavoriteProducts\Repository\ProductLegacyRepository;

class FavoriteProductService
{
Expand All @@ -26,9 +26,9 @@ class FavoriteProductService
private FavoriteProductRepository $favoriteProductsRepository;

/*
* @var FavoriteProductRepositoryLegacy
* @var FavoriteProductLegacyRepository
*/
private FavoriteProductRepositoryLegacy $favoriteProductsRepositoryLegacy;
private FavoriteProductLegacyRepository $favoriteProductsRepositoryLegacy;

/*
* @var FavoriteProductsCookieRepository
Expand All @@ -38,7 +38,7 @@ class FavoriteProductService
/*
* @var ProductRepository
*/
private ProductRepository $productRepository;
private ProductLegacyRepository $productRepository;

private FavoriteProductMapper $favoriteProductMapper;

Expand All @@ -50,8 +50,8 @@ public function __construct(
\Context $context,
FavoriteProductRepository $favoriteProductsRepository,
FavoriteProductCookieRepository $favoriteProductsCookieRepository,
ProductRepository $productRepository,
FavoriteProductRepositoryLegacy $favoriteProductsRepositoryLegacy,
ProductLegacyRepository $productRepository,
FavoriteProductLegacyRepository $favoriteProductsRepositoryLegacy,
FavoriteProductMapper $favoriteProductMapper
) {
$this->context = $context;
Expand Down

0 comments on commit 304c40d

Please sign in to comment.