Issues (41)

src/NamedPdoModule.php (1 issue)

Labels
Severity
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 Override;
11
use Ray\Di\AbstractModule;
12
use SensitiveParameter;
13
14
final class NamedPdoModule extends AbstractModule
15
{
16
    public const string PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 16 at column 24
Loading history...
17
18
    /**
19
     * @param string              $qualifer Qualifer for ExtendedPdoInterface
20
     * @param string              $dsn      Data Source Name (DSN)
21
     * @param string              $username User name for the DSN string
22
     * @param string              $password Password for the DSN string
23
     * @param string              $slave    Comma separated slave host list
24
     * @param array<string,mixed> $options  A key=>value array of driver-specific connection options
25
     * @param array<string>       $queries  Queries to execute after the connection.
26
     */
27
    public function __construct(
28
        private readonly string $qualifer,
29
        private readonly string $dsn,
30
        private readonly string $username = '',
31
        #[SensitiveParameter]
32
        private readonly string $password = '',
33
        private readonly string $slave = '',
34
        /** @var array<string, mixed> */
35
        private readonly array $options = [],
36
        /** @var array<string> */
37
        private readonly array $queries = []
38
    ) {
39
        parent::__construct();
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    #[Override]
46
    protected function configure(): void
47
    {
48
        $this->slave ? $this->configureMasterSlaveDsn()
49
            : $this->configureSingleDsn();
50
    }
51
52
    private function configureSingleDsn(): void
53
    {
54
        $this->bind(ExtendedPdoInterface::class)
55
            ->annotatedWith($this->qualifer)
56
            ->toConstructor(
57
                ExtendedPdo::class,
58
                "dsn={$this->qualifer}_dsn,username={$this->qualifer}_username,password={$this->qualifer}_password",
59
            );
60
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
61
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
62
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
63
    }
64
65
    private function configureMasterSlaveDsn(): void
66
    {
67
        $locator = $this->getLocator();
68
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
69
    }
70
71
    private function getLocator(): ConnectionLocator
72
    {
73
        return ConnectionLocatorFactory::fromInstance(
74
            $this->dsn,
75
            $this->username,
76
            $this->password,
77
            $this->slave,
78
            $this->options,
79
            $this->queries,
80
        );
81
    }
82
}
83