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

NamedPdoModule::getLocator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 20
rs 9.7666
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 Ray\Di\AbstractModule;
11
12
class NamedPdoModule extends AbstractModule
13
{
14
    public const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
15
16
    private string $qualifer;
17
    private string $dsn;
18
    private string $username;
19
    private string $password;
20
    private string $slave;
21
22
    /** @var array<string> */
23
    private array $options;
24
25
    /** @var array<string> */
26
    private array $queries;
27
    private bool $isEnv;
28
29
    /**
30
     * @param string        $qualifer Qualifer for ExtendedPdoInterface
31
     * @param string        $dsn      Data Source Name (DSN)
32
     * @param string        $username User name for the DSN string
33
     * @param string        $password Password for the DSN string
34
     * @param string        $slave    Comma separated slave host list
35
     * @param array<string> $options  A key=>value array of driver-specific connection options
36
     * @param array<string> $queries  Queries to execute after the connection.
37
     */
38
    public function __construct(
39
        string $qualifer,
40
        string $dsn,
41
        string $username = '',
42
        string $password = '',
43
        string $slave = '',
44
        array $options = [],
45
        array $queries = [],
46
        bool $isEnv = false
47
    ) {
48
        $this->qualifer = $qualifer;
49
        $this->dsn = $dsn;
50
        $this->username = $username;
51
        $this->password = $password;
52
        $this->slave = $slave;
53
        $this->options = $options;
54
        $this->queries = $queries;
55
        $this->isEnv = $isEnv;
56
        parent::__construct();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function configure(): void
63
    {
64
        $this->slave ? $this->configureMasterSlaveDsn()
65
            : $this->configureSingleDsn();
66
    }
67
68
    private function configureSingleDsn(): void
69
    {
70
        $this->bind(ExtendedPdoInterface::class)
71
            ->annotatedWith($this->qualifer)
72
            ->toConstructor(
73
                ExtendedPdo::class,
74
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password"
75
            );
76
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
77
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
78
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
79
    }
80
81
    private function configureMasterSlaveDsn(): void
82
    {
83
        $locator = $this->getLocator();
84
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
85
    }
86
87
    private function getLocator(): ConnectionLocator
88
    {
89
        if ($this->isEnv) {
90
            return ConnectionLocatorFactory::fromEnv(
91
                $this->dsn,
92
                $this->username,
93
                $this->password,
94
                $this->slave,
95
                $this->options,
96
                $this->queries
97
            );
98
        }
99
100
        return ConnectionLocatorFactory::fromInstance(
101
            $this->dsn,
102
            $this->username,
103
            $this->password,
104
            $this->slave,
105
            $this->options,
106
            $this->queries
107
        );
108
    }
109
}
110