Passed
Push — env_slave ( 983dee...2fb48c )
by Akihito
01:46
created

NamedPdoEnvModule::configureSingleDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ConnectionLocator;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Ray\Di\AbstractModule;
10
11
class NamedPdoEnvModule extends AbstractModule
12
{
13
    public const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
14
15
    private string $qualifer;
16
    private string $dsn;
17
    private string $username;
18
    private string $password;
19
    private string $slave;
20
21
    /** @var array<string> */
22
    private array $options;
23
24
    /** @var array<string> */
25
    private array $queries;
26
27
    /**
28
     * @param string        $qualifer Qualifer for ExtendedPdoInterface
29
     * @param string        $dsn      Data Source Name (DSN)
30
     * @param string        $username User name for the DSN string
31
     * @param string        $password Password for the DSN string
32
     * @param string        $slave    Comma separated slave host list
33
     * @param array<string> $options  A key=>value array of driver-specific connection options
34
     * @param array<string> $queries  Queries to execute after the connection.
35
     */
36
    public function __construct(
37
        string $qualifer,
38
        string $dsn,
39
        string $username = '',
40
        string $password = '',
41
        string $slave = '',
42
        array $options = [],
43
        array $queries = []
44
    ) {
45
        $this->qualifer = $qualifer;
46
        $this->dsn = $dsn;
47
        $this->username = $username;
48
        $this->password = $password;
49
        $this->slave = $slave;
50
        $this->options = $options;
51
        $this->queries = $queries;
52
        parent::__construct();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function configure(): void
59
    {
60
        $this->slave ? $this->configureMasterSlaveDsn()
61
            : $this->configureSingleDsn();
62
    }
63
64
    private function configureSingleDsn(): void
65
    {
66
        $connection = new EnvConnection(
67
            $this->dsn, null, $this->username, $this->password, $this->options, $this->queries
68
        );
69
        $this->bind(EnvConnection::class)->annotatedWith($this->qualifer)->toInstance($connection);
70
        $this->bind(ExtendedPdoInterface::class)->annotatedWith($this->qualifer)->toProvider(
71
            NamedExtendedPdoProvider::class, $this->qualifer
72
        );
73
    }
74
75
    private function configureMasterSlaveDsn(): void
76
    {
77
        $locator = $this->getLocator();
78
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
79
    }
80
81
    private function getLocator(): ConnectionLocator
82
    {
83
            return ConnectionLocatorFactory::fromEnv(
84
                $this->dsn,
85
                $this->username,
86
                $this->password,
87
                $this->slave,
88
                $this->options,
89
                $this->queries
90
            );
91
    }
92
}
93