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

MySQLReplicationFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 93.88%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 46
cts 49
cp 0.9388
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unregisterSubscriber() 0 3 1
B __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\EventException;
16
use MySQLReplication\Event\RowEvent\RowEventFactory;
17
use MySQLReplication\Exception\MySQLReplicationException;
18
use MySQLReplication\Gtid\GtidException;
19
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderException;
20
use MySQLReplication\Repository\MySQLRepository;
21
use MySQLReplication\Repository\RepositoryInterface;
22
use MySQLReplication\Socket\Socket;
23
use MySQLReplication\Socket\SocketException;
24
use MySQLReplication\Socket\SocketInterface;
25
use Psr\SimpleCache\CacheInterface;
26
use Psr\SimpleCache\InvalidArgumentException;
27
use Symfony\Component\EventDispatcher\EventDispatcher;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
30
31
/**
32
 * Class MySQLReplicationFactory
33
 * @package MySQLReplication
34
 */
35
class MySQLReplicationFactory
36
{
37
    /**
38
     * @var Connection
39
     */
40
    private $connection;
41
    /**
42
     * @var EventDispatcher
43
     */
44
    private $eventDispatcher;
45
    /**
46
     * @var Event
47
     */
48
    private $event;
49
50
    /**
51
     * @param Config $config
52
     * @param RepositoryInterface|null $repository
53
     * @param CacheInterface|null $cache
54
     * @param EventDispatcherInterface|null $eventDispatcher
55
     * @param SocketInterface|null $socket
56
     * @throws BinLogException
57
     * @throws ConfigException
58
     * @throws DBALException
59
     * @throws SocketException
60
     * @throws GtidException
61
     */
62 54
    public function __construct(
63 1
        Config $config,
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-unused */ Config $config,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
        RepositoryInterface $repository = null,
65
        CacheInterface $cache = null,
66 1
        EventDispatcherInterface $eventDispatcher = null,
67
        SocketInterface $socket = null
68
    ) {
69 54
        Config::validate();
70
71 54
        if (null === $repository) {
72 54
            $this->connection = DriverManager::getConnection(
73
                [
74 54
                    'user' => Config::getUser(),
75 54
                    'password' => Config::getPassword(),
76 54
                    'host' => Config::getHost(),
77 54
                    'port' => Config::getPort(),
78 54
                    'driver' => 'pdo_mysql',
79 54
                    'charset' => Config::getCharset()
80 54
                ]
81 54
            );
82 54
            $repository = new MySQLRepository($this->connection);
83 54
        }
84 54
        if (null === $cache) {
85 54
            $cache = new ArrayCache();
86 54
        }
87 54
        if (null === $eventDispatcher) {
88 54
            $this->eventDispatcher = new EventDispatcher();
89 54
        }
90 54
        if (null === $socket) {
91 54
            $socket = new Socket();
92 54
        }
93
94 54
        $this->event = new Event(
95 54
            new BinLogSocketConnect(
96 54
                $repository,
97
                $socket
98 54
            ),
99 54
            new RowEventFactory(
100 54
                $repository,
101
                $cache
102 54
            ),
103 54
            $this->eventDispatcher,
104
            $cache
105 54
        );
106 54
    }
107
108
    /**
109
     * @param EventSubscriberInterface $eventSubscribers
110
     */
111 54
    public function registerSubscriber(EventSubscriberInterface $eventSubscribers)
112
    {
113 54
        $this->eventDispatcher->addSubscriber($eventSubscribers);
114 54
    }
115
116
    /**
117
     * @param EventSubscriberInterface $eventSubscribers
118
     */
119 54
    public function unregisterSubscriber(EventSubscriberInterface $eventSubscribers)
120
    {
121 54
        $this->eventDispatcher->removeSubscriber($eventSubscribers);
122 54
    }
123
124
    /**
125
     * @return Connection
126
     */
127 54
    public function getDbConnection()
128
    {
129 54
        return $this->connection;
130
    }
131
132
    /**
133
     * @throws MySQLReplicationException
134
     * @throws InvalidArgumentException
135
     * @throws BinLogException
136
     * @throws BinaryDataReaderException
137
     * @throws EventException
138
     * @throws JsonBinaryDecoderException
139
     * @throws SocketException
140
     */
141 54
    public function consume()
142
    {
143 54
        $this->event->consume();
144 54
    }
145
146
    /**
147
     * @throws SocketException
148
     * @throws JsonBinaryDecoderException
149
     * @throws EventException
150
     * @throws BinaryDataReaderException
151
     * @throws BinLogException
152
     * @throws InvalidArgumentException
153
     * @throws MySQLReplicationException
154
     */
155
    public function run()
156
    {
157
        while (1) {
158
            $this->consume();
159
        }
160
    }
161
}