• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Elasticsearch/State/CollectionProvider.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Elasticsearch\State;
15

16
use ApiPlatform\Elasticsearch\Extension\RequestBodySearchCollectionExtensionInterface;
17
use ApiPlatform\Elasticsearch\Paginator;
18
use ApiPlatform\Metadata\InflectorInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Util\Inflector;
21
use ApiPlatform\State\ApiResource\Error;
22
use ApiPlatform\State\Pagination\Pagination;
23
use ApiPlatform\State\ProviderInterface;
24
use Elastic\Elasticsearch\Client;
25
use Elastic\Elasticsearch\Exception\ClientResponseException;
26
use Elastic\Elasticsearch\Response\Elasticsearch;
27
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
28

29
/**
30
 * Collection provider for Elasticsearch.
31
 *
32
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
33
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
34
 */
35
final class CollectionProvider implements ProviderInterface
36
{
37
    /**
38
     * @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
39
     */
40
    public function __construct(private readonly Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = [], private readonly ?InflectorInterface $inflector = new Inflector())
41
    {
42
    }
×
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): Paginator
48
    {
49
        $resourceClass = $operation->getClass();
×
50
        $body = [];
×
51

52
        foreach ($this->collectionExtensions as $collectionExtension) {
×
53
            $body = $collectionExtension->applyToCollection($body, $resourceClass, $operation, $context);
×
54
        }
55

56
        if (!isset($body['query']) && !isset($body['aggs'])) {
×
57
            $body['query'] = ['match_all' => new \stdClass()];
×
58
        }
59

60
        $limit = $body['size'] ??= $this->pagination->getLimit($operation, $context);
×
61
        $offset = $body['from'] ??= $this->pagination->getOffset($operation, $context);
×
62

63
        $options = $operation->getStateOptions() instanceof Options ? $operation->getStateOptions() : new Options(index: $this->getIndex($operation));
×
64

UNCOV
65
        $params = [
×
66
            'index' => $options->getIndex() ?? $this->getIndex($operation),
×
67
            'body' => $body,
×
68
        ];
×
69

70
        try {
NEW
71
            $documents = $this->client->search($params);
×
72
        } catch (ClientResponseException $e) {
×
73
            $response = $e->getResponse();
×
74
            throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
×
75
        }
76

77
        if ($documents instanceof Elasticsearch) {
×
78
            $documents = $documents->asArray();
×
79
        }
80

81
        return new Paginator(
×
82
            $this->denormalizer,
×
83
            $documents,
×
84
            $resourceClass,
×
85
            $limit,
×
86
            $offset,
×
87
            $context
×
88
        );
×
89
    }
90

91
    private function getIndex(Operation $operation): string
92
    {
93
        return $this->inflector->tableize($operation->getShortName());
×
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc