Completed
Push — master ( 1c956a...362a13 )
by WEBEWEB
01:17
created

MicrosoftAccessDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Client;
13
14
use InvalidArgumentException;
15
use PDO;
16
use WBW\Library\Core\Security\Authenticator;
17
18
/**
19
 * Microsoft Access database.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Database\Client
23
 */
24
class MicrosoftAccessDatabase extends AbstractDatabase {
25
26
    /**
27
     * Microsoft Access DSN.
28
     *
29
     * @var string
30
     */
31
    const DEFAULT_DSN = "odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=%DBQ%; UID=%UID%; PWD=%PWD%";
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Authenticator $authenticator The authenticator.
37
     * @param string $database The database.
38
     */
39
    public function __construct(Authenticator $authenticator, $database) {
40
        parent::__construct($authenticator);
41
        $this->setDatabase($database);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function connect() {
48
49
        if (false === file_exists($this->getDatabase())) {
50
            throw new InvalidArgumentException(sprintf("The database \"%s\" was not found", $this->getDatabase()));
51
        }
52
53
        $searches = ["%DBQ%", "%UID%", "%PWD%"];
54
        $replaces = [$this->getDatabase(), $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword()];
55
56
        $dsn = str_replace($searches, $replaces, self::DEFAULT_DSN);
57
58
        return new PDO($dsn);
59
    }
60
}
61