TableIndexTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableIndexType() 0 16 5
A makeTableIndex() 0 10 2
A indexes() 0 7 2
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\MySql\Db\Traits;
4
5
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
6
use Lagdo\DbAdmin\Driver\Entity\IndexEntity;
7
8
trait TableIndexTrait
9
{
10
    /**
11
     * @param array $row
12
     *
13
     * @return string
14
     */
15
    private function getTableIndexType(array $row): string
16
    {
17
        $name = $row['Key_name'];
18
        if ($name === 'PRIMARY') {
19
            return 'PRIMARY';
20
        }
21
        if ($row['Index_type'] === 'FULLTEXT') {
22
            return 'FULLTEXT';
23
        }
24
        if (!$row['Non_unique']) {
25
            return 'UNIQUE';
26
        }
27
        if ($row['Index_type'] === 'SPATIAL') {
28
            return 'SPATIAL';
29
        }
30
        return 'INDEX';
31
    }
32
33
    /**
34
     * @param array $row
35
     *
36
     * @return IndexEntity
37
     */
38
    private function makeTableIndex(array $row): IndexEntity
39
    {
40
        $index = new IndexEntity();
41
42
        $index->type = $this->getTableIndexType($row);
43
        $index->columns[] = $row['Column_name'];
44
        $index->lengths[] = ($row['Index_type'] == 'SPATIAL' ? null : $row['Sub_part']);
45
        $index->descs[] = null;
46
47
        return $index;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function indexes(string $table)
54
    {
55
        $indexes = [];
56
        foreach ($this->driver->rows('SHOW INDEX FROM ' . $this->driver->escapeTableName($table)) as $row) {
57
            $indexes[$row['Key_name']] = $this->makeTableIndex($row);
58
        }
59
        return $indexes;
60
    }
61
}
62