Passed
Push — master ( f6033e...966d85 )
by kacper
05:24
created

MySQLReplicationFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 94%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 122
ccs 47
cts 50
cp 0.94
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unregisterSubscriber() 0 3 1
A __construct() 0 43 5
A registerSubscriber() 0 3 1
A run() 0 4 2
A getDbConnection() 0 3 1
A consume() 0 3 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\BinaryDataReaderException;
9
use MySQLReplication\BinLog\BinLogException;
10
use MySQLReplication\BinLog\BinLogSocketConnect;
11
use MySQLReplication\Cache\ArrayCache;
12
use MySQLReplication\Config\Config;
13
use MySQLReplication\Config\ConfigException;
14
use MySQLReplication\Event\Event;
15
use MySQLReplication\Event\RowEvent\RowEventFactory;
16
use MySQLReplication\Exception\MySQLReplicationException;
17
use MySQLReplication\Gtid\GtidException;
18
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException;
19
use MySQLReplication\Repository\MySQLRepository;
20
use MySQLReplication\Repository\RepositoryInterface;
21
use MySQLReplication\Socket\Socket;
22
use MySQLReplication\Socket\SocketException;
23
use MySQLReplication\Socket\SocketInterface;
24
use Psr\SimpleCache\CacheInterface;
25
use Psr\SimpleCache\InvalidArgumentException;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
29
30
/**
31
 * Class MySQLReplicationFactory
32
 * @package MySQLReplication
33
 */
34
class MySQLReplicationFactory
35
{
36
    /**
37
     * @var Connection
38
     */
39
    private $connection;
40
    /**
41
     * @var EventDispatcher
42
     */
43
    private $eventDispatcher;
44
    /**
45
     * @var Event
46
     */
47
    private $event;
48
49
    /**
50
     * @param Config $config
51
     * @param RepositoryInterface|null $repository
52
     * @param CacheInterface|null $cache
53
     * @param EventDispatcherInterface|null $eventDispatcher
54
     * @param SocketInterface|null $socket
55
     * @throws BinLogException
56
     * @throws ConfigException
57
     * @throws DBALException
58
     * @throws SocketException
59
     * @throws GtidException
60
     */
61 56
    public function __construct(
62
        Config $config,
63 1
        RepositoryInterface $repository = null,
64
        CacheInterface $cache = null,
65
        EventDispatcherInterface $eventDispatcher = null,
66 1
        SocketInterface $socket = null
67
    ) {
68 56
        $config::validate();
69
70 56
        if (null === $repository) {
71 56
            $this->connection = DriverManager::getConnection(
72
                [
73 56
                    'user' => Config::getUser(),
74 56
                    'password' => Config::getPassword(),
75 56
                    'host' => Config::getHost(),
76 56
                    'port' => Config::getPort(),
77 56
                    'driver' => 'pdo_mysql',
78 56
                    'charset' => Config::getCharset()
79 56
                ]
80 56
            );
81 56
            $repository = new MySQLRepository($this->connection);
82 56
        }
83 56
        if (null === $cache) {
84 56
            $cache = new ArrayCache();
85 56
        }
86 56
        if (null === $eventDispatcher) {
87 56
            $this->eventDispatcher = new EventDispatcher();
88 56
        }
89 56
        if (null === $socket) {
90 56
            $socket = new Socket();
91 56
        }
92
93 56
        $this->event = new Event(
94 56
            new BinLogSocketConnect(
95 56
                $repository,
96
                $socket
97 56
            ),
98 56
            new RowEventFactory(
99 56
                $repository,
100
                $cache
101 56
            ),
102 56
            $this->eventDispatcher,
103
            $cache
104 56
        );
105 56
    }
106
107
    /**
108
     * @param EventSubscriberInterface $eventSubscribers
109
     */
110 56
    public function registerSubscriber(EventSubscriberInterface $eventSubscribers)
111
    {
112 56
        $this->eventDispatcher->addSubscriber($eventSubscribers);
113 56
    }
114
115
    /**
116
     * @param EventSubscriberInterface $eventSubscribers
117
     */
118 56
    public function unregisterSubscriber(EventSubscriberInterface $eventSubscribers)
119
    {
120 56
        $this->eventDispatcher->removeSubscriber($eventSubscribers);
121 56
    }
122
123
    /**
124
     * @return Connection
125
     */
126 56
    public function getDbConnection()
127 1
    {
128 56
        return $this->connection;
129
    }
130
131
    /**
132
     * @throws MySQLReplicationException
133
     * @throws InvalidArgumentException
134
     * @throws BinLogException
135
     * @throws BinaryDataReaderException
136
     * @throws JsonBinaryDecoderException
137
     * @throws SocketException
138
     */
139 56
    public function consume()
140
    {
141 56
        $this->event->consume();
142 56
    }
143
144
    /**
145
     * @throws SocketException
146
     * @throws JsonBinaryDecoderException
147
     * @throws BinaryDataReaderException
148
     * @throws BinLogException
149
     * @throws InvalidArgumentException
150
     * @throws MySQLReplicationException
151
     */
152
    public function run()
153
    {
154
        while (1) {
155
            $this->consume();
156
        }
157
    }
158
}