Gtid   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 42
ccs 20
cts 20
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncodedLength() 0 3 1
A __construct() 0 11 3
A getEncoded() 0 16 3
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\Gtid;
5
6
use MySQLReplication\BinaryDataReader\BinaryDataReader;
7
8
class Gtid
9
{
10
    private $intervals = [];
11
    private $sid;
12
13
    /**
14
     * @throws GtidException
15
     */
16 6
    public function __construct(string $gtid)
17
    {
18 6
        if (false === (bool)preg_match('/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches)) {
19 1
            throw new GtidException(GtidException::INCORRECT_GTID_MESSAGE, GtidException::INCORRECT_GTID_CODE);
20
        }
21
22 5
        $this->sid = $matches[1];
23 5
        foreach (array_filter(explode(':', $matches[2])) as $k) {
24 5
            $this->intervals[] = explode('-', $k);
25
        }
26 5
        $this->sid = str_replace('-', '', $this->sid);
27 5
    }
28
29 2
    public function getEncoded(): string
30
    {
31 2
        $buffer = pack('H*', $this->sid);
32 2
        $buffer .= BinaryDataReader::pack64bit(count($this->intervals));
33
34 2
        foreach ($this->intervals as $interval) {
35 2
            if (count($interval) !== 1) {
36 2
                $buffer .= BinaryDataReader::pack64bit((int)$interval[0]);
37 2
                $buffer .= BinaryDataReader::pack64bit((int)$interval[1]);
38
            } else {
39 2
                $buffer .= BinaryDataReader::pack64bit((int)$interval[0]);
40 2
                $buffer .= BinaryDataReader::pack64bit($interval[0] + 1);
41
            }
42
        }
43
44 2
        return $buffer;
45
    }
46
47 2
    public function getEncodedLength(): int
48
    {
49 2
        return (40 * count($this->intervals));
50
    }
51
}