-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b03f674
commit 54e54af
Showing
16 changed files
with
209 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusElasticsearchPlugin\Facet; | ||
|
||
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; | ||
use BitBag\SyliusElasticsearchPlugin\Repository\ProductAttributeRepositoryInterface; | ||
use BitBag\SyliusElasticsearchPlugin\Repository\ProductOptionRepositoryInterface; | ||
use Sylius\Component\Attribute\Model\AttributeInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Sylius\Component\Product\Model\ProductOptionInterface; | ||
|
||
final class AutoDiscoverRegistry implements AutoDiscoverRegistryInterface | ||
{ | ||
/** @var FacetInterface[] */ | ||
private array $facets = []; | ||
|
||
public function __construct( | ||
private bool $autoRegister, | ||
private ProductAttributeRepositoryInterface $productAttributeRepository, | ||
private ProductOptionRepositoryInterface $productOptionRepository, | ||
private ConcatedNameResolverInterface $attributeNameResolver, | ||
private ConcatedNameResolverInterface $optionNameResolver, | ||
private LocaleContextInterface $localeContext, | ||
private RegistryInterface $registry, | ||
private array $excludedAttributes = [], | ||
private array $excludedOptions = [], | ||
) { | ||
} | ||
|
||
public function autoRegister(): void | ||
{ | ||
if (false === $this->autoRegister || [] !== $this->facets) { | ||
return; | ||
} | ||
|
||
$this->discoverAttributes(); | ||
$this->discoverOptions(); | ||
|
||
foreach ($this->facets as $facetId => $facet) { | ||
$this->registry->addFacet($facetId, $facet); | ||
} | ||
} | ||
|
||
private function discoverAttributes(): void | ||
{ | ||
$attributes = $this->productAttributeRepository->findAllWithTranslations($this->localeContext->getLocaleCode()); | ||
|
||
/** @var AttributeInterface $attribute */ | ||
foreach ($attributes as $attribute) { | ||
$code = $attribute->getCode(); | ||
|
||
if (in_array($code, $this->excludedAttributes, true)) { | ||
continue; | ||
} | ||
|
||
$this->facets[$code] = new AttributeFacet( | ||
$this->attributeNameResolver, | ||
$attribute, | ||
$this->localeContext, | ||
); | ||
} | ||
} | ||
|
||
private function discoverOptions(): void | ||
{ | ||
$options = $this->productOptionRepository->findAllWithTranslations($this->localeContext->getLocaleCode()); | ||
|
||
/** @var ProductOptionInterface $option */ | ||
foreach ($options as $option) { | ||
$code = $option->getCode(); | ||
|
||
if (in_array($code, $this->excludedOptions, true)) { | ||
continue; | ||
} | ||
|
||
$this->facets[$code] = new OptionFacet( | ||
$this->optionNameResolver, | ||
$option, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace BitBag\SyliusElasticsearchPlugin\Facet; | ||
|
||
interface AutoDiscoverRegistryInterface | ||
{ | ||
public function autoRegister(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusElasticsearchPlugin\Repository; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
class ProductOptionRepository implements ProductOptionRepositoryInterface | ||
{ | ||
public function __construct( | ||
private RepositoryInterface $productOptionRepository | ||
) { | ||
} | ||
|
||
public function findAllWithTranslations(?string $locale): array | ||
{ | ||
/** @var QueryBuilder $queryBuilder */ | ||
$queryBuilder = $this->productOptionRepository->createQueryBuilder('o'); | ||
|
||
if (null !== $locale) { | ||
$queryBuilder | ||
->addSelect('translation') | ||
->leftJoin('o.translations', 'translation', 'ot') | ||
->andWhere('translation.locale = :locale') | ||
->setParameter('locale', $locale) | ||
; | ||
} | ||
|
||
return $queryBuilder | ||
->getQuery() | ||
->getResult() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusElasticsearchPlugin\Repository; | ||
|
||
interface ProductOptionRepositoryInterface | ||
{ | ||
public function findAllWithTranslations(?string $locale): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.