-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventRepresentationProvider.php
More file actions
42 lines (36 loc) · 1.41 KB
/
Copy pathEventRepresentationProvider.php
File metadata and controls
42 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace App\Api\State;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Exception\IndexException;
use App\Model\IndexName;
use App\Service\ElasticSearch\ElasticSearchPaginator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
final class EventRepresentationProvider extends AbstractProvider implements ProviderInterface
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws IndexException
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ElasticSearchPaginator|array|null
{
if ($operation instanceof CollectionOperationInterface) {
$filters = $this->getFilters($operation, $context);
$offset = $this->calculatePageOffset($context);
$limit = $this->getItemsPerPage($context);
$results = $this->index->getAll(IndexName::Events->value, $filters, $offset, $limit);
return new ElasticSearchPaginator($results, $limit, $offset);
}
try {
return [$this->index->get(IndexName::Events->value, $uriVariables['id'])['_source']];
} catch (IndexException $e) {
if (404 === $e->getCode()) {
return null;
}
throw $e;
}
}
}