Passed
Push — 1.x ( 93745e...3395ce )
by Akihito
01:56 queued 45s
created

NamedPdoModule::configureMasterSlaveDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocator;
8
use Aura\Sql\ExtendedPdo;
9
use Aura\Sql\ExtendedPdoInterface;
10
use Override;
11
use Ray\Di\AbstractModule;
12
use SensitiveParameter;
13
14
final class NamedPdoModule extends AbstractModule
15
{
16
    public const string PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 16 at column 24
Loading history...
17
18
    /**
19
     * @param string        $qualifer Qualifer for ExtendedPdoInterface
20
     * @param string        $dsn      Data Source Name (DSN)
21
     * @param string        $username User name for the DSN string
22
     * @param string        $password Password for the DSN string
23
     * @param string        $slave    Comma separated slave host list
24
     * @param array<string> $options  A key=>value array of driver-specific connection options
25
     * @param array<string> $queries  Queries to execute after the connection.
26
     */
27
    public function __construct(
28
        private readonly string $qualifer,
29
        private readonly string $dsn,
30
        private readonly string $username = '',
31
        #[SensitiveParameter]
32
        private readonly string $password = '',
33
        private readonly string $slave = '',
34
        private readonly array $options = [],
35
        private readonly array $queries = []
36
    ) {
37
        parent::__construct();
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 6
    #[Override]
44
    protected function configure(): void
45
    {
46
        $this->slave ? $this->configureMasterSlaveDsn()
47
            : $this->configureSingleDsn();
48
    }
49
50 6
    private function configureSingleDsn(): void
51 6
    {
52 6
        $this->bind(ExtendedPdoInterface::class)
53 6
            ->annotatedWith($this->qualifer)
54 6
            ->toConstructor(
55 6
                ExtendedPdo::class,
56 6
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password",
57
            );
58
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
59
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
60
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
61 6
    }
62
63 6
    private function configureMasterSlaveDsn(): void
64 3
    {
65 6
        $locator = $this->getLocator();
66
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
67 3
    }
68
69 3
    private function getLocator(): ConnectionLocator
70 3
    {
71 3
        return ConnectionLocatorFactory::fromInstance(
72 3
            $this->dsn,
73 3
            $this->username,
74
            $this->password,
75 3
            $this->slave,
76 3
            $this->options,
77 3
            $this->queries,
78 3
        );
79
    }
80
}
81