TcpConnectionConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 11
ccs 3
cts 3
cp 1
crap 1
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\Postgres;
13
14
use Cycle\Database\Config\ProvidesSourceString;
15
16
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString
17
{
18
    /**
19
     * @var positive-int
0 ignored issues
show
Documentation Bug introduced by
The doc comment positive-int at position 0 could not be parsed: Unknown type name 'positive-int' at position 0 in positive-int.
Loading history...
20
     */
21
    public int $port;
22
23
    /**
24
     * @param non-empty-string $database
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...
25
     * @param non-empty-string $host
26 34
     * @param numeric-string|positive-int $port
27
     * @param non-empty-string|null $user
28
     * @param non-empty-string|null $password
29
     */
30
    public function __construct(
31
        public string $database,
32
        public string $host = 'localhost',
33
        int|string $port = 5432,
34 34
        ?string $user = null,
35 34
        ?string $password = null,
36
        array $options = [],
37
    ) {
38
        $this->port = (int) $port;
39
40 2
        parent::__construct($user, $password, $options);
41
    }
42 2
43
    public function getSourceString(): string
44
    {
45
        return $this->database;
46
    }
47
48
    /**
49
     * Returns the Postgres-specific PDO DataSourceName, that looks like:
50
     * <code>
51
     *  pgsql:host=localhost;port=5432;dbname=dbname;user=login;password=pass
52
     * </code>
53 40
     *
54
     * {@inheritDoc}
55 40
     */
56 40
    public function getDsn(): string
57 40
    {
58 40
        $config = [
59
            'host' => $this->host,
60
            'port' => $this->port,
61
            'dbname' => $this->database,
62
63
            //
64
            // Username and Password may be is a part of DataSourceName
65
            // However, they can also be passed as separate
66
            // parameters, so we ignore the case with the DataSourceName:
67
            //
68
            // 'user'     => $this->user,
69 40
            // 'password' => $this->password,
70
        ];
71
72
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
73
    }
74
}
75