Completed
Push — 1.x ( 249f8e...9ce618 )
by Akihito
11s
created

configureSingleDsnWithoutQualifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
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 Override;
9
use Ray\Di\AbstractModule;
10
11
final class NamedPdoEnvModule extends AbstractModule
12
{
13
    public const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
14
15
    /**
16
     * @param string              $qualifer Qualifer for ExtendedPdoInterface
17
     * @param string              $dsn      Data Source Name (DSN)
18
     * @param string              $username User name for the DSN string
19
     * @param string              $password Password for the DSN string
20
     * @param string              $slave    Comma separated slave host list
21
     * @param array<string,mixed> $options  A key=>value array of driver-specific connection options
22
     * @param array<string>       $queries  Queries to execute after the connection.
23
     */
24
    public function __construct(
25
        private readonly string $qualifer,
26
        private readonly string $dsn,
27
        private readonly string $username = '',
28
        private readonly string $password = '',
29
        private readonly string $slave = '',
30
        /** @var array<string, mixed> */
31
        private readonly array $options = [],
32
        /** @var array<string> */
33
        private readonly array $queries = []
34
    ) {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    #[Override]
42
    protected function configure(): void
43
    {
44
        $this->slave ? $this->configureMasterSlaveDsn()
45
            : $this->configureSingleDsn();
46
    }
47
48
    private function configureSingleDsn(): void
49
    {
50
        if ($this->qualifer === '') {
51
            $this->configureSingleDsnWithoutQualifier();
52
        } else {
53
            $this->configureSingleDsnWithQualifier();
54
        }
55
    }
56
57
    private function configureSingleDsnWithoutQualifier(): void
58
    {
59
        $connection = new EnvConnection(
60
            $this->dsn,
61
            null,
62
            $this->username,
63
            $this->password,
64
            $this->options,
65
            $this->queries,
66
        );
67
        $this->bind(EnvConnection::class)->toInstance($connection);
68
        $this->bind(ExtendedPdoInterface::class)->toProvider(
69
            NamedExtendedPdoProvider::class,
70
            '',
71
        );
72
    }
73
74
    private function configureSingleDsnWithQualifier(): void
75
    {
76
        $connection = new EnvConnection(
77
            $this->dsn,
78
            null,
79
            $this->username,
80
            $this->password,
81
            $this->options,
82
            $this->queries,
83
        );
84
        $this->bind(EnvConnection::class)
85
            /** @phpstan-ignore argument.type */
86
            ->annotatedWith($this->qualifer)
87
            ->toInstance($connection);
88
        $this->bind(ExtendedPdoInterface::class)
89
            /** @phpstan-ignore argument.type */
90
            ->annotatedWith($this->qualifer)
91
            ->toProvider(
92
                NamedExtendedPdoProvider::class,
93
                $this->qualifer,
94
            );
95
    }
96
97
    private function configureMasterSlaveDsn(): void
98
    {
99
        $locator = ConnectionLocatorFactory::fromEnv(
100
            $this->dsn,
101
            $this->username,
102
            $this->password,
103
            $this->slave,
104
            $this->options,
105
            $this->queries,
106
        );
107
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
108
    }
109
}
110