Index::generateIndexName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
6
use LaravelDoctrine\Fluent\Buildable;
7
8
class Index implements Buildable
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $separator = '_';
14
15
    /**
16
     * Suffix to be added to the index key name
17
     *
18
     * @var string
19
     */
20
    protected $suffix = 'index';
21
22
    /**
23
     * @var ClassMetadataBuilder
24
     */
25
    protected $builder;
26
27
    /**
28
     * @var array
29
     */
30
    protected $columns;
31
32
    /**
33
     * @var string|null
34
     */
35
    protected $name = null;
36
37
    /**
38
     * @param ClassMetadataBuilder $builder
39
     * @param string[]             $columns
40
     */
41 12
    public function __construct(ClassMetadataBuilder $builder, array $columns)
42
    {
43 12
        $this->builder = $builder;
44 12
        $this->columns = $columns;
45 12
    }
46
47
    /**
48
     * Execute the build process
49
     */
50 3
    public function build()
51
    {
52 3
        $this->builder->addIndex(
53 3
            $this->getColumns(),
54 3
            $this->getName()
55 3
        );
56 3
    }
57
58
    /**
59
     * @param string $name
60
     *
61
     * @return $this
62
     */
63 2
    public function name($name)
64
    {
65 2
        $this->name = $name;
66
67 2
        return $this;
68
    }
69
70
    /**
71
     * @return string[]
72
     */
73 12
    public function getColumns()
74
    {
75 12
        return $this->columns;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 6
    public function getName()
82
    {
83 6
        return $this->name ?: $this->generateIndexName();
84
    }
85
86
    /**
87
     * @return string
88
     */
89 4
    protected function generateIndexName()
90
    {
91 4
        $table = $this->builder->getClassMetadata()->getTableName();
92
93 4
        return $table . $this->separator . implode($this->separator, $this->getColumns()) . $this->separator . $this->suffix;
94
    }
95
}
96