TcpConnectionConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
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 7
dl 0
loc 12
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\MySQL;
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 $host
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 numeric-string|positive-int $port
26
     * @param non-empty-string $database
27 4
     * @param non-empty-string|null $charset
28
     * @param non-empty-string|null $user
29
     * @param non-empty-string|null $password
30
     * @param array<int, non-empty-string|non-empty-string> $options
31
     */
32
    public function __construct(
33
        public string $database,
34
        public string $host = 'localhost',
35
        int|string $port = 3307,
36 4
        public ?string $charset = null,
37 4
        ?string $user = null,
38
        ?string $password = null,
39
        array $options = [],
40
    ) {
41
        $this->port = (int) $port;
42 484
43
        parent::__construct($user, $password, $options);
44 484
    }
45
46
    public function getSourceString(): string
47
    {
48
        return $this->database;
49
    }
50
51
    /**
52
     * Returns the MySQL-specific PDO DataSourceName with connection URI,
53
     * that looks like:
54
     * <code>
55
     *  mysql:host=localhost;port=3307;dbname=dbname
56 8
     * </code>
57
     *
58 8
     * {@inheritDoc}
59 8
     */
60 8
    public function getDsn(): string
61 8
    {
62 8
        $config = [
63
            'host' => $this->host,
64
            'port' => $this->port,
65 8
            'dbname' => $this->database,
66
            'charset' => $this->charset,
67
        ];
68
69
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
70
    }
71
}
72