-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInitializeIndex.php
More file actions
38 lines (28 loc) · 1022 Bytes
/
InitializeIndex.php
File metadata and controls
38 lines (28 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php declare(strict_types = 1);
namespace Spameri\Elastic\Model;
readonly class InitializeIndex
{
public function __construct(
private \Spameri\Elastic\Provider\DateTimeProvider $dateTimeProvider,
private \Spameri\Elastic\Model\Indices\Get $get,
private \Spameri\Elastic\Model\Indices\Create $create,
private \Spameri\Elastic\Model\Indices\AddAlias $addAlias,
)
{
}
public function execute(\Spameri\Elastic\Settings\IndexConfigInterface $indexConfig): void
{
$indexAlias = $indexConfig->provide()->indexName();
try {
$this->get->execute($indexAlias);
throw new \Spameri\Elastic\Exception\IndexAlreadyExists($indexAlias);
} catch (\Spameri\Elastic\Exception\ElasticSearch $exception) {
$indexName =
$indexAlias
. '-'
. $this->dateTimeProvider->provide()->format(\Spameri\Elastic\Entity\Property\DateTime::INDEX_FORMAT);
$this->create->execute($indexName, $indexConfig->provide()->toArray(), $indexAlias);
$this->addAlias->execute($indexAlias, $indexName);
}
}
}