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

api-platform / core / 6198903018

15 Sep 2023 01:50PM UTC coverage: 37.037% (+0.04%) from 36.999%
6198903018

push

github

web-flow
fix: exception to status on error resource (#5823)

52 of 52 new or added lines in 12 files covered. (100.0%)

10121 of 27327 relevant lines covered (37.04%)

19.98 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\ApiResource\Error;
17
use ApiPlatform\Elasticsearch\Extension\RequestBodySearchCollectionExtensionInterface;
18
use ApiPlatform\Elasticsearch\Metadata\Document\DocumentMetadata;
19
use ApiPlatform\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface;
20
use ApiPlatform\Elasticsearch\Paginator;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Util\Inflector;
23
use ApiPlatform\State\Pagination\Pagination;
24
use ApiPlatform\State\ProviderInterface;
25
use Elastic\Elasticsearch\Client;
26
use Elastic\Elasticsearch\Exception\ClientResponseException;
27
use Elastic\Elasticsearch\Response\Elasticsearch;
28
use Elasticsearch\Client as LegacyClient;
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(private readonly LegacyClient|Client $client, private readonly ?DocumentMetadataFactoryInterface $documentMetadataFactory = null, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = []) // @phpstan-ignore-line
43
    {
44
    }
×
45

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

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

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

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

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

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

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

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

84
        if ($documents instanceof Elasticsearch) {
×
85
            $documents = $documents->asArray();
×
86
        }
87

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

98
    private function convertDocumentMetadata(DocumentMetadata $documentMetadata): Options
99
    {
100
        return new Options($documentMetadata->getIndex(), $documentMetadata->getType());
×
101
    }
102

103
    private function getIndex(Operation $operation): string
104
    {
105
        return Inflector::tableize($operation->getShortName());
×
106
    }
107
}
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