Completed
Pull Request — master (#53)
by kacper
04:34
created

ColumnDTO::getSetValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Event\RowEvent;
5
6
use MySQLReplication\BinaryDataReader\BinaryDataReader;
7
use MySQLReplication\Definitions\ConstFieldType;
8
use MySQLReplication\Repository\FieldDTO;
9
10
class ColumnDTO
11
{
12
    private $fieldDTO;
13
    private $maxLength;
14
    private $size;
15
    private $fsp;
16
    private $lengthSize;
17
    private $precision;
18
    private $decimals;
19
    private $bits;
20
    private $bytes;
21
    private $type;
22
23 53
    public function __construct(
24
        FieldDTO $fieldDTO,
25
        int $type,
26
        int $maxLength,
27
        int $size,
28
        int $fsp,
29
        int $lengthSize,
30
        int $precision,
31
        int $decimals,
32
        int $bits,
33
        int $bytes
34
    ) {
35 53
        $this->fieldDTO = $fieldDTO;
36 53
        $this->type = $type;
37 53
        $this->maxLength = $maxLength;
38 53
        $this->size = $size;
39 53
        $this->fsp = $fsp;
40 53
        $this->lengthSize = $lengthSize;
41 53
        $this->precision = $precision;
42 53
        $this->decimals = $decimals;
43 53
        $this->bits = $bits;
44 53
        $this->bytes = $bytes;
45 53
    }
46
47 53
    public static function make(
48
        int $columnType,
49
        FieldDTO $fieldDTO,
50
        BinaryDataReader $binaryDataReader
51
    ): self {
52 53
        $maxLength = 0;
53 53
        $size = 0;
54 53
        $fsp = 0;
55 53
        $lengthSize = 0;
56 53
        $precision = 0;
57 53
        $decimals = 0;
58 53
        $bits = 0;
59 53
        $bytes = 0;
60
61 53
        if ($columnType === ConstFieldType::VARCHAR) {
62 6
            $maxLength = $binaryDataReader->readInt16();
63 51
        } else if ($columnType === ConstFieldType::DOUBLE) {
64 1
            $size = $binaryDataReader->readUInt8();
65 50
        } else if ($columnType === ConstFieldType::FLOAT) {
66 1
            $size = $binaryDataReader->readUInt8();
67 49
        } else if ($columnType === ConstFieldType::TIMESTAMP2) {
68 2
            $fsp = $binaryDataReader->readUInt8();
69 47
        } else if ($columnType === ConstFieldType::DATETIME2) {
70 4
            $fsp = $binaryDataReader->readUInt8();
71 44
        } else if ($columnType === ConstFieldType::TIME2) {
72 2
            $fsp = $binaryDataReader->readUInt8();
73 43
        } else if ($columnType === ConstFieldType::VAR_STRING || $columnType === ConstFieldType::STRING) {
74 5
            $metadata = ($binaryDataReader->readUInt8() << 8) + $binaryDataReader->readUInt8();
75 5
            $realType = $metadata >> 8;
76 5
            if ($realType === ConstFieldType::SET || $realType === ConstFieldType::ENUM) {
77 2
                $columnType = $realType;
78 2
                $size = $metadata & 0x00ff;
79
            } else {
80 5
                $maxLength = ((($metadata >> 4) & 0x300) ^ 0x300) + ($metadata & 0x00ff);
81
            }
82 38
        } else if ($columnType === ConstFieldType::BLOB || $columnType === ConstFieldType::IGNORE) {
83 5
            $lengthSize = $binaryDataReader->readUInt8();
84 34
        } else if ($columnType === ConstFieldType::GEOMETRY) {
85 1
            $lengthSize = $binaryDataReader->readUInt8();
86 33
        } else if ($columnType === ConstFieldType::JSON) {
87
            $lengthSize = $binaryDataReader->readUInt8();
88 33
        } else if ($columnType === ConstFieldType::NEWDECIMAL) {
89 10
            $precision = $binaryDataReader->readUInt8();
90 10
            $decimals = $binaryDataReader->readUInt8();
91 23
        } else if ($columnType === ConstFieldType::BIT) {
92 1
            $bits = $binaryDataReader->readUInt8();
93 1
            $bytes = $binaryDataReader->readUInt8();
94
95 1
            $bits = ($bytes * 8) + $bits;
96 1
            $bytes = (int)(($bits + 7) / 8);
97
        }
98
99 53
        return new self(
100 53
            $fieldDTO,
101 53
            $columnType,
102 53
            $maxLength,
103 53
            $size,
104 53
            $fsp,
105 53
            $lengthSize,
106 53
            $precision,
107 53
            $decimals,
108 53
            $bits,
109 53
            $bytes
110
        );
111
    }
112
113
    public function getFieldDTO(): FieldDTO
114
    {
115
        return $this->fieldDTO;
116
    }
117
118 8
    public function getMaxLength(): int
119
    {
120 8
        return $this->maxLength;
121
    }
122
123 2
    public function getSize(): int
124
    {
125 2
        return $this->size;
126
    }
127
128 5
    public function getFsp(): int
129
    {
130 5
        return $this->fsp;
131
    }
132
133 6
    public function getLengthSize(): int
134
    {
135 6
        return $this->lengthSize;
136
    }
137
138 10
    public function getPrecision(): int
139
    {
140 10
        return $this->precision;
141
    }
142
143 10
    public function getDecimals(): int
144
    {
145 10
        return $this->decimals;
146
    }
147
148 1
    public function getBits(): int
149
    {
150 1
        return $this->bits;
151
    }
152
153 1
    public function getBytes(): int
154
    {
155 1
        return $this->bytes;
156
    }
157
158 52
    public function getType(): int
159
    {
160 52
        return $this->type;
161
    }
162
163 52
    public function getName(): string
164
    {
165 52
        return $this->fieldDTO->getColumnName();
166
    }
167
168 1
    public function getEnumValues(): array
169
    {
170 1
        if ($this->type === ConstFieldType::ENUM) {
171 1
            return explode(',', str_replace(['enum(', ')', '\''], '', $this->fieldDTO->getColumnType()));
172
        }
173
174
        return [];
175
    }
176
177 1
    public function getSetValues(): array
178
    {
179 1
        if ($this->type === ConstFieldType::SET) {
180 1
            return explode(',', str_replace(['set(', ')', '\''], '', $this->fieldDTO->getColumnType()));
181
        }
182
183
        return [];
184
    }
185
186 18
    public function isUnsigned(): bool
187
    {
188 18
        return !(stripos($this->fieldDTO->getColumnType(), 'unsigned') === false);
189
    }
190
191
    public function isPrimary(): bool
192
    {
193
        return $this->fieldDTO->getColumnKey() === 'PRI';
194
    }
195
}