Completed
Push — master ( 15c40a...010522 )
by kacper
01:57
created

MySQLReplicationFactory::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 1
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\BinaryDataReaderService;
9
use MySQLReplication\BinLog\BinLogAuth;
10
use MySQLReplication\BinLog\BinLogConnect;
11
use MySQLReplication\BinLog\Exception\BinLogException;
12
use MySQLReplication\Config\Config;
13
use MySQLReplication\Config\Exception\ConfigException;
14
use MySQLReplication\Event\Event;
15
use MySQLReplication\Event\EventSubscribers;
16
use MySQLReplication\Event\RowEvent\RowEventService;
17
use MySQLReplication\Exception\MySQLReplicationException;
18
use MySQLReplication\Gtid\GtidService;
19
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderFactory;
20
use MySQLReplication\Repository\MySQLRepository;
21
use Symfony\Component\EventDispatcher\EventDispatcher;
22
23
/**
24
 * Class MySQLReplicationFactory
25
 * @package MySQLReplication
26
 */
27
class MySQLReplicationFactory
28
{
29
    /**
30
     * @var EventDispatcher
31
     */
32
    private $eventDispatcher;
33
    /**
34
     * @var MySQLRepository
35
     */
36
    private $MySQLRepository;
37
    /**
38
     * @var BinLogConnect
39
     */
40
    private $binLogConnect;
41
    /**
42
     * @var Event
43
     */
44
    private $event;
45
    /**
46
     * @var BinLogAuth
47
     */
48
    private $binLogAuth;
49
    /**
50
     * @var Connection
51
     */
52
    private $connection;
53
    /**
54
     * @var BinaryDataReaderService
55
     */
56
    private $binaryDataReaderService;
57
    /**
58
     * @var GtidService
59
     */
60
    private $GtiService;
61
    /**
62
     * @var RowEventService
63
     */
64
    private $rowEventService;
65
    /**
66
     * @var JsonBinaryDecoderFactory
67
     */
68
    private $jsonBinaryDecoderFactory;
69
70
    /**
71
     * @param Config $config
72
     * @throws MySQLReplicationException
73
     * @throws DBALException
74
     * @throws ConfigException
75
     * @throws BinLogException
76
     */
77
    public function __construct(Config $config)
78
    {
79
        $config->validate();
80
81
        $this->connection = DriverManager::getConnection([
82
            'user' => $config->getUser(),
83
            'password' => $config->getPassword(),
84
            'host' => $config->getHost(),
85
            'port' => $config->getPort(),
86
            'driver' => 'pdo_mysql',
87
            'charset' => $config->getCharset()
88
        ]);
89
        $this->binLogAuth = new BinLogAuth();
90
        $this->MySQLRepository = new MySQLRepository($this->connection);
91
        $this->GtiService = new GtidService();
92
93
        $this->binLogConnect = new BinLogConnect($config, $this->MySQLRepository, $this->binLogAuth, $this->GtiService);
94
        $this->binLogConnect->connectToStream();
95
96
        $this->jsonBinaryDecoderFactory = new JsonBinaryDecoderFactory();
97
        $this->binaryDataReaderService = new BinaryDataReaderService();
98
        $this->rowEventService = new RowEventService($config, $this->MySQLRepository, $this->jsonBinaryDecoderFactory);
99
        $this->eventDispatcher = new EventDispatcher();
100
101
        $this->event = new Event(
102
            $config,
103
            $this->binLogConnect,
104
            $this->binaryDataReaderService,
105
            $this->rowEventService,
106
            $this->eventDispatcher
107
        );
108
    }
109
110
    /**
111
     * @param EventSubscribers $eventSubscribers
112
     */
113
    public function registerSubscriber(EventSubscribers $eventSubscribers)
114
    {
115
        $this->eventDispatcher->addSubscriber($eventSubscribers);
116
    }
117
118
    /**
119
     * @return Connection
120
     */
121
    public function getDbConnection()
122
    {
123
        return $this->connection;
124
    }
125
126
    /**
127
     * @throws MySQLReplicationException
128
     */
129
    public function binLogEvent()
130
    {
131
        $this->event->consume();
132
    }
133
}
134