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

MicrosoftSQLServerDatabaseConnector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 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\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