-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor query build and implement sorting
- Loading branch information
Showing
18 changed files
with
212 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Config\MonologConfig; | ||
|
||
/** @psalm-suppress UndefinedClass */ | ||
return static function (MonologConfig $monolog): void { | ||
$monolog->channels(['webgriffe_sylius_elasticsearch_plugin']); | ||
}; |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Webgriffe\SyliusElasticsearchPlugin\Builder\TwigQueryBuilder; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set('webgriffe.sylius_elasticsearch_plugin.builder.query', TwigQueryBuilder::class) | ||
->args([ | ||
service('twig'), | ||
service('sylius.context.locale'), | ||
service('monolog.logger.webgriffe_sylius_elasticsearch_plugin'), | ||
]) | ||
; | ||
}; |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Builder; | ||
|
||
use Sylius\Component\Core\Model\TaxonInterface; | ||
|
||
interface QueryBuilderInterface | ||
{ | ||
/** | ||
* @param array<string, string> $sorting | ||
*/ | ||
public function buildTaxonQuery( | ||
TaxonInterface $taxon, | ||
array $sorting = [], | ||
): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Builder; | ||
|
||
use const JSON_THROW_ON_ERROR; | ||
use Psr\Log\LoggerInterface; | ||
use Sylius\Component\Core\Model\TaxonInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Twig\Environment; | ||
|
||
final readonly class TwigQueryBuilder implements QueryBuilderInterface | ||
{ | ||
public function __construct( | ||
private Environment $twig, | ||
private LocaleContextInterface $localeContext, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function buildTaxonQuery( | ||
TaxonInterface $taxon, | ||
array $sorting = [], | ||
): array { | ||
$query = $this->twig->render('@WebgriffeSyliusElasticsearchPlugin/query/taxon/query.json.twig', [ | ||
'taxon' => $taxon, | ||
]); | ||
$taxonQuery = []; | ||
/** @var array $queryNormalized */ | ||
$queryNormalized = json_decode($query, true, 512, JSON_THROW_ON_ERROR); | ||
$taxonQuery['query'] = $queryNormalized; | ||
$localeCode = $this->localeContext->getLocaleCode(); | ||
|
||
foreach ($sorting as $field => $order) { | ||
$sort = $this->twig->render('@WebgriffeSyliusElasticsearchPlugin/query/taxon/sort/' . $field . '.json.twig', [ | ||
'field' => $field, | ||
'order' => $order, | ||
'taxon' => $taxon, | ||
'localeCode' => $localeCode, | ||
]); | ||
/** @var array $sortNormalized */ | ||
$sortNormalized = json_decode($sort, true, 512, JSON_THROW_ON_ERROR); | ||
$taxonQuery['sort'][] = $sortNormalized; | ||
} | ||
|
||
$this->logger->debug(sprintf('Built taxon query: "%s".', json_encode($taxonQuery, JSON_THROW_ON_ERROR))); | ||
|
||
return $taxonQuery; | ||
} | ||
} |
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
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,27 @@ | ||
{ | ||
"bool": { | ||
"must": [ | ||
{ | ||
"nested": { | ||
"path": "taxons", | ||
"query": { | ||
"bool": { | ||
"must": [ | ||
{ | ||
"term": { | ||
"taxons.sylius-id": "{{ taxon.id }}" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"term": { | ||
"enabled": true | ||
} | ||
} | ||
] | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"created-at": { | ||
"order": "{{ order }}" | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"name.value": { | ||
"order": "{{ order }}", | ||
"nested": { | ||
"path": "name", | ||
"filter": { | ||
"match": { | ||
"name.locale": "{{ localeCode }}" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"taxons.position": { | ||
"order": "{{ order }}", | ||
"nested": { | ||
"path": "taxons", | ||
"filter": { | ||
"term": { | ||
"taxons.sylius-id": "{{ taxon.id }}" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"variants.price.price": { | ||
"order": "{{ order }}", | ||
"mode": "avg", | ||
"nested": { | ||
"path": "variants" | ||
} | ||
} | ||
} |
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