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

MicrosoftAccessDatabaseConnector   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 5 1
A connect() 0 13 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\Connector;
13
14
use InvalidArgumentException;
15
use PDO;
16
use WBW\Library\Core\Security\Authenticator;
17
18
/**
19
 * Microsoft Access database connector.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Database\Connector
23
 */
24
class MicrosoftAccessDatabaseConnector extends AbstractDatabaseConnector {
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
42
        $this->setDatabase($database);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function connect() {
49
50
        if (false === file_exists($this->getDatabase())) {
51
            throw new InvalidArgumentException(sprintf("The database \"%s\" was not found", $this->getDatabase()));
52
        }
53
54
        $searches = ["%DBQ%", "%UID%", "%PWD%"];
55
        $replaces = [$this->getDatabase(), $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword()];
56
57
        $dsn = str_replace($searches, $replaces, self::DEFAULT_DSN);
58
59
        return new PDO($dsn);
60
    }
61
}
62