-
Notifications
You must be signed in to change notification settings - Fork 188
/
IndexSuffixFinder.php
53 lines (46 loc) · 1.35 KB
/
IndexSuffixFinder.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\Client;
/**
* Constructs index name with date's suffix.
*/
class IndexSuffixFinder
{
/**
* Constructs index name with date suffix. Sets name in the connection.
*
* E.g. 2022.03.22-5 (if 4 indexes exists already for given date)
*
* @param Connection $connection Connection to act upon.
* @param null|\DateTime $time Date for which the suffix will be based on.
* Current date if null.
*
* @return string
*/
public function setNextFreeIndex(Connection $connection, \DateTime $time = null)
{
if ($time === null) {
$time = new \DateTime();
}
$suffix = null;
$date = $time->format('Y.m.d');
$indexName = $connection->getIndexName();
$nameBase = $indexName . '-' . $date;
$name = $nameBase;
$i = 0;
$connection->setIndexName($name);
while ($connection->indexExists()) {
$i++;
$name = "{$nameBase}-{$i}";
$connection->setIndexName($name);
}
return $name;
}
}