DsnConnectionConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of Cycle Database package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Config\MySQL;
13
14
use Cycle\Database\Config\ProvidesSourceString;
15
use Cycle\Database\Config\Support\DataSourceName;
16
17
class DsnConnectionConfig extends ConnectionConfig implements ProvidesSourceString
18
{
19
    /**
20
     * @var non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
21
     *
22
     * @psalm-allow-private-mutation
23
     */
24
    public string $dsn;
25
26
    private ?string $database = null;
27
28
    /**
29
     * @param non-empty-string|\Stringable $dsn
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|\Stringable at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|\Stringable.
Loading history...
30
     * @param non-empty-string|null $user
31
     * @param non-empty-string|null $password
32
     */
33
    public function __construct(
34
        string|\Stringable $dsn,
35
        ?string $user = null,
36
        ?string $password = null,
37
        array $options = [],
38
    ) {
39
        parent::__construct($user, $password, $options);
40
41
        /** @psalm-suppress ArgumentTypeCoercion */
42
        $this->dsn = DataSourceName::normalize((string) $dsn, $this->getName());
43
    }
44
45
    public function getSourceString(): string
46
    {
47
        /** @psalm-suppress ArgumentTypeCoercion */
48
        return $this->database ??= DataSourceName::read($this->getDsn(), 'dbname') ?? '*';
49
    }
50
51
    public function getDsn(): string
52
    {
53
        return $this->dsn;
54
    }
55
}
56