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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates document's metadata using the attribute configuration. |
22
|
|
|
* |
23
|
|
|
* @experimental |
24
|
|
|
* |
25
|
|
|
* @author Baptiste Meyer <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class AttributeDocumentMetadataFactory implements DocumentMetadataFactoryInterface |
28
|
|
|
{ |
29
|
|
|
private $resourceMetadataFactory; |
30
|
|
|
private $decorated; |
31
|
|
|
|
32
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ?DocumentMetadataFactoryInterface $decorated = null) |
33
|
|
|
{ |
34
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function create(string $resourceClass): DocumentMetadata |
42
|
|
|
{ |
43
|
|
|
$documentMetadata = null; |
44
|
|
|
|
45
|
|
|
if ($this->decorated) { |
46
|
|
|
try { |
47
|
|
|
$documentMetadata = $this->decorated->create($resourceClass); |
48
|
|
|
} catch (IndexNotFoundException $e) { |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$resourceMetadata = null; |
53
|
|
|
|
54
|
|
|
if (!$documentMetadata || null === $documentMetadata->getIndex()) { |
55
|
|
|
$resourceMetadata = $resourceMetadata ?? $this->resourceMetadataFactory->create($resourceClass); |
56
|
|
|
|
57
|
|
|
if (null !== $index = $resourceMetadata->getAttribute('elasticsearch_index')) { |
58
|
|
|
$documentMetadata = $documentMetadata ? $documentMetadata->withIndex($index) : new DocumentMetadata($index); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$documentMetadata || DocumentMetadata::DEFAULT_TYPE === $documentMetadata->getType()) { |
63
|
|
|
$resourceMetadata = $resourceMetadata ?? $this->resourceMetadataFactory->create($resourceClass); |
64
|
|
|
|
65
|
|
|
if (null !== $type = $resourceMetadata->getAttribute('elasticsearch_type')) { |
66
|
|
|
$documentMetadata = $documentMetadata ? $documentMetadata->withType($type) : new DocumentMetadata(null, $type); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($documentMetadata) { |
71
|
|
|
return $documentMetadata; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new IndexNotFoundException(sprintf('No index associated with the "%s" resource class.', $resourceClass)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|