MysqlDatabaseIndex::getIndexType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Starkerxp\DatabaseChecker\Structure;
4
5
6
use Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException;
7
use Starkerxp\DatabaseChecker\LoggerTrait;
8
9
class MysqlDatabaseIndex implements DatabaseInterface
10
{
11
    use LoggerTrait;
12
13
    private $table;
14
    private $name;
15
    private $unique;
16
    private $columns;
17
18
    /**
19
     * DatabaseColumnStructure constructor.
20
     *
21
     * @param string  $name
22
     * @param boolean $unique
23
     * @param array   $columns
24
     *
25
     * @throws \RuntimeException
26
     */
27
    public function __construct($name, array $columns, $unique)
28
    {
29
        if (empty($name)) {
30
            throw new \RuntimeException('');
31
        }
32
        $this->name = $name;
33
        $this->unique = $unique;
34
        $this->columns = $columns;
35
    }
36
37
    public function toArray()
38
    {
39
        $tmp = get_object_vars($this);
40
        unset($tmp['logger']);
41
        return $tmp;
42
    }
43
44
    /**
45
     * @return array
46
     *
47
     * @throws TablenameHasNotDefinedException
48
     */
49
    public function alterStatement()
50
    {
51
        $modifications = [];
52
        $modifications[] = $this->deleteStatement();
53
        $modifications[] = $this->createStatement()[0];
54
55
        return $modifications;
56
    }
57
58
    /**
59
     * @return string
60
     *
61
     * @throws TablenameHasNotDefinedException
62
     */
63
    public function deleteStatement()
64
    {
65
        if ($this->isPrimary()) {
66
            return sprintf('ALTER TABLE `%s` DROP PRIMARY KEY;', $this->getTable());
67
        }
68
69
        return sprintf('ALTER TABLE `%s` DROP INDEX `%s`;', $this->getTable(), $this->getName());
70
    }
71
72
73
    public function isPrimary(): bool
74
    {
75
        return strtolower($this->name) == 'primary';
76
    }
77
78
    /**
79
     * @return mixed
80
     *
81
     * @throws \Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException
82
     */
83
    public function getTable()
84
    {
85
        if (!$this->table) {
86
            $this->critical('You need to define name of your table');
87
            throw new TablenameHasNotDefinedException('table not defined');
88
        }
89
90
        return $this->table;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function getName()
97
    {
98
        return $this->name;
99
    }
100
101
    /**
102
     * @return array
103
     *
104
     * @throws TablenameHasNotDefinedException
105
     */
106
    public function createStatement()
107
    {
108
        if ($this->isPrimary()) {
109
            return [sprintf('ALTER TABLE `%s` ADD PRIMARY KEY (%s);', $this->getTable(), '`' . implode('`, `', $this->getColumns()) . '`')];
110
        }
111
112
        return [sprintf('ALTER TABLE `%s` ADD %s INDEX `%s` (%s);', $this->getTable(), $this->getIndexType(), $this->getName(), '`' . implode('`, `', $this->getColumns()) . '`')];
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getColumns(): array
119
    {
120
        return $this->columns;
121
    }
122
123
    public function getIndexType(): string
124
    {
125
        if ($this->isUnique()) {
126
            return 'UNIQUE';
127
        }
128
129
        return '';
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isUnique(): bool
136
    {
137
        return $this->unique;
138
    }
139
140
    /**
141
     * @param mixed $table
142
     */
143
    public function setTable($table): void
144
    {
145
        $this->table = $table;
146
    }
147
148
149
}
150