1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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\Core\Bridge\Elasticsearch\Metadata\Document\Factory; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException; |
17
|
|
|
use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\DocumentMetadata; |
18
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
19
|
|
|
use Doctrine\Common\Inflector\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
|
|
|
* |
28
|
|
|
* @experimental |
29
|
|
|
* |
30
|
|
|
* @author Baptiste Meyer <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class CatDocumentMetadataFactory implements DocumentMetadataFactoryInterface |
33
|
|
|
{ |
34
|
|
|
private $client; |
35
|
|
|
private $resourceMetadataFactory; |
36
|
|
|
private $decorated; |
37
|
|
|
|
38
|
|
|
public function __construct(Client $client, ResourceMetadataFactoryInterface $resourceMetadataFactory, ?DocumentMetadataFactoryInterface $decorated = null) |
39
|
|
|
{ |
40
|
|
|
$this->client = $client; |
41
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
42
|
|
|
$this->decorated = $decorated; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function create(string $resourceClass): DocumentMetadata |
49
|
|
|
{ |
50
|
|
|
$documentMetadata = null; |
51
|
|
|
|
52
|
|
|
if ($this->decorated) { |
53
|
|
|
try { |
54
|
|
|
$documentMetadata = $this->decorated->create($resourceClass); |
55
|
|
|
} catch (IndexNotFoundException $e) { |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($documentMetadata && null !== $documentMetadata->getIndex()) { |
60
|
|
|
return $documentMetadata; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (null === $resourceShortName = $this->resourceMetadataFactory->create($resourceClass)->getShortName()) { |
64
|
|
|
return $this->handleNotFound($documentMetadata, $resourceClass); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$index = Inflector::tableize($resourceShortName); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$this->client->cat()->indices(['index' => $index]); |
71
|
|
|
} catch (Missing404Exception $e) { |
72
|
|
|
return $this->handleNotFound($documentMetadata, $resourceClass); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return ($documentMetadata ?? new DocumentMetadata())->withIndex($index); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws IndexNotFoundException |
80
|
|
|
*/ |
81
|
|
|
private function handleNotFound(?DocumentMetadata $documentMetadata, string $resourceClass): DocumentMetadata |
82
|
|
|
{ |
83
|
|
|
if ($documentMetadata) { |
84
|
|
|
return $documentMetadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new IndexNotFoundException(sprintf('No index associated with the "%s" resource class.', $resourceClass)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|