Passed
Push — support-php-8.2-8.3 ( 931a27...2d6bad )
by Akihito
01:18
created

NamedPdoModule::configure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 5
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 InvalidArgumentException;
11
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Ray\Di\AbstractModule;
13
use SensitiveParameter;
0 ignored issues
show
Bug introduced by
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
final class NamedPdoModule extends AbstractModule
16
{
17
    public const PARSE_PDO_DSN_REGEX = '/(.*?)\:(host|server)=.*?;(.*)/i';
18
19
    /**
20
     * @param string              $qualifer Qualifer for ExtendedPdoInterface
21
     * @param string              $dsn      Data Source Name (DSN)
22
     * @param string              $username User name for the DSN string
23
     * @param string              $password Password for the DSN string
24
     * @param string              $slave    Comma separated slave host list
25
     * @param array<string,mixed> $options  A key=>value array of driver-specific connection options
26
     * @param array<string>       $queries  Queries to execute after the connection.
27
     */
28
    public function __construct(
29
        private readonly string $qualifer,
30
        private readonly string $dsn,
31
        private readonly string $username = '',
32
        #[SensitiveParameter]
33
        private readonly string $password = '',
34
        private readonly string $slave = '',
35
        /** @var array<string, mixed> */
36
        private readonly array $options = [],
37
        /** @var array<string> */
38
        private readonly array $queries = []
39
    ) {
40
        if ($this->qualifer === '') {
41
            throw new InvalidArgumentException(
42
                'NamedPdoModule requires a non-empty qualifier. ' .
43
                'Use AuraSqlModule instead for unqualified bindings.',
44
            );
45
        }
46
47
        parent::__construct();
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    #[Override]
54
    protected function configure(): void
55
    {
56
        $this->slave ? $this->configureMasterSlaveDsn()
57
            : $this->configureSingleDsn();
58
    }
59
60
    private function configureSingleDsn(): void
61
    {
62
        $this->bind(ExtendedPdoInterface::class)
63
            ->annotatedWith($this->qualifer)
64
            ->toConstructor(
65
                ExtendedPdo::class,
66
                [
67
                    'dsn' => "{$this->qualifer}_dsn",
68
                    'username' => "{$this->qualifer}_username",
69
                    'password' => "{$this->qualifer}_password",
70
                    'driver_options' => "{$this->qualifer}_options",
71
                    'after_connect' => "{$this->qualifer}_queries",
72
                ],
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
        $this->bind()->annotatedWith("{$this->qualifer}_options")->toInstance($this->options);
78
        $this->bind()->annotatedWith("{$this->qualifer}_queries")->toInstance($this->queries);
79
    }
80
81
    private function configureMasterSlaveDsn(): void
82
    {
83
        $locator = $this->getLocator();
84
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
85
    }
86
87
    private function getLocator(): ConnectionLocator
88
    {
89
        return ConnectionLocatorFactory::fromInstance(
90
            $this->dsn,
91
            $this->username,
92
            $this->password,
93
            $this->slave,
94
            $this->options,
95
            $this->queries,
96
        );
97
    }
98
}
99