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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 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\Metadata\Document\DocumentMetadata;
18
use ApiPlatform\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface;
19
use ApiPlatform\Elasticsearch\Paginator;
20
use ApiPlatform\Elasticsearch\Util\ElasticsearchVersion;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Util\Inflector;
23
use ApiPlatform\State\Pagination\Pagination;
24
use ApiPlatform\State\ProviderInterface;
25
use Elasticsearch\Client;
26
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
27

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

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

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

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

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

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

64
        // TODO: remove in 4.x
65
        if ($operation->getElasticsearch() && !$operation->getStateOptions()) {
×
66
            $options = $this->convertDocumentMetadata($this->documentMetadataFactory->create($resourceClass));
×
67
        }
68

69
        $params = [
×
70
            'index' => $options->getIndex() ?? $this->getIndex($operation),
×
71
            'body' => $body,
×
72
        ];
×
73

74
        if (null !== $options->getType() && ElasticsearchVersion::supportsMappingType()) {
×
75
            $params['type'] = $options->getType();
×
76
        }
77

78
        $documents = $this->client->search($params);
×
79

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

90
    private function convertDocumentMetadata(DocumentMetadata $documentMetadata): Options
91
    {
92
        return new Options($documentMetadata->getIndex(), $documentMetadata->getType());
×
93
    }
94

95
    private function getIndex(Operation $operation): string
96
    {
97
        return Inflector::tableize($operation->getShortName());
×
98
    }
99
}
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