Passed
Pull Request — 2.x (#83)
by Maxim
36:42 queued 16:48
created

TcpConnectionConfig   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 59
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourceString() 0 3 1
A __construct() 0 9 1
A getDsn() 0 17 1
A __set_state() 0 3 1
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
     * @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...
20
     * @param non-empty-string $host
21
     * @param positive-int $port
22
     * @param non-empty-string|null $user
23
     * @param non-empty-string|null $password
24
     * @param array $options
25
     */
26 34
    public function __construct(
27
        public string $database,
28
        public string $host = 'localhost',
29
        public int $port = 5432,
30
        ?string $user = null,
31
        ?string $password = null,
32
        array $options = []
33
    ) {
34 34
        parent::__construct($user, $password, $options);
35 34
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 2
    public function getSourceString(): string
41
    {
42 2
        return $this->database;
43
    }
44
45
    /**
46
     * Returns the Postgres-specific PDO DataSourceName, that looks like:
47
     * <code>
48
     *  pgsql:host=localhost;port=5432;dbname=dbname;user=login;password=pass
49
     * </code>
50
     *
51
     * {@inheritDoc}
52
     */
53 40
    public function getDsn(): string
54
    {
55 40
        $config = [
56 40
            'host' => $this->host,
57 40
            'port' => $this->port,
58 40
            'dbname' => $this->database,
59
60
            //
61
            // Username and Password may be is a part of DataSourceName
62
            // However, they can also be passed as separate
63
            // parameters, so we ignore the case with the DataSourceName:
64
            //
65
            // 'user'     => $this->user,
66
            // 'password' => $this->password,
67
        ];
68
69 40
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
70
    }
71
72
    public static function __set_state(array $state): self
73
    {
74
        return new self(...$state);
0 ignored issues
show
Bug introduced by
$state is expanded, but the parameter $database of Cycle\Database\Config\Po...onConfig::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        return new self(/** @scrutinizer ignore-type */ ...$state);
Loading history...
75
    }
76
}
77