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

AuraSqlModule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 1
Metric Value
eloc 28
c 8
b 1
f 1
dl 0
loc 67
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 2
A configureSingleDsn() 0 18 1
A __construct() 0 12 1
A configureMasterSlaveDsn() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Aura\Sql\ExtendedPdo;
8
use Aura\Sql\ExtendedPdoInterface;
9
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...
10
use Ray\Di\AbstractModule;
11
use Ray\Di\Scope;
12
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...
13
14
final class AuraSqlModule extends AbstractModule
15
{
16
    public const PARSE_PDO_DSN_REGEX = '/(.*?):(?:(host|server)=.*?;)?(.*)/i';
17
18
    /**
19
     * @param string              $dsn      Data Source Name (DSN)
20
     * @param string              $user     User name for the DSN string
21
     * @param string              $password Password for the DSN string
22
     * @param string              $slave    Comma separated slave host list
23
     * @param array<string,mixed> $options  A key=>value array of driver-specific connection options
24
     * @param array<string>       $queries
25
     */
26
    public function __construct(
27
        private readonly string $dsn,
28
        private readonly string $user = '',
29
        #[SensitiveParameter]
30
        private readonly string $password = '',
31
        private readonly string $slave = '',
32
        /** @var array<string, mixed> */
33
        private readonly array $options = [],
34
        /** @var array<string> */
35
        private readonly array $queries = []
36
    ) {
37
        parent::__construct();
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    #[Override]
44
    protected function configure(): void
45
    {
46
        $this->slave ? $this->configureMasterSlaveDsn() : $this->configureSingleDsn();
47
        $this->install(new AuraSqlBaseModule($this->dsn));
48
    }
49
50
    private function configureSingleDsn(): void
51
    {
52
        $this->bind()->annotatedWith('pdo_dsn')->toInstance($this->dsn);
53
        $this->bind()->annotatedWith('pdo_user')->toInstance($this->user);
54
        $this->bind()->annotatedWith('pdo_pass')->toInstance($this->password);
55
        $this->bind()->annotatedWith('pdo_slave')->toInstance($this->slave);
56
        $this->bind()->annotatedWith('pdo_options')->toInstance($this->options);
57
        $this->bind()->annotatedWith('pdo_queries')->toInstance($this->queries);
58
        $this->bind(ExtendedPdoInterface::class)->toConstructor(
59
            ExtendedPdo::class,
60
            [
61
                'dsn' => 'pdo_dsn',
62
                'username' => 'pdo_user',
63
                'password' => 'pdo_pass',
64
                'options' => 'pdo_options',
65
                'queries' => 'pdo_queries',
66
            ],
67
        )->in(Scope::SINGLETON);
68
    }
69
70
    private function configureMasterSlaveDsn(): void
71
    {
72
        $locator = ConnectionLocatorFactory::fromInstance(
73
            $this->dsn,
74
            $this->user,
75
            $this->password,
76
            $this->slave,
77
            $this->options,
78
            $this->queries,
79
        );
80
        $this->install(new AuraSqlReplicationModule($locator));
81
    }
82
}
83