AuraSqlReplicationModule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 22
c 5
b 0
f 1
dl 0
loc 48
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A installReadWriteConnection() 0 13 1
A configure() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocatorInterface;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection;
10
use Ray\AuraSqlModule\Annotation\WriteConnection;
11
use Ray\Di\AbstractModule;
12
use Ray\Di\Scope;
13
14
class AuraSqlReplicationModule extends AbstractModule
15
{
16
    private ConnectionLocatorInterface $connectionLocator;
17
    private string $qualifer;
18
19
    public function __construct(
20
        ConnectionLocatorInterface $connectionLocator,
21
        string $qualifer = '',
22
        ?AbstractModule $module = null
23
    ) {
24
        $this->connectionLocator = $connectionLocator;
25
        $this->qualifer = $qualifer;
26
27
        parent::__construct($module);
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected function configure(): void
34
    {
35
        $this->bind(ConnectionLocatorInterface::class)
36
            ->annotatedWith($this->qualifer)
37
            ->toInstance($this->connectionLocator);
38
39
        // ReadOnlyConnection when GET, otherwise WriteConnection
40
        $this->bind(ExtendedPdoInterface::class)
41
            ->annotatedWith($this->qualifer)
42
            ->toProvider(AuraSqlReplicationDbProvider::class, $this->qualifer)
43
            ->in(Scope::SINGLETON);
44
45
        // @ReadOnlyConnection @WriteConnection
46
        $this->installReadWriteConnection();
47
    }
48
49
    protected function installReadWriteConnection(): void
50
    {
51
        // @ReadOnlyConnection
52
        $this->bindInterceptor(
53
            $this->matcher->any(),
54
            $this->matcher->annotatedWith(ReadOnlyConnection::class),
55
            [AuraSqlSlaveDbInterceptor::class],
56
        );
57
        // @WriteConnection
58
        $this->bindInterceptor(
59
            $this->matcher->any(),
60
            $this->matcher->annotatedWith(WriteConnection::class),
61
            [AuraSqlMasterDbInterceptor::class],
62
        );
63
    }
64
}
65