Completed
Push — master ( 1c956a...362a13 )
by WEBEWEB
01:17
created

MicrosoftSQLServerDatabase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A connect() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Database\Client;
13
14
use PDO;
15
use WBW\Library\Core\Security\Authenticator;
16
17
/**
18
 * Microsoft SQL Server database.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Database\Client
22
 */
23
class MicrosoftSQLServerDatabase extends AbstractDatabase {
24
25
    /**
26
     * Microsoft SQL Server DSN.
27
     *
28
     * @var string
29
     */
30
    const DEFAULT_DSN = "sqlsrv:server=%HOST%,%PORT%;database=%DATABASE%;";
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param Authenticator $authenticator The authenticator.
36
     * @param string $database The database.
37
     */
38
    public function __construct(Authenticator $authenticator, $database) {
39
        parent::__construct($authenticator);
40
        $this->setDatabase($database);
41
        if (null === $this->getAuthenticator()->getPort()) {
42
            $this->getAuthenticator()->setPort(1433);
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function connect() {
50
51
        $searches = ["%HOST%", "%PORT%", "%DATABASE%"];
52
        $replaces = [$this->getAuthenticator()->getHostname(), $this->getAuthenticator()->getPort(), $this->getDatabase()];
53
54
        $dsn = str_replace($searches, $replaces, self::DEFAULT_DSN);
55
56
        return new PDO($dsn, $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword());
57
    }
58
}
59