NamedPdoModule   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 36
c 3
b 0
f 2
dl 0
loc 82
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configureMasterSlaveDsn() 0 4 1
A __construct() 0 18 1
A configureSingleDsn() 0 11 1
A getLocator() 0 9 1
A configure() 0 4 2
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
54
        parent::__construct();
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    protected function configure(): void
61
    {
62
        $this->slave ? $this->configureMasterSlaveDsn()
63
            : $this->configureSingleDsn();
64
    }
65
66
    private function configureSingleDsn(): void
67
    {
68
        $this->bind(ExtendedPdoInterface::class)
69
            ->annotatedWith($this->qualifer)
70
            ->toConstructor(
71
                ExtendedPdo::class,
72
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password",
73
            );
74
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
75
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
76
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
77
    }
78
79
    private function configureMasterSlaveDsn(): void
80
    {
81
        $locator = $this->getLocator();
82
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
83
    }
84
85
    private function getLocator(): ConnectionLocator
86
    {
87
        return ConnectionLocatorFactory::fromInstance(
88
            $this->dsn,
89
            $this->username,
90
            $this->password,
91
            $this->slave,
92
            $this->options,
93
            $this->queries,
94
        );
95
    }
96
}
97