Completed
Push — master ( c6707f...7942ad )
by WEBEWEB
03:28
created

MicrosoftAccessDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 NdC/WBW
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\Database\AbstractDatabase;
16
use WBW\Library\Core\Exception\File\FileNotFoundException;
17
use WBW\Library\Core\Utility\StringUtility;
18
19
/**
20
 * Microsoft Access database.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Database
24
 * @final
25
 */
26
final class MicrosoftAccessDatabase extends AbstractDatabase {
27
28
    /**
29
     * Microsoft Access DSN.
30
     */
31
    const DEFAULT_DSN = "odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=%DBQ%; UID=%UID%; PWD=%PWD%";
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param string $filename The filename.
37
     * @param string $username The username.
38
     * @param string $password The password.
39
     */
40
    public function __construct($filename = null, $username = "", $password = "") {
41
        parent::__construct();
42
        $this->setDatabase($filename);
43
        $this->setUsername($username);
44
        $this->setPassword($password);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function connect() {
51
52
        // Check if the filename exists.
53
        if (false === file_exists($this->getDatabase())) {
54
            throw new FileNotFoundException($this->getDatabase());
55
        }
56
57
        // Replace the parameters.
58
        $dsn = StringUtility::replace(self::DEFAULT_DSN, ["%DBQ%", "%UID%", "%PWD%"], [$this->getDatabase(), $this->getUsername(), $this->getPassword()]);
59
60
        // Return the connection.
61
        return new PDO($dsn);
62
    }
63
64
}
65