-
Notifications
You must be signed in to change notification settings - Fork 89
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
39c18d1
commit 3dac705
Showing
3 changed files
with
317 additions
and
0 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,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; | ||
} | ||
} |
Oops, something went wrong.