NamedPdoModule::configureSingleDsn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
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\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
28
    /**
29
     * @param string        $qualifer Qualifer for ExtendedPdoInterface
30
     * @param string        $dsn      Data Source Name (DSN)
31
     * @param string        $username User name for the DSN string
32
     * @param string        $password Password for the DSN string
33
     * @param string        $slave    Comma separated slave host list
34
     * @param array<string> $options  A key=>value array of driver-specific connection options
35
     * @param array<string> $queries  Queries to execute after the connection.
36
     */
37
    public function __construct(
38
        string $qualifer,
39
        string $dsn,
40
        string $username = '',
41
        string $password = '',
42
        string $slave = '',
43
        array $options = [],
44
        array $queries = []
45
    ) {
46
        $this->qualifer = $qualifer;
47
        $this->dsn = $dsn;
48
        $this->username = $username;
49
        $this->password = $password;
50
        $this->slave = $slave;
51
        $this->options = $options;
52
        $this->queries = $queries;
53
        parent::__construct();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function configure(): void
60
    {
61
        $this->slave ? $this->configureMasterSlaveDsn()
62
            : $this->configureSingleDsn();
63
    }
64
65
    private function configureSingleDsn(): void
66
    {
67
        $this->bind(ExtendedPdoInterface::class)
68
            ->annotatedWith($this->qualifer)
69
            ->toConstructor(
70
                ExtendedPdo::class,
71
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password"
72
            );
73
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
74
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
75
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
76
    }
77
78
    private function configureMasterSlaveDsn(): void
79
    {
80
        $locator = $this->getLocator();
81
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
82
    }
83
84
    private function getLocator(): ConnectionLocator
85
    {
86
        return ConnectionLocatorFactory::fromInstance(
87
            $this->dsn,
88
            $this->username,
89
            $this->password,
90
            $this->slave,
91
            $this->options,
92
            $this->queries
93
        );
94
    }
95
}
96