Passed
Branch master (e090ea)
by kacper
04:22
created

MySQLReplicationFactory::binLogEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 1
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace MySQLReplication;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\DriverManager;
8
use MySQLReplication\BinaryDataReader\BinaryDataReaderException;
9
use MySQLReplication\BinaryDataReader\BinaryDataReaderFactory;
10
use MySQLReplication\BinLog\BinLogException;
11
use MySQLReplication\BinLog\BinLogSocketConnect;
12
use MySQLReplication\Cache\ArrayCache;
13
use MySQLReplication\Config\Config;
14
use MySQLReplication\Config\ConfigException;
15
use MySQLReplication\Event\Event;
16
use MySQLReplication\Event\EventException;
17
use MySQLReplication\Event\EventSubscribers;
18
use MySQLReplication\Event\RowEvent\RowEventFactory;
19
use MySQLReplication\Exception\MySQLReplicationException;
20
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException;
21
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderFactory;
22
use MySQLReplication\Repository\MySQLRepository;
23
use MySQLReplication\Socket\Socket;
24
use MySQLReplication\Socket\SocketException;
25
use Psr\SimpleCache\InvalidArgumentException;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
28
/**
29
 * Class MySQLReplicationFactory
30
 * @package MySQLReplication
31
 */
32
class MySQLReplicationFactory
33
{
34
    /**
35
     * @var Connection
36
     */
37
    private $connection;
38
    /**
39
     * @var EventDispatcher
40
     */
41
    private $eventDispatcher;
42
    /**
43
     * @var Event
44
     */
45
    private $event;
46
47
    /**
48
     * @throws MySQLReplicationException
49
     * @throws DBALException
50
     * @throws ConfigException
51
     * @throws BinLogException
52
     * @throws \MySQLReplication\Gtid\GtidException
53
     * @throws \MySQLReplication\Socket\SocketException
54
     */
55 54
    public function __construct()
56
    {
57 54
        Config::validate();
58
59 54
        $this->connection = DriverManager::getConnection(
60
            [
61 54
                'user' => Config::getUser(),
62 54
                'password' => Config::getPassword(),
63 54
                'host' => Config::getHost(),
64 54
                'port' => Config::getPort(),
65 54
                'driver' => 'pdo_mysql',
66 54
                'charset' => Config::getCharset()
67 54
            ]
68 54
        );
69 54
        $repository = new MySQLRepository($this->connection);
70
71 54
        $rowEventService = new RowEventFactory(
72 54
            $repository,
73 54
            new JsonBinaryDecoderFactory(),
74 54
            new ArrayCache()
75 54
        );
76 54
        $this->eventDispatcher = new EventDispatcher();
77
78 54
        $this->event = new Event(
79 54
            new BinLogSocketConnect($repository, new Socket()),
80 54
            new BinaryDataReaderFactory(),
81 54
            $rowEventService,
82 54
            $this->eventDispatcher
83 54
        );
84 54
    }
85
86
    /**
87
     * @param EventSubscribers $eventSubscribers
88
     */
89 54
    public function registerSubscriber(EventSubscribers $eventSubscribers)
90
    {
91 54
        $this->eventDispatcher->addSubscriber($eventSubscribers);
92 54
    }
93
94
    /**
95
     * @param EventSubscribers $eventSubscribers
96
     */
97
    public function unregisterSubscriber(EventSubscribers $eventSubscribers)
98
    {
99
        $this->eventDispatcher->removeSubscriber($eventSubscribers);
100
    }
101
102
    /**
103
     * @return Connection
104
     */
105 54
    public function getDbConnection()
106
    {
107 54
        return $this->connection;
108
    }
109
110
    /**
111
     * @throws MySQLReplicationException
112
     * @throws InvalidArgumentException
113
     * @throws BinLogException
114
     * @throws BinaryDataReaderException
115
     * @throws ConfigException
116
     * @throws EventException
117
     * @throws JsonBinaryDecoderException
118
     * @throws SocketException
119
     */
120 54
    public function consume()
121
    {
122 54
        $this->event->consume();
123 54
    }
124
}
125