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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 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 Elasticsearch\Client as V7Client;
28
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
29
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
30

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

51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): Paginator
55
    {
56
        $resourceClass = $operation->getClass();
×
57
        $body = [];
×
58

59
        foreach ($this->collectionExtensions as $collectionExtension) {
×
60
            $body = $collectionExtension->applyToCollection($body, $resourceClass, $operation, $context);
×
61
        }
62

63
        if (!isset($body['query']) && !isset($body['aggs'])) {
×
64
            $body['query'] = ['match_all' => new \stdClass()];
×
65
        }
66

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

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

72
        $params = [
×
73
            'index' => $options->getIndex() ?? $this->getIndex($operation),
×
74
            'body' => $body,
×
75
        ];
×
76

77
        try {
NEW
78
            $documents = $this->client->search($params); // @phpstan-ignore-line
×
NEW
79
        } catch (V7Missing404Exception $e) { // @phpstan-ignore-line
×
NEW
80
            throw new Error(status: $e->getCode(), detail: $e->getMessage(), title: $e->getMessage(), originalTrace: $e->getTrace()); // @phpstan-ignore-line
×
81
        } catch (ClientResponseException $e) {
×
82
            $response = $e->getResponse();
×
83
            throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
×
84
        }
85

NEW
86
        if (class_exists(Elasticsearch::class) && $documents instanceof Elasticsearch) {
×
87
            $documents = $documents->asArray();
×
88
        }
89

90
        return new Paginator(
×
91
            $this->denormalizer,
×
92
            $documents,
×
93
            $resourceClass,
×
94
            $limit,
×
95
            $offset,
×
96
            $context
×
97
        );
×
98
    }
99

100
    private function getIndex(Operation $operation): string
101
    {
102
        return $this->inflector->tableize($operation->getShortName());
×
103
    }
104
}
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