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

TcpConnectionConfig::getDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 20
ccs 17
cts 17
cp 1
crap 1
rs 9.7333
c 1
b 0
f 0
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\SQLServer;
13
14
use Cycle\Database\Config\ProvidesSourceString;
15
use Cycle\Database\Config\PDOConnectionConfig;
16
17
/**
18
 * @psalm-type IsolationLevelType = \PDO::SQLSRV_TXN_*
19
 * @psalm-import-type PDOFlag from PDOConnectionConfig
20
 */
21
class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString
22
{
23
    /**
24
     * @param non-empty-string $database The name of the 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 Database connection host.
26
     * @param positive-int|null $port Database connection port.
27
     * @param non-empty-string|null $app The application name used in tracing.
28
     * @param bool|null $pooling Specifies whether the connection is assigned from a
29
     *        connection pool ({@see true}) or not ({@see false}).
30
     * @param bool|null $encrypt Specifies whether the communication with SQL Server
31
     *        is encrypted ({@see true}) or unencrypted ({@see false}).
32
     * @param non-empty-string|null $failover Specifies the server and instance of the database's
33
     *        mirror (if enabled and configured) to use when the primary server is unavailable.
34
     * @param int|null $timeout Specifies the number of seconds to wait before failing the connection attempt.
35
     * @param bool|null $mars Disables or explicitly enables support for Multiple Active Result Sets (MARS).
36
     * @param bool|null $quoted Specifies whether to use SQL-92 rules for quoted
37
     *        identifiers ({@see true}) or to use legacy Transact-SQL rules ({@see false}).
38
     * @param non-empty-string|null $traceFile Specifies the path for the file used for trace data.
39
     * @param bool|null $trace Specifies whether ODBC tracing is enabled ({@see true}) or
40
     *        disabled ({@see false}) for the connection being established.
41
     * @param IsolationLevelType|null $isolation Specifies the transaction isolation level.
42
     *        The accepted values for this option are:
43
     *          - {@see \PDO::SQLSRV_TXN_READ_UNCOMMITTED}
44
     *          - {@see \PDO::SQLSRV_TXN_READ_COMMITTED}
45
     *          - {@see \PDO::SQLSRV_TXN_REPEATABLE_READ}
46
     *          - {@see \PDO::SQLSRV_TXN_SNAPSHOT}
47
     *          - {@see \PDO::SQLSRV_TXN_SERIALIZABLE}
48
     * @param bool|null $trustServerCertificate Specifies whether the client should
49
     *        trust ({@see true}) or reject ({@see false}) a self-signed server certificate.
50
     * @param non-empty-string|null $wsid Specifies the name of the computer for tracing.
51
     * @param non-empty-string|null $user
52
     * @param non-empty-string|null $password
53
     * @param array $options
54
     */
55
    public function __construct(
56
        public string $database,
57
        public string $host = 'localhost',
58
        public ?int $port = null,
59
        public ?string $app = null,
60
        public ?bool $pooling = null,
61
        public ?bool $encrypt = null,
62
        public ?string $failover = null,
63
        public ?int $timeout = null,
64
        public ?bool $mars = null,
65
        public ?bool $quoted = null,
66
        public ?string $traceFile = null,
67
        public ?bool $trace = null,
68
        public ?int $isolation = null,
69
        public ?bool $trustServerCertificate = null,
70
        public ?string $wsid = null,
71
        ?string $user = null,
72
        ?string $password = null,
73
        array $options = []
74
    ) {
75
        parent::__construct($user, $password, $options);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 2
    public function getSourceString(): string
82
    {
83 2
        return $this->database;
84
    }
85
86
    /**
87
     * Returns the SQL Server specific PDO DataSourceName, that looks like:
88
     * <code>
89
     *  sqlsrv:Server=localhost,1521;Database=dbname
90
     * </code>
91
     *
92
     * {@inheritDoc}
93
     */
94 6
    public function getDsn(): string
95
    {
96 6
        $config = [
97 6
            'APP' => $this->app,
98 6
            'ConnectionPooling' => $this->pooling,
99 6
            'Database' => $this->database,
100 6
            'Encrypt' => $this->encrypt,
101 6
            'Failover_Partner' => $this->failover,
102 6
            'LoginTimeout' => $this->timeout,
103 6
            'MultipleActiveResultSets' => $this->mars,
104 6
            'QuotedId' => $this->quoted,
105 6
            'Server' => \implode(',', [$this->host, $this->port]),
106 6
            'TraceFile' => $this->traceFile,
107 6
            'TraceOn' => $this->trace,
108 6
            'TransactionIsolation' => $this->isolation,
109 6
            'TrustServerCertificate' => $this->trustServerCertificate,
110 6
            'WSID' => $this->wsid,
111
        ];
112
113 6
        return \sprintf('%s:%s', $this->getName(), $this->dsn($config));
114
    }
115
116
    public static function __set_state(array $state): self
117
    {
118
        return new self(...$state);
0 ignored issues
show
Bug introduced by
$state is expanded, but the parameter $database of Cycle\Database\Config\SQ...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

118
        return new self(/** @scrutinizer ignore-type */ ...$state);
Loading history...
119
    }
120
}
121