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

IndexSuffixFinder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getNextFreeIndex() 0 20 3
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