AbstractPDOConnection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 1
cbo 0
dl 0
loc 94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A getConnection() 0 4 1
A buildDSNString() 0 11 4
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 4/5/15
5
 * Time: 12:20 PM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Cache\Adapter\SQL\Connection;
12
13
use InvalidArgumentException;
14
use PDO;
15
use PDOException;
16
17
/**
18
 * Class AbstractPDOConnection
19
 * @package NilPortugues\Cache\Adapter\SQL\Connection
20
 */
21
abstract class AbstractPDOConnection
22
{
23
    const DRIVER         = 'driver';
24
    const DATABASE       = 'database';
25
    const SOCKET         = 'socket';
26
    const USER           = 'user';
27
    const PASSWORD       = 'password';
28
    const DRIVER_OPTIONS = 'options';
29
30
    /**
31
     * For MySql
32
     */
33
    const HOST        = 'host';
34
    const PORT        = 'port';
35
    const DB_NAME     = 'dbname';
36
    const UNIX_SOCKET = 'unix_socket';
37
    const CHARSET     = 'charset';
38
39
    /**
40
     * For Sqlite
41
     */
42
    const PATH   = 'path';
43
    const MEMORY = 'memory';
44
45
    /**
46
     * @var PDO
47
     */
48
    protected $connection;
49
50
    /**
51
     * @var array
52
     */
53
    protected $keys = [];
54
55
    /**
56
     * @var string
57
     */
58
    protected $dsn = '';
59
60
    /**
61
     * Attempts to create a connection with the database.
62
     *
63
     * @param array       $parameters    All connection parameters passed by the user.
64
     * @param string|null $username      The username to use when connecting.
65
     * @param string|null $password      The password to use when connecting.
66
     * @param array       $driverOptions The driver options to use when connecting.
67
     *
68
     * @throws InvalidArgumentException
69
     */
70
    public function __construct(array $parameters, $username = null, $password = null, array $driverOptions = [])
71
    {
72
        if (false === \array_key_exists(self::DATABASE, $parameters)) {
73
            throw new InvalidArgumentException(
74
                \sprintf("Parameter array requires of '%s' data to be set.", self::DATABASE)
75
            );
76
        }
77
78
        try {
79
            $dsn = $this->buildDSNString($parameters[self::DATABASE]);
80
            $this->connection = new PDO($this->dsn.$dsn, $username, $password, $driverOptions);
81
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
82
        } catch (PDOException $e) {
83
            throw new InvalidArgumentException(
84
                \sprintf("An exception occurred in %s: %s", \get_class($this), $e->getMessage())
85
            );
86
        }
87
    }
88
89
    /**
90
     * @return PDO
91
     * @codeCoverageIgnore
92
     */
93
    public function getConnection()
94
    {
95
        return $this->connection;
96
    }
97
98
    /**
99
     * @param array $parameters
100
     *
101
     * @return string
102
     */
103
    protected function buildDSNString(array &$parameters)
104
    {
105
        $dsn = [];
106
        foreach ($this->keys as $keyName) {
107
            if (\array_key_exists($keyName, $parameters) && \strlen($parameters[$keyName]) > 0) {
108
                $dsn[] = "{$keyName}={$parameters[$keyName]}";
109
            }
110
        }
111
112
        return \implode(";", $dsn);
113
    }
114
}
115