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

MySQLReplicationFactory::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 30
ccs 23
cts 23
cp 1
crap 1
rs 8.8571
c 0
b 0
f 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 54
        $cache = new ArrayCache();
71
72 54
        $rowEventService = new RowEventFactory(
73 54
            $repository,
74 54
            new JsonBinaryDecoderFactory(),
75
            $cache
76 54
        );
77 54
        $this->eventDispatcher = new EventDispatcher();
78
79 54
        $this->event = new Event(
80 54
            new BinLogSocketConnect($repository, new Socket()),
81 54
            new BinaryDataReaderFactory(),
82 54
            $rowEventService,
83 54
            $this->eventDispatcher,
84
            $cache
85 54
        );
86 54
    }
87
88
    /**
89
     * @param EventSubscribers $eventSubscribers
90
     */
91 54
    public function registerSubscriber(EventSubscribers $eventSubscribers)
92
    {
93 54
        $this->eventDispatcher->addSubscriber($eventSubscribers);
94 54
    }
95
96
    /**
97
     * @param EventSubscribers $eventSubscribers
98
     */
99
    public function unregisterSubscriber(EventSubscribers $eventSubscribers)
100
    {
101
        $this->eventDispatcher->removeSubscriber($eventSubscribers);
102
    }
103
104
    /**
105
     * @return Connection
106
     */
107 54
    public function getDbConnection()
108
    {
109 54
        return $this->connection;
110 1
    }
111
112
    /**
113
     * @throws MySQLReplicationException
114
     * @throws InvalidArgumentException
115
     * @throws BinLogException
116
     * @throws BinaryDataReaderException
117
     * @throws ConfigException
118
     * @throws EventException
119
     * @throws JsonBinaryDecoderException
120
     * @throws SocketException
121
     */
122 54
    public function consume()
123
    {
124 54
        $this->event->consume();
125 54
    }
126
127
    /**
128
     * @throws MySQLReplicationException
129
     * @throws InvalidArgumentException
130
     * @throws BinLogException
131
     * @throws BinaryDataReaderException
132
     * @throws ConfigException
133
     * @throws EventException
134
     * @throws JsonBinaryDecoderException
135
     * @throws SocketException
136
     */
137 1
    public function run()
138 1
    {
139
        while (1) {
140
            $this->consume();
141 1
        }
142
    }
143
}