AuraSqlReplicationModule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 3
dl 0
loc 9
rs 10
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