Skip to content

Commit

Permalink
Parse response from ES
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Feb 20, 2024
1 parent 7223279 commit f3b9026
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 31 deletions.
1 change: 1 addition & 0 deletions config/services/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
service('sylius.context.channel'),
service('lruozzi9.sylius_elasticsearch_plugin.generator.index_name'),
service('lruozzi9.sylius_elasticsearch_plugin.provider.document_type'),
service('lruozzi9.sylius_elasticsearch_plugin.parser.elasticsearch_document'),
])
->call('setContainer', [service('service_container')])
->tag('controller.service_arguments')
Expand Down
18 changes: 18 additions & 0 deletions config/services/factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use LRuozzi9\SyliusElasticsearchPlugin\Factory\ProductResponseFactory;
use LRuozzi9\SyliusElasticsearchPlugin\Model\ProductResponse;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('lruozzi9.sylius_elasticsearch_plugin.factory.product_response', ProductResponseFactory::class)
->args([
ProductResponse::class,
])
;
};
20 changes: 20 additions & 0 deletions config/services/parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use LRuozzi9\SyliusElasticsearchPlugin\Parser\ElasticsearchProductDocumentParser;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('lruozzi9.sylius_elasticsearch_plugin.parser.elasticsearch_document', ElasticsearchProductDocumentParser::class)
->args([
service('lruozzi9.sylius_elasticsearch_plugin.factory.product_response'),
service('sylius.context.locale'),
service('sylius.context.channel'),
param('sylius_locale.locale'),
])
;
};
7 changes: 6 additions & 1 deletion src/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace LRuozzi9\SyliusElasticsearchPlugin\Command;

