createDatabaseSourceString()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 6
eloc 17
nc 10
nop 1
1
<?php
2
3
namespace Pyjac\ORM;
4
5
use Pyjac\ORM\Exception\DatabaseDriverNotSupportedException;
6
7
class DatabaseConnectionStringFactory implements DatabaseConnectionStringFactoryInterface
8
{
9
    /**
10
     * Create a connection string.
11
     *
12
     * @param array $config
13
     *
14
     * @throws Pyjac\PotatoORM\Exception\DatabaseDriverNotSupportedException
15
     *
16
     * @return string
17
     */
18
    public function createDatabaseSourceString($config)
19
    {
20
        $driver = $config['DRIVER'];
21
22
        switch ($driver) {
23
            case 'sqlite':
24
                $dsn = $driver.'::memory:';
25
                break;
26
            case 'mysql':
27
            case 'postgres':
28
                if (strcasecmp($driver, 'postgres') == 0) {
29
                    $driver = 'pgsql';
30
                }
31
                $dsn = $driver.':host='.$config['HOSTNAME'].';dbname='.$config['DBNAME'];
32
                if (isset($config['PORT'])) {
33
                    $dsn .= ';port='.$config['PORT'];
34
                }
35
                break;
36
            default:
37
                throw new DatabaseDriverNotSupportedException();
38
        }
39
40
        return $dsn;
41
    }
42
}
43