Completed
Push — master ( ce0d98...2b6032 )
by Mehmet
02:11
created

SQLTable::buildIndexes()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 15
cts 15
cp 1
rs 8.8571
cc 5
eloc 11
nc 8
nop 0
crap 5
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 = null;
14
    protected $collection = null;
15
    protected $fields = null;
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 3
    public function __construct($schemaManager, $collection, $fields)
30
    {
31 3
        $this->schemaManager = $schemaManager;
32 3
        $this->collection = $collection;
33 3
        $this->fields = $fields;
34 3
    }
35
36 3
    public function createTable()
37
    {
38 3
        $this->buildColumns();
39 3
        $this->buildIndexes();
40 3
        $table = new Table($this->collection, $this->columns, $this->indexes);
41 3
        return $this->schemaManager->createTable($table);
42
    }
43
44 3
    protected function buildColumns()
45
    {
46 3
        $this->columns[] = new Column('id', Type::getType('integer'), ['unsigned' => true, 'autoincrement' => true] );
47 3
        foreach ($this->fields as $field){
48 3
            $field = array_merge(self::$columnDefaults, $field);
49 3
            $options = [];
50 3
            if ($field['type'] == 'integer' && $field['type_info'] == 'unsigned') {
51
                $options['unsigned'] = true;
52
            }
53 3
            $options['length'] = $field['maxLength'];
54 3
            $options['default'] = $field['default'];        
55 3
            $this->columns[] = new Column($field['name'], Type::getType($field['type']), $options );
56 3
        }
57 3
    }
58
59 3
    protected function buildIndexes()
60
    {
61 3
        $this->indexes[] = new Index($this->collection.'_PK', ['id'], false, true);
62 3
        foreach ($this->fields as $field){
63 3
            $field = array_merge(self::$columnDefaults, $field);
64 3
            if ($field['index'] !== null) {
65 3
                if ( $field['index_type'] == 'unique' ) {
66 3
                    $this->indexes[] = new Index($this->collection . '_' . $field['name'] . '_UNQ', [$field['name']], true, false);
67 3
                } else {
68 3
                    $this->tmpIndexes[] = $field['name'];
69
                }
70 3
            }
71 3
        }
72 3
        if(count($this->tmpIndexes)>0){
73 3
            $this->indexes[] = new Index($this->collection . '_IDX', $this->tmpIndexes, false, false);
74 3
        }
75 3
    }
76
77
    public function createOnlyIndexes()
78
    {
79
        $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...
80
        foreach ($this->indexes as $index) {
81
            $this->schemaManager->createIndex($index, $this->collection);
82
        }
83
        return true;
84
    }
85
}