TcpConnectionConfig   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 96
ccs 19
cts 22
cp 0.8636
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

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