Completed
Push — master ( 470be9...859b72 )
by WEBEWEB
01:46
created

MicrosoftAccessDatabase::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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\Microsoft;
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 NdC/WBW <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Database\Microsoft
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
     * Connect.
49
     *
50
     * @return PDO Returns the connection.
51
     * @throws FileNotFoundException Throws a file not found exception if the filename does not exists.
52
     */
53
    public function connect() {
54
55
        // Check if the filename exists.
56
        if (false === file_exists($this->getDatabase())) {
57
            throw new FileNotFoundException($this->getDatabase());
58
        }
59
60
        // Replace the parameters.
61
        $dsn = StringUtility::replace(self::DEFAULT_DSN, ["%DBQ%", "%UID%", "%PWD%"], [$this->getDatabase(), $this->getUsername(), $this->getPassword()]);
62
63
        // Return the connection.
64
        return new PDO($dsn);
65
    }
66
67
}
68