Completed
Push — master ( 956484...f9dbde )
by WEBEWEB
01:39
created

MicrosoftSQLServerDatabase::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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;
13
14
use PDO;
15
use WBW\Library\Core\Security\Authenticator;
16
use WBW\Library\Core\Utility\Argument\StringUtility;
17
18
/**
19
 * Microsoft SQL Server database.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Database
23
 */
24
class MicrosoftSQLServerDatabase extends AbstractDatabase {
25
26
    /**
27
     * Microsoft SQL Server DSN.
28
     *
29
     * @var string
30
     */
31
    const DEFAULT_DSN = "sqlsrv:server=%HOST%,%PORT%;database=%DATABASE%;";
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Authenticator $authenticator The authenticator.
37
     * @param string $database The database.
38
     */
39
    public function __construct(Authenticator $authenticator, $database) {
40
        parent::__construct($authenticator);
41
        $this->setDatabase($database);
42
        if (null === $this->getAuthenticator()->getPort()) {
43
            $this->getAuthenticator()->setPort(1433);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function connect() {
51
52
        // Prepare the parameters.
53
        $searches = ["%HOST%", "%PORT%", "%DATABASE%"];
54
        $replaces = [$this->getAuthenticator()->getHost(), $this->getAuthenticator()->getPort(), $this->getDatabase()];
55
56
        // Replace the parameters.
57
        $dsn = StringUtility::replace(self::DEFAULT_DSN, $searches, $replaces);
58
59
        // Return the connection.
60
        return new PDO($dsn, $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword());
61
    }
62
63
}
64