Completed
Pull Request — master (#869)
by
unknown
02:28 queued 01:12
created

IndexSuffixFinder::getNextFreeIndex()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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
namespace ONGR\ElasticsearchBundle\Service;
13
14
class IndexSuffixFinder
15
{
16
    public function getNextFreeIndex(IndexService $index, \DateTime $time = null): string
17
    {
18
        if ($time === null) {
19
            $time = new \DateTime();
20
        }
21
22
        $date = $time->format('Y.m.d');
23
        $alias = $index->getIndexName();
24
        $indexName = $alias . '-' . $date;
25
        $i = 0;
26
27
        $client = $index->getClient();
28
29
        while ($client->indices()->exists(['index' => $indexName])) {
30
            $i++;
31
            $indexName = "{$indexName}-{$i}";
32
        }
33
34
        return $indexName;
35
    }
36
}
37