Completed
Push — master ( d0e678...183caa )
by WEBEWEB
01:15
created

MicrosoftSQLServerDatabaseConnector::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Connector;
13
14
use PDO;
15
use WBW\Library\Core\Security\Authenticator;
16
17
/**
18
 * Microsoft SQL Server database connector.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Database\Connector
22
 */
23
class MicrosoftSQLServerDatabaseConnector extends AbstractDatabaseConnector {
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
41
        $this->setDatabase($database);
42
43
        if (null === $this->getAuthenticator()->getPort()) {
44
            $this->getAuthenticator()->setPort(1433);
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function connect() {
52
53
        $searches = ["%HOST%", "%PORT%", "%DATABASE%"];
54
        $replaces = [$this->getAuthenticator()->getHostname(), $this->getAuthenticator()->getPort(), $this->getDatabase()];
55
56
        $dsn = str_replace($searches, $replaces, self::DEFAULT_DSN);
57
58
        return new PDO($dsn, $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword());
59
    }
60
}
61