Completed
Pull Request — master (#1246)
by Istvan
04:23
created

Index   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 1
dl 0
loc 50
ccs 4
cts 10
cp 0.4
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 8 2
A getOriginalName() 0 4 2
A overrideName() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\ElasticaBundle\Elastica;
13
14
use Elastica\Index as BaseIndex;
15
16
/**
17
 * Overridden Elastica Index class that provides dynamic index name changes.
18
 *
19
 * @author Konstantin Tjuterev <[email protected]>
20
 */
21
class Index extends BaseIndex
22
{
23
    private $originalName;
24
25
    /**
26
     * Stores created types to avoid recreation.
27
     *
28
     * @var array
29
     */
30
    private $typeCache = [];
31
32
    /**
33
     * Returns the original name of the index if the index has been renamed for reindexing
34
     * or realiasing purposes.
35
     *
36
     * @return string
37
     */
38
    public function getOriginalName()
39
    {
40
        return $this->originalName ?: $this->_name;
41
    }
42
43
    /**
44
     * @param string $type
45
     *
46
     * @return mixed
47
     */
48 13
    public function getType($type)
49
    {
50 13
        if (isset($this->typeCache[$type])) {
51 2
            return $this->typeCache[$type];
52
        }
53
54 13
        return $this->typeCache[$type] = parent::getType($type);
55
    }
56
57
    /**
58
     * Reassign index name.
59
     *
60
     * While it's technically a regular setter for name property, it's specifically named overrideName, but not setName
61
     * since it's used for a very specific case and normally should not be used
62
     *
63
     * @param string $name Index name
64
     */
65
    public function overrideName($name)
66
    {
67
        $this->originalName = $this->_name;
68
        $this->_name = $name;
69
    }
70
}
71