PDO::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Connection\PDO;
13
14
use Rocket\ORM\Connection\ConnectionFactoryInterface;
15
use Rocket\ORM\Connection\ConnectionInterface;
16
17
/**
18
 * @author Sylvain Lorinet <[email protected]>
19
 */
20
class PDO extends \PDO implements ConnectionFactoryInterface, ConnectionInterface
21
{
22
    /**
23
     * @param array $config
24
     *
25
     * @return static
26
     */
27 1
    public static function create(array $config)
28
    {
29
        $options = [
30 1
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
31 1
        ];
32
33 1
        if (isset($config['options'])) {
34
            // array_merge() cannot work here because the array keys are integer
35 1
            $options = $config['options'] + $options;
36 1
        }
37
38
39
        // No database in the dns configuration, it is handled by the schema itself
40 1
        $config['dsn'] = preg_replace('/dbname=[a-zA-Z0-9-_]+;?/', '', $config['dsn']);
41
42 1
        return new static($config['dsn'], $config['username'], $config['password'], $options);
43
    }
44
45
    /**
46
     * // TODO rename "isDatabaseExists"
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
47
     *
48
     * @param string $databaseName
49
     *
50
     * @return bool
51
     */
52 1
    public function isDatabaseCreated($databaseName)
53
    {
54 1
        $stmt = $this->query("SHOW DATABASES LIKE '" . $databaseName . "'");
55 1
        $stmt->execute();
56
57 1
        return 0 < $stmt->rowCount();
58
    }
59
60
    /**
61
     * @param string $databaseName
62
     *
63
     * @return bool
64
     */
65 1
    public function createDatabase($databaseName)
66
    {
67 1
        $stmt = $this->prepare('CREATE DATABASE IF NOT EXISTS ' . $databaseName);
68
69 1
        return $stmt->execute();
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public static function getDriver()
76
    {
77 1
        return 'mysql';
78
    }
79
}
80