Database::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 +-----------------------------------------------------------------------------+
6
 | PHPPackage - PDO Wrapper
7
 +-----------------------------------------------------------------------------+
8
 | Copyright (c)2018 (http://github.com/phppackage/pdo-wrapper)
9
 +-----------------------------------------------------------------------------+
10
 | This source file is subject to MIT License
11
 | that is bundled with this package in the file LICENSE.
12
 |
13
 | If you did not receive a copy of the license and are unable to
14
 | obtain it through the world-wide-web, please send an email
15
 | to [email protected] so we can send you a copy immediately.
16
 +-----------------------------------------------------------------------------+
17
 | Authors:
18
 |   Lawrence Cherone <[email protected]>
19
 +-----------------------------------------------------------------------------+
20
 */
21
22
namespace PHPPackage\PDOWrapper;
23
24
class Database
25
{
26
    /**
27
     * @var \PHPPackage\PDOWrapper\PDO
28
     */
29
    private $pdo;
30
    
31
    /**
32
     * @var array PDO attribute keys.
33
     */
34
    private $attributes = array(
35
        'AUTOCOMMIT', 'ERRMODE', 'CASE', 'CLIENT_VERSION', 'CONNECTION_STATUS',
36
        'ORACLE_NULLS', 'PERSISTENT', 'PREFETCH', 'SERVER_INFO', 'SERVER_VERSION',
37
        'TIMEOUT', 'DRIVER_NAME'
38
    );
39
40
    /**
41
     * @param \PHPPackage\PDOWrapper\PDO $pdo
42
     */
43 11
    public function __construct(PDO $pdo)
44
    {
45 11
        $this->pdo = $pdo;
46 11
    }
47
    
48
    /**
49
     * Enumarate PDO attributes
50
     *
51
     * @param string $key Pick out a attribute by key
52
     * @return mixed
53
     */
54 6
    public function attribute(string $key = null) 
55
    {
56 6
        $key = 'PDO::ATTR_'.strtoupper($key);
57
58
        try {
59 6
            return $this->pdo->getAttribute(constant($key));
60 1
        } catch (\PDOException $e) {
61 1
            return;
62
        }
63
    }
64
    
65
    /**
66
     * Enumarate PDO attributes
67
     *
68
     * @param string $key Pick out a attribute by key
69
     * @return mixed
70
     */
71 6
    public function info(string $key = null)
72
    {
73 6
        if (!is_null($key)) {
74 6
            return $this->attribute($key);
75
        }
76
        
77 1
        $return = [];
78 1
        foreach ($this->attributes as $value) {
79 1
            $return['PDO::ATTR_'.$value] = $this->attribute($value);
80
        }
81
        
82 1
        return $return;
83
    }
84
85
    /**
86
     * Get database name from dsn
87
     *
88
     * @throws RuntimeException
89
     * @return string
90
     */
91 4
    public function name(string $dsn): string
92
    {
93
        // match database from dsn & set working vars
94 4
        if (!preg_match('/dbname=(\w+);/', $dsn, $results)) {
95 1
            throw new \RuntimeException('Could not match database name from dsn');
96
        }
97
98 3
        return $results[1];
99
    }
100
101
    /**
102
     * Create database
103
     *
104
     * @return bool
105
     */
106 1
    public function create(string $name, $username, $password): bool
107
    {
108 1
        if (!in_array($name, $this->databases())) {
109 1
            return (bool) $this->pdo->exec("
110 1
                CREATE DATABASE `$name`;
111 1
                CREATE USER '{$username}'@'%' IDENTIFIED BY '{$password}';
112 1
                GRANT ALL ON `$name`.* TO '{$username}'@'%';
113
                FLUSH PRIVILEGES;
114
            ");
115
        }
116 1
        return false;
117
    }
118
119
    /**
120
     * Returns an array of databases
121
     * @return mixed
122
     */
123 2
    public function databases(): array
124
    {
125 2
        $stmt = $this->pdo->query("SHOW DATABASES");
126
127 1
        $result = [];
128 1
        while ($row = $stmt->fetchColumn(0)) {
129 1
            $result[] = $row;
130
        }
131
132 1
        return $result;
133
    }
134
135
    /**
136
     * Returns an array of tables
137
     * @return array
138
     */
139 1
    public function tables(): array
140
    {
141 1
        $stmt = $this->pdo->query("SHOW TABLES");
142
143 1
        $result = [];
144 1
        while ($row = $stmt->fetchColumn(0)) {
145 1
            $result[] = $row;
146
        }
147
148 1
        return $result;
149
    }
150
}
151