Completed
Push — 1.x ( 980d56...edf1de )
by Akihito
14s queued 12s
created

AuraSqlModule::changeHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 2
rs 10
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 8
        $this->install(new AuraSqlBaseModule($this->dsn));
59
    }
60 8
61 8
    private function configureSingleDsn(): void
62 8
    {
63 8
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
64 8
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
65 8
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
66 8
        $this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave);
67 8
        $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 8
        )->in(Scope::SINGLETON);
73
    }
74 8
75
    private function configureMasterSlaveDsn(): void
76 8
    {
77 8
        $locator = ConnectionLocatorFactory::fromInstance(
78 8
            $this->dsn,
79 8
            $this->user,
80 8
            $this->password,
81 8
            $this->slave,
82
            $this->options,
83 6
            $this->queries
84
        );
85 6
        $this->install(new AuraSqlReplicationModule($locator));
86 6
    }
87
}
88