Passed
Push — master ( 80ac3a...2487f9 )
by kacper
05:49
created

Event::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MySQLReplication\Event;
4
5
use MySQLReplication\BinaryDataReader\BinaryDataReader;
6
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
7
use MySQLReplication\BinLog\BinLogException;
8
use MySQLReplication\BinLog\BinLogSocketConnect;
9
use MySQLReplication\Config\Config;
10
use MySQLReplication\Definitions\ConstEventType;
11
use MySQLReplication\Event\DTO\EventDTO;
12
use MySQLReplication\Event\DTO\FormatDescriptionEventDTO;
13
use MySQLReplication\Event\DTO\HeartbeatDTO;
14
use MySQLReplication\Event\RowEvent\RowEventFactory;
15
use MySQLReplication\Exception\MySQLReplicationException;
16
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException;
17
use MySQLReplication\Socket\SocketException;
18
use Psr\SimpleCache\CacheInterface;
19
use Psr\SimpleCache\InvalidArgumentException;
20
use Symfony\Component\EventDispatcher\EventDispatcher;
21
22
/**
23
 * Class Event
24
 * @package MySQLReplication\Event
25
 */
26
class Event
27
{
28
    /**
29
     * @var BinLogSocketConnect
30
     */
31
    private $binLogSocketConnect;
32
    /**
33
     * @var RowEventFactory
34
     */
35
    private $rowEventFactory;
36
    /**
37
     * @var EventDispatcher
38
     */
39
    private $eventDispatcher;
40
    /**
41
     * @var CacheInterface
42
     */
43
    private $cache;
44
45
    /**
46
     * BinLogPack constructor.
47
     * @param BinLogSocketConnect $binLogSocketConnect
48
     * @param RowEventFactory $rowEventFactory
49
     * @param EventDispatcher $eventDispatcher
50
     * @param CacheInterface $cache
51
     */
52 54
    public function __construct(
53
        BinLogSocketConnect $binLogSocketConnect,
54
        RowEventFactory $rowEventFactory,
55
        EventDispatcher $eventDispatcher,
56
        CacheInterface $cache
57
    ) {
58 54
        $this->binLogSocketConnect = $binLogSocketConnect;
59 54
        $this->rowEventFactory = $rowEventFactory;
60 54
        $this->eventDispatcher = $eventDispatcher;
61 54
        $this->cache = $cache;
62 54
    }
63
64
    /**
65
     * @throws BinaryDataReaderException
66
     * @throws BinLogException
67
     * @throws EventException
68
     * @throws MySQLReplicationException
69
     * @throws JsonBinaryDecoderException
70
     * @throws InvalidArgumentException
71
     * @throws SocketException
72
     */
73 54
    public function consume()
74
    {
75 54
        $binaryDataReader = new BinaryDataReader($this->binLogSocketConnect->getResponse());
76
77
        // "ok" value on first byte continue
78 54
        $binaryDataReader->advance(1);
79
80
        // decode all events data
81 54
        $eventInfo = $this->createEventInfo($binaryDataReader);
82
83 54
        $eventDTO = null;
84
85
        // always parse table map event but propagate when needed (we need this for creating table cache)
86 54
        if (ConstEventType::TABLE_MAP_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
87 52
            $eventDTO = $this->rowEventFactory->makeRowEvent($binaryDataReader, $eventInfo)->makeTableMapDTO();
88 52
        }
89
90 54
        if (!Config::checkEvent($eventInfo->getType())) {
0 ignored issues
show
Bug introduced by
$eventInfo->getType() of type string is incompatible with the type integer expected by parameter $type of MySQLReplication\Config\Config::checkEvent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        if (!Config::checkEvent(/** @scrutinizer ignore-type */ $eventInfo->getType())) {
Loading history...
91 3
            return;
92
        }
93
94 54
        if (in_array(
95 54
            $eventInfo->getType(), [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], true
96 54
        )) {
97 1
            $eventDTO = $this->rowEventFactory->makeRowEvent($binaryDataReader, $eventInfo)->makeUpdateRowsDTO();
98 54
        } else if (in_array(
99 54
            $eventInfo->getType(), [ConstEventType::WRITE_ROWS_EVENT_V1, ConstEventType::WRITE_ROWS_EVENT_V2], true
100 54
        )) {
101 52
            $eventDTO = $this->rowEventFactory->makeRowEvent($binaryDataReader, $eventInfo)->makeWriteRowsDTO();
102 54
        } else if (in_array(
103 54
            $eventInfo->getType(), [ConstEventType::DELETE_ROWS_EVENT_V1, ConstEventType::DELETE_ROWS_EVENT_V2], true
104 54
        )) {
105 1
            $eventDTO = $this->rowEventFactory->makeRowEvent($binaryDataReader, $eventInfo)->makeDeleteRowsDTO();
106 54
        } else if (ConstEventType::XID_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
107 3
            $eventDTO = (new XidEvent($eventInfo, $binaryDataReader))->makeXidDTO();
108 54
        } else if (ConstEventType::ROTATE_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
109
            $this->cache->clear();
110
            $eventDTO = (new RotateEvent($eventInfo, $binaryDataReader))->makeRotateEventDTO();
111 54
        } else if (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
112
            $eventDTO = (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO();
113 54
        } else if (ConstEventType::QUERY_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
114 54
            $eventDTO = (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO();
115 54
        } else if (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
116
            $eventDTO = (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO();
117 54
        } else if (ConstEventType::FORMAT_DESCRIPTION_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
118 54
            $eventDTO = new FormatDescriptionEventDTO($eventInfo);
119 54
        } else if (ConstEventType::HEARTBEAT_LOG_EVENT === $eventInfo->getType()) {
0 ignored issues
show
introduced by
The condition MySQLReplication\Definit...= $eventInfo->getType() is always false.
Loading history...
120
            $eventDTO = new HeartbeatDTO($eventInfo);
121
        }
122
123 54
        $this->dispatch($eventDTO);
124 54
    }
125
126
    /**
127
     * @param BinaryDataReader $binaryDataReader
128
     * @return EventInfo
129
     */
130 54
    private function createEventInfo(BinaryDataReader $binaryDataReader)
131
    {
132 54
        return new EventInfo(
133 54
            $binaryDataReader->readInt32(),
134 54
            $binaryDataReader->readUInt8(),
135 54
            $binaryDataReader->readInt32(),
136 54
            $binaryDataReader->readInt32(),
137 54
            $binaryDataReader->readInt32(),
138 54
            $binaryDataReader->readUInt16(),
139 54
            $this->binLogSocketConnect->getCheckSum(),
140 54
            $this->binLogSocketConnect->getBinLogCurrent()
141 54
        );
142
    }
143
144
    /**
145
     * @param EventDTO $eventDTO
146
     */
147 54
    private function dispatch(EventDTO $eventDTO = null)
148
    {
149 54
        if (null !== $eventDTO) {
150 54
            $this->eventDispatcher->dispatch($eventDTO->getType(), $eventDTO);
151 54
        }
152 54
    }
153
}
154