Skip to content

Commit

Permalink
OP-303: Expose API resource
Browse files Browse the repository at this point in the history
  • Loading branch information
GracjanJozefczyk committed Jun 6, 2024
1 parent 39c18d1 commit 3dac705
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Api/DataProvider/ProductCollectionDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Api\DataProvider;

use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use BitBag\SyliusElasticsearchPlugin\Api\RequestDataHandler\RequestDataHandlerInterface;
use BitBag\SyliusElasticsearchPlugin\Api\Resolver\FacetsResolverInterface;
use BitBag\SyliusElasticsearchPlugin\Finder\ShopProductsFinderInterface;

final class ProductCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
private const SUPPORTED_OPERATION_TYPE = 'elasticsearch_shop_get';

public function __construct(
private RequestDataHandlerInterface $dataHandler,
private ShopProductsFinderInterface $shopProductsFinder,
private FacetsResolverInterface $facetsResolver
) {
}

public function getCollection(
string $resourceClass,
string $operationName = null,
array $context = []
) {
$data = $this->dataHandler->retrieveData($context);
$facets = $this->facetsResolver->resolve($data);
$products = $this->shopProductsFinder->find($data);

return [
'items' => $products->jsonSerialize(),
'facets' => $facets,
'pagination' => [
'current_page' => $products->getCurrentPage(),
'has_previous_page' => $products->hasPreviousPage(),
'has_next_page' => $products->hasNextPage(),
'per_page' => $products->getMaxPerPage(),
'total_items' => $products->getNbResults(),
'total_pages' => $products->getNbPages(),
],
];
}

public function supports(
string $resourceClass,
string $operationName = null,
array $context = []
): bool {
return self::SUPPORTED_OPERATION_TYPE === $operationName;
}
}
Loading

0 comments on commit 3dac705

Please sign in to comment.