AuraSqlModule::configureSingleDsn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 12
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ExtendedPdo;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Ray\Di\AbstractModule;
10
use Ray\Di\Scope;
11
12
class AuraSqlModule extends AbstractModule
13
{
14
    public const PARSE_PDO_DSN_REGEX = '/(.*?):(?:(host|server)=.*?;)?(.*)/i';
15
16
    private string $dsn;
17
    private string $user;
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        $dsnKey      Data Source Name (DSN)
29
     * @param string        $user        User name for the DSN string
30
     * @param string        $passwordKey Password for the DSN string
31
     * @param string        $slaveKey    Comma separated slave host list
32
     * @param array<string> $options     A key=>value array of driver-specific connection options
33
     * @param array<string> $queries
34
     */
35
    public function __construct(
36
        string $dsnKey,
37
        string $user = '',
38
        string $passwordKey = '',
39
        string $slaveKey = '',
40
        array $options = [],
41
        array $queries = []
42
    ) {
43
        $this->dsn = $dsnKey;
44
        $this->user = $user;
45
        $this->password = $passwordKey;
46
        $this->slave = $slaveKey;
47
        $this->options = $options;
48
        $this->queries = $queries;
49
        parent::__construct();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function configure(): void
56
    {
57
        $this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn();
58
        $this->install(new AuraSqlBaseModule($this->dsn));
59
    }
60
61
    private function configureSingleDsn(): void
62
    {
63
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
64
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
65
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
66
        $this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave);
67
        $this->bind()->annotatedWith('pdo_options')->toInstance($this->options);
68
        $this->bind()->annotatedWith('pdo_queries')->toInstance($this->queries);
69
        $this->bind(ExtendedPdoInterface::class)->toConstructor(
70
            ExtendedPdo::class,
71
            'dsn=pdo_dsn,username=pdo_user,password=pdo_pass,options=pdo_options,queries=pdo_queries'
72
        )->in(Scope::SINGLETON);
73
    }
74
75
    private function configureMasterSlaveDsn(): void
76
    {
77
        $locator = ConnectionLocatorFactory::fromInstance(
78
            $this->dsn,
79
            $this->user,
80
            $this->password,
81
            $this->slave,
82
            $this->options,
83
            $this->queries
84
        );
85
        $this->install(new AuraSqlReplicationModule($locator));
86
    }
87
}
88