Completed
Push — master ( 099bee...470be9 )
by WEBEWEB
01:42
created

AccessDatabase::connect()   A

Complexity

Conditions 2
Paths 2

Size

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