Passed
Push — master ( 5e9f01...72890a )
by Sébastien
03:41
created

JdbcDsnFactory::createDsnFromParams()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Jasper report integration for PHP
7
 *
8
 * @link      https://github.com/belgattitude/soluble-jasper
9
 * @author    Vanvelthem Sébastien
10
 * @copyright Copyright (c) 2017 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper\DataSource\Util;
15
16
use Soluble\Jasper\Exception\InvalidArgumentException;
17
18
class JdbcDsnFactory
19
{
20
    /**
21
     * Return a JDBC DSN formatted string from options.
22
     *
23
     * @param string   $driver        driver name  (mysql/oracle/postgres...)
24
     * @param string   $db            database name
25
     * @param string   $host          server ip or hostname (i.e localhost)
26
     * @param string   $user          username to connect
27
     * @param string   $password      password to connect
28
     * @param string[] $driverOptions extra options as an associative array (i.e serverTimezone...)
29
     *
30
     * @return string i.e: "jdbc:[driver]://localhost/[database]?user=[user]&password=[password]&serverTimezone=UTC";
31
     */
32 3
    public static function createDsn(
33
                                    string $driver,
34
                                    string $db,
35
                                    string $host,
36
                                    string $user,
37
                                    string $password,
38
                                    array $driverOptions = []
39
    ): string {
40 3
        $extras = '';
41 3
        if (!empty($driverOptions)) {
42 2
            $tmp = [];
43 2
            foreach ($driverOptions as $key => $value) {
44 2
                $tmp[] = urlencode($key) . '=' . urlencode($value);
45
            }
46 2
            $extras = '&' . implode('&', $tmp);
47
        }
48
49 3
        return sprintf(
50
            'jdbc:%s://%s/%s?user=%s&password=%s%s',
51
                $driver,
52
                $host,
53
                $db,
54
                $user,
55
                $password,
56
                $extras
57
            );
58
    }
59 5
60
    /**
61 5
     * Return a JDBC DSN formatted string from options.
62 5
     *
63 1
     * @param array $params associative array with ['driver', 'db', 'host', 'user', 'password'] and optionally ['driverOptions'] as array
64
     *
65
     * @return string i.e: "jdbc:[driver]://localhost/[database]?user=[user]&password=[password]&serverTimezone=UTC";
66 4
     */
67 4
    public static function createDsnFromParams(array $params): string
68 1
    {
69
        $driver = $params['driver'] ?? null;
70
        if ($driver === null) {
71 3
            throw new InvalidArgumentException('Missing required "driver" option.');
72 3
        }
73 1
74
        $db = $params['db'] ?? null;
75
        if ($db === null) {
76
            throw new InvalidArgumentException('Missing required "db" option.');
77 2
        }
78 2
79
        $host = $params['host'] ?? null;
80 2
        if ($host === null) {
81
            throw new InvalidArgumentException('Missing required "host" option.');
82 2
        }
83 1
84
        // use and password can be optional for sqlite
85
        $user     = $params['user'] ?? '';
86 1
        $password = $params['password'] ?? ''; // can be cleartext
87
88
        $driverOptions = $params['driverOptions'] ?? [];
89
90
        if (!is_array($driverOptions)) {
91
            throw new InvalidArgumentException('Invalid type, "driverOptions" must be an array.');
92
        }
93
94
        return self::createDsn($driver, $db, $host, $user, $password, $driverOptions);
95
    }
96
}
97