• 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/Metadata/Document/Factory/CatDocumentMetadataFactory.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\Metadata\Document\Factory;
15

16
use ApiPlatform\Elasticsearch\Exception\IndexNotFoundException;
17
use ApiPlatform\Elasticsearch\Metadata\Document\DocumentMetadata;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Util\Inflector;
20
use Elasticsearch\Client;
21
use Elasticsearch\Common\Exceptions\Missing404Exception;
22

23
/**
24
 * Creates document's metadata using indices from the cat APIs.
25
 *
26
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-indices.html
27
 * @deprecated
28
 *
29
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
30
 */
31
final class CatDocumentMetadataFactory implements DocumentMetadataFactoryInterface
32
{
33
    public function __construct(private readonly Client $client, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly ?DocumentMetadataFactoryInterface $decorated = null)
34
    {
35
    }
×
36

37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function create(string $resourceClass): DocumentMetadata
41
    {
42
        $documentMetadata = null;
×
43

44
        if ($this->decorated) {
×
45
            try {
46
                $documentMetadata = $this->decorated->create($resourceClass);
×
47
            } catch (IndexNotFoundException) {
×
48
            }
49
        }
50

51
        if ($documentMetadata && null !== $documentMetadata->getIndex()) {
×
52
            return $documentMetadata;
×
53
        }
54

55
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
×
56
        $resourceShortName = $resourceMetadata->getOperation()->getShortName();
×
57

58
        if (null === $resourceShortName) {
×
59
            return $this->handleNotFound($documentMetadata, $resourceClass);
×
60
        }
61

62
        $index = Inflector::tableize($resourceShortName);
×
63

64
        try {
65
            $this->client->cat()->indices(['index' => $index]);
×
66
        } catch (Missing404Exception) {
×
67
            return $this->handleNotFound($documentMetadata, $resourceClass);
×
68
        }
69

70
        return ($documentMetadata ?? new DocumentMetadata())->withIndex($index);
×
71
    }
72

73
    /**
74
     * @throws IndexNotFoundException
75
     */
76
    private function handleNotFound(?DocumentMetadata $documentMetadata, string $resourceClass): DocumentMetadata
77
    {
78
        if ($documentMetadata) {
×
79
            return $documentMetadata;
×
80
        }
81

82
        throw new IndexNotFoundException(sprintf('No index associated with the "%s" resource class.', $resourceClass));
×
83
    }
84
}
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