Completed
Push — master ( c4f500...65ebe1 )
by Mehmet
03:13 queued 33s
created

SQLTable   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 89
ccs 0
cts 49
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createTable() 0 7 1
A buildColumns() 0 18 4
B buildIndexes() 0 27 5
A createOnlyIndexes() 0 8 2
1
<?php
2
3
4
namespace Soupmix;
5
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Schema\Column;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\DBAL\Schema\Index;
10
11
class SQLTable
12
{
13
    protected $schemaManager ;
14
    protected $collection;
15
    protected $fields;
16
    protected $columns = [];
17
    protected $indexes = [];
18
    protected $tmpIndexes = [];
19
    protected static $columnDefaults = [
20
        'name'      => null,
21
        'type'      => 'string',
22
        'type_info' => null,
23
        'maxLength' => 255,
24
        'default'   => null,
25
        'index'     => null,
26
        'index_type'=> null,
27
    ];
28
29
    public function __construct($schemaManager, $collection, $fields)
30
    {
31
        $this->schemaManager = $schemaManager;
32
        $this->collection = $collection;
33
        $this->fields = $fields;
34
    }
35
36
    public function createTable()
37
    {
38
        $this->buildColumns();
39
        $this->buildIndexes();
40
        $table = new Table($this->collection, $this->columns, $this->indexes);
41
        return $this->schemaManager->createTable($table);
42
    }
43
44
    protected function buildColumns()
45
    {
46
        $this->columns[] = new Column(
47
            'id',
48
            Type::getType('integer'),
49
            ['unsigned' => true, 'autoincrement' => true]
50
        );
51
        foreach ($this->fields as $field) {
52
            $field = array_merge(self::$columnDefaults, $field);
53
            $options = [];
54
            if ($field['type'] === 'integer' && $field['type_info'] === 'unsigned') {
55
                $options['unsigned'] = true;
56
            }
57
            $options['length'] = $field['maxLength'];
58
            $options['default'] = $field['default'];
59
            $this->columns[] = new Column($field['name'], Type::getType($field['type']), $options);
60
        }
61
    }
62
63
    protected function buildIndexes()
64
    {
65
        $this->indexes[] = new Index($this->collection.'_PK', ['id'], false, true);
66
        foreach ($this->fields as $field) {
67
            $field = array_merge(self::$columnDefaults, $field);
68
            if ($field['index'] !== null) {
69
                if ($field['index_type'] === 'unique') {
70
                    $this->indexes[] = new Index(
71
                        $this->collection . '_' . $field['name'] . '_UNQ',
72
                        [$field['name']],
73
                        true,
74
                        false
75
                    );
76
                    continue;
77
                }
78
                $this->tmpIndexes[] = $field['name'];
79
            }
80
        }
81
        if (count($this->tmpIndexes)>0) {
82
            $this->indexes[] = new Index(
83
                $this->collection . '_IDX',
84
                $this->tmpIndexes,
85
                false,
86
                false
87
            );
88
        }
89
    }
90
91
    public function createOnlyIndexes()
92
    {
93
        $this->createIndexes();
0 ignored issues
show
Bug introduced by
The method createIndexes() does not seem to exist on object<Soupmix\SQLTable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        foreach ($this->indexes as $index) {
95
            $this->schemaManager->createIndex($index, $this->collection);
96
        }
97
        return true;
98
    }
99
}
100