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

NamedPdoModule::configureMasterSlaveDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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;
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 6
                'Use AuraSqlModule instead for unqualified bindings.',
44
            );
45
        }
46
47
        parent::__construct();
48
    }
49
50 6
    /**
51 6
     * {@inheritDoc}
52 6
     */
53 6
    #[Override]
54 6
    protected function configure(): void
55 6
    {
56 6
        $this->slave ? $this->configureMasterSlaveDsn()
57
            : $this->configureSingleDsn();
58
    }
59
60
    private function configureSingleDsn(): void
61 6
    {
62
        $this->bind(ExtendedPdoInterface::class)
63 6
            ->annotatedWith($this->qualifer)
64 3
            ->toConstructor(
65 6
                ExtendedPdo::class,
66
                [
67 3
                    'dsn' => "{$this->qualifer}_dsn",
68
                    'username' => "{$this->qualifer}_username",
69 3
                    'password' => "{$this->qualifer}_password",
70 3
                    'driver_options' => "{$this->qualifer}_options",
71 3
                    'after_connect' => "{$this->qualifer}_queries",
72 3
                ],
73 3
            );
74
        $this->bind()->annotatedWith("{$this->qualifer}_dsn")->toInstance($this->dsn);
75 3
        $this->bind()->annotatedWith("{$this->qualifer}_username")->toInstance($this->username);
76 3
        $this->bind()->annotatedWith("{$this->qualifer}_password")->toInstance($this->password);
77 3
        $this->bind()->annotatedWith("{$this->qualifer}_options")->toInstance($this->options);
78 3
        $this->bind()->annotatedWith("{$this->qualifer}_queries")->toInstance($this->queries);
79
    }
80 3
81
    private function configureMasterSlaveDsn(): void
82 3
    {
83 3
        $locator = $this->getLocator();
84 3
        $this->install(new AuraSqlReplicationModule($locator, $this->qualifer));
85 3
    }
86 3
87 3
    private function getLocator(): ConnectionLocator
88 3
    {
89 3
        return ConnectionLocatorFactory::fromInstance(
90
            $this->dsn,
91 3
            $this->username,
92 3
            $this->password,
93
            $this->slave,
94
            $this->options,
95
            $this->queries,
96
        );
97
    }
98
}
99