use InvalidArgumentException;
use LRuozzi9\SyliusElasticsearchPlugin\Message\CreateIndex;
use LRuozzi9\SyliusElasticsearchPlugin\Provider\DocumentTypeProviderInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
Expand All @@ -30,7 +31,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$channels = $this->channelRepository->findAll();
foreach ($channels as $channel) {
foreach ($this->documentTypeProvider->getDocumentsType() as $documentType) {
$this->messageBus->dispatch(new CreateIndex($channel->getId(), $documentType->getCode()));
$channelId = $channel->getId();
if (!is_string($channelId) && !is_int($channelId)) {
throw new InvalidArgumentException('Channel id must be a string or an integer');
}
$this->messageBus->dispatch(new CreateIndex($channelId, $documentType->getCode()));
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/Controller/ElasticsearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use LRuozzi9\SyliusElasticsearchPlugin\Generator\IndexNameGeneratorInterface;
use LRuozzi9\SyliusElasticsearchPlugin\Model\QueryResult;
use LRuozzi9\SyliusElasticsearchPlugin\Pagerfanta\ElasticsearchAdapter;
use LRuozzi9\SyliusElasticsearchPlugin\Parser\DocumentParserInterface;
use LRuozzi9\SyliusElasticsearchPlugin\Provider\DocumentTypeProviderInterface;
use Pagerfanta\Pagerfanta;
use Sylius\Component\Channel\Context\ChannelContextInterface;
Expand All @@ -20,6 +21,9 @@
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ElasticsearchController extends AbstractController
{
public function __construct(
Expand All @@ -29,6 +33,7 @@ public function __construct(
private readonly ChannelContextInterface $channelContext,
private readonly IndexNameGeneratorInterface $indexNameGenerator,
private readonly DocumentTypeProviderInterface $documentTypeProvider,
private readonly DocumentParserInterface $documentParser,
) {
}

Expand All @@ -42,7 +47,8 @@ public function taxonAction(string $slug): Response
$channel = $this->channelContext->getChannel();
Assert::isInstanceOf($channel, ChannelInterface::class);

$result = $this->indexManager->query([
$aliasName = $this->indexNameGenerator->generateAlias($channel, $this->documentTypeProvider->getDocumentType(ProductDocumentType::CODE));
$query = [
'query' => [
'bool' => [
'must' => [
Expand All @@ -54,8 +60,15 @@ public function taxonAction(string $slug): Response
],
],
],
], $this->indexNameGenerator->generateAlias($channel, $this->documentTypeProvider->getDocumentType(ProductDocumentType::CODE)));
$products = new Pagerfanta(new ElasticsearchAdapter(new QueryResult($result['hits']['hits'])));
];
$result = $this->indexManager->query($query, $aliasName);
$responses = [];
/** @var array{_index: string, _id: string, score: float, _source: array} $hit */
foreach ($result['hits']['hits'] as $hit) {
$responses[] = $this->documentParser->parse($hit);
}
$queryResult = new QueryResult($responses);
$products = new Pagerfanta(new ElasticsearchAdapter($queryResult));

return $this->render('@LRuozzi9SyliusElasticsearchPlugin/Product/index.html.twig', [
'taxon' => $taxon,
Expand Down
19 changes: 19 additions & 0 deletions src/DocumentType/ProductDocumentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ public function getMappings(): array
],
],
],
'slug' => [
'type' => 'nested',
'dynamic' => 'false',
'include_in_parent' => true,
'properties' => [
'locale' => [
'type' => 'text',
'index' => false,
],
'value' => [
'type' => 'keyword',
],
],
],
'main_taxon' => [
'type' => 'object',
'dynamic' => false,
Expand Down Expand Up @@ -150,6 +164,7 @@ private function normalizeProduct(ProductInterface $product): array
'code' => $product->getCode(),
'name' => [],
'description' => [],
'slug' => [],
'taxons' => [],
];
/** @var ProductTranslationInterface $productTranslation */
Expand All @@ -162,6 +177,10 @@ private function normalizeProduct(ProductInterface $product): array
'locale' => $productTranslation->getLocale(),
'value' => $productTranslation->getDescription(),
];
$normalizedProduct['slug'][] = [
'locale' => $productTranslation->getLocale(),
'value' => $productTranslation->getSlug(),
];
}
$mainTaxon = $product->getMainTaxon();
if ($mainTaxon instanceof TaxonInterface) {
Expand Down
30 changes: 30 additions & 0 deletions src/Factory/ProductResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace LRuozzi9\SyliusElasticsearchPlugin\Factory;

use LRuozzi9\SyliusElasticsearchPlugin\Model\ProductResponseInterface;
use Webmozart\Assert\Assert;

final readonly class ProductResponseFactory implements ProductResponseFactoryInterface
{
/**
* @param class-string $responseClass
*/
public function __construct(
private string $responseClass,
) {
}

/**
* @psalm-suppress MixedMethodCall
*/
public function createNew(): ProductResponseInterface
{
$response = new $this->responseClass();
Assert::isInstanceOf($response, ProductResponseInterface::class);

return $response;
}
}
12 changes: 12 additions & 0 deletions src/Factory/ProductResponseFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace LRuozzi9\SyliusElasticsearchPlugin\Factory;

use LRuozzi9\SyliusElasticsearchPlugin\Model\ProductResponseInterface;

interface ProductResponseFactoryInterface
{
public function createNew(): ProductResponseInterface;
}
33 changes: 33 additions & 0 deletions src/Model/AbstractResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace LRuozzi9\SyliusElasticsearchPlugin\Model;

abstract class AbstractResponse implements ResponseInterface
{
private ?string $routeName = null;

/** @var array<string, mixed>|null */
private ?array $routeParams = null;

public function getRouteName(): ?string
{
return $this->routeName;
}

public function setRouteName(?string $routeName): void
{
$this->routeName = $routeName;
}

public function getRouteParams(): ?array
{
return $this->routeParams;
}

public function setRouteParams(?array $routeParams): void
{
$this->routeParams = $routeParams;
}
}
30 changes: 9 additions & 21 deletions src/Model/ProductResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,32 @@

namespace LRuozzi9\SyliusElasticsearchPlugin\Model;

final class ProductResponse implements ProductResponseInterface
final class ProductResponse extends AbstractResponse implements ProductResponseInterface
{
/**
* @param array<string, string>|null $routeParams
*/
public function __construct(
private ?string $routeName = null,
private ?array $routeParams = [],
) {
}
private ?string $name = null;

public function getImage(): string
{
return 'TODO';
}

public function getName(): string
public function getName(): ?string
{
return 'TODO';
return $this->name;
}

public function getPrice(): int
public function setName(?string $name): void
{
return 0;
$this->name = $name;
}

public function getOriginalPrice(): int
public function getPrice(): int
{
return 0;
}

public function getRouteName(): string
{
return (string) $this->routeName;
}

public function getRouteParams(): array
public function getOriginalPrice(): int
{
return $this->routeParams ?? [];
return 0;
}
}
6 changes: 4 additions & 2 deletions src/Model/ProductResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

interface ProductResponseInterface extends ResponseInterface
{
public function getImage(): string;
public function getName(): ?string;

public function setName(?string $name): void;

public function getName(): string;
public function getImage(): string;

public function getPrice(): int;

Expand Down
13 changes: 10 additions & 3 deletions src/Model/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

interface ResponseInterface
{
public function getRouteName(): string;
public function getRouteName(): ?string;

public function setRouteName(?string $routeName): void;

/**
* @return array<string, mixed>
*/
public function getRouteParams(): ?array;

/**
* @return array<string, string>
* @param array<string, mixed> $routeParams
*/
public function getRouteParams(): array;
public function setRouteParams(?array $routeParams): void;
}
15 changes: 15 additions & 0 deletions src/Parser/DocumentParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace LRuozzi9\SyliusElasticsearchPlugin\Parser;

use LRuozzi9\SyliusElasticsearchPlugin\Model\ResponseInterface;

interface DocumentParserInterface
{
/**
* @param array{_index: string, _id: string, score: float, _source: array} $document
*/
public function parse(array $document): ResponseInterface;
}
Loading

0 comments on commit f3b9026

Please sign in to comment.