SQLTable::buildIndexes()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 20
cts 20
cp 1
rs 8.439
cc 5
eloc 19
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 ;
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 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(
47 3
            'id',
48 3
            Type::getType('integer'),
49 3
            ['unsigned' => true, 'autoincrement' => true]
50
        );
51 3
        foreach ($this->fields as $field) {
52 3
            $field = array_merge(self::$columnDefaults, $field);
53 3
            $options = [];
54 3
            if ($field['type'] === 'integer' && $field['type_info'] === 'unsigned') {
55
                $options['unsigned'] = true;
56
            }
57 3
            $options['length'] = $field['maxLength'];
58 3
            $options['default'] = $field['default'];
59 3
            $this->columns[] = new Column($field['name'], Type::getType($field['type']), $options);
60
        }
61 3
    }
62
63 3
    protected function buildIndexes()
64
    {
65 3
        $this->indexes[] = new Index($this->collection.'_PK', ['id'], false, true);
66 3
        foreach ($this->fields as $field) {
67 3
            $field = array_merge(self::$columnDefaults, $field);
68 3
            if ($field['index'] !== null) {
69 3
                if ($field['index_type'] === 'unique') {
70 3
                    $this->indexes[] = new Index(
71 3
                        $this->collection . '_' . $field['name'] . '_UNQ',
72 3
                        [$field['name']],
73 3
                        true,
74 3
                        false
75
                    );
76 3
                    continue;
77
                }
78 3
                $this->tmpIndexes[] = $field['name'];
79
            }
80
        }
81 3
        if (count($this->tmpIndexes)>0) {
82 3
            $this->indexes[] = new Index(
83 3
                $this->collection . '_IDX',
84 3
                $this->tmpIndexes,
85 3
                false,
86 3
                false
87
            );
88
        }
89 3
    }
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