AuraSqlLocatorModule   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 79
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A installReadWriteConnection() 0 13 1
A configure() 0 15 3
A installLocatorDb() 0 15 1
A __construct() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocatorInterface;
8
use Ray\AuraSqlModule\Annotation\AuraSql;
9
use Ray\AuraSqlModule\Annotation\Read;
10
use Ray\AuraSqlModule\Annotation\ReadOnlyConnection;
11
use Ray\AuraSqlModule\Annotation\Write;
12
use Ray\AuraSqlModule\Annotation\WriteConnection;
13
use Ray\Di\AbstractModule;
14
15
use function array_merge;
16
17
class AuraSqlLocatorModule extends AbstractModule
18
{
19
    private ConnectionLocatorInterface $connectionLocator;
20
21
    /** @var string[] */
22
    private array $readMethods;
23
24
    /** @var string[] */
25
    private array $writeMethods;
26
27
    /**
28
     * @phpstan-param array<string> $readMethods
29
     * @phpstan-param array<string> $writeMethods
30
     */
31
    public function __construct(
32
        ConnectionLocatorInterface $connectionLocator,
33
        array $readMethods = [],
34
        array $writeMethods = [],
35
        ?AbstractModule $module = null
36
    ) {
37
        $this->connectionLocator = $connectionLocator;
38
        $this->readMethods = $readMethods;
39
        $this->writeMethods = $writeMethods;
40
41
        parent::__construct($module);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function configure(): void
48
    {
49
        if ((bool) $this->readMethods && (bool) $this->writeMethods) {
50
            $this->bind()->annotatedWith(Read::class)->toInstance($this->readMethods);
51
            $this->bind()->annotatedWith(Write::class)->toInstance($this->writeMethods);
52
        }
53
54
        $this->bind(ConnectionLocatorInterface::class)->toInstance($this->connectionLocator);
55
        $methods = array_merge($this->readMethods, $this->writeMethods);
56
        // @AuraSql
57
        $this->installLocatorDb($methods);
58
        // @ReadOnlyConnection @WriteConnection
59
        $this->installReadWriteConnection();
60
        // @Transactional
61
        $this->install(new TransactionalModule());
62
    }
63
64
    protected function installReadWriteConnection(): void
65
    {
66
        // @ReadOnlyConnection
67
        $this->bindInterceptor(
68
            $this->matcher->any(),
69
            $this->matcher->annotatedWith(ReadOnlyConnection::class),
70
            [AuraSqlSlaveDbInterceptor::class],
71
        );
72
        // @WriteConnection
73
        $this->bindInterceptor(
74
            $this->matcher->any(),
75
            $this->matcher->annotatedWith(WriteConnection::class),
76
            [AuraSqlMasterDbInterceptor::class],
77
        );
78
    }
79
80
    /** @param string[] $methods */
81
    private function installLocatorDb(array $methods): void
82
    {
83
        // locator db
84
        $this->bindInterceptor(
85
            $this->matcher->annotatedWith(AuraSql::class), // @AuraSql in class
86
            $this->matcher->logicalAnd(
87
                new IsInMethodMatcher($methods),
88
                $this->matcher->logicalNot(
89
                    $this->matcher->annotatedWith(ReadOnlyConnection::class),
90
                ),
91
                $this->matcher->logicalNot(
92
                    $this->matcher->annotatedWith(Connection::class),
93
                ),
94
            ),
95
            [AuraSqlConnectionInterceptor::class],
96
        );
97
    }
98
}
99