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

NamedPdoModule::changeHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
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 10
ccs 8
cts 8
cp 1
crap 2
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 6
        array $options = [],
44
        array $queries = []
45
    ) {
46
        $this->qualifer = $qualifer;
47
        $this->dsn = $dsn;
48
        $this->username = $username;
49
        $this->password = $password;
50 6
        $this->slave = $slave;
51 6
        $this->options = $options;
52 6
        $this->queries = $queries;
53 6
        parent::__construct();
54 6
    }
55 6
56 6
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function configure(): void
60
    {
61 6
        $this->slave ? $this->configureMasterSlaveDsn()
62
            : $this->configureSingleDsn();
63 6
    }
64 3
65 6
    private function configureSingleDsn(): void
66
    {
67 3
        $this->bind(ExtendedPdoInterface::class)
68
            ->annotatedWith($this->qualifer)
69 3
            ->toConstructor(
70 3
                ExtendedPdo::class,
71 3
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password"
72 3
            );
73 3
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
74
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
75 3
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
76 3
    }
77 3
78 3
    private function configureMasterSlaveDsn(): void
79
    {
80 3
        $locator = $this->getLocator();
81
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
82 3
    }
83 3
84 3
    private function getLocator(): ConnectionLocator
85 3
    {
86 3
        return ConnectionLocatorFactory::fromInstance(
87 3
            $this->dsn,
88 3
            $this->username,
89 3
            $this->password,
90
            $this->slave,
91 3
            $this->options,
92 3
            $this->queries
93
        );
94
    }
95
}
96