NamedPdoEnvModule::configureSingleDsn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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