Pdo::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace Infuse\Services;
13
14
use Infuse\Application;
15
16
class Pdo
17
{
18
    public function __invoke($app)
19
    {
20
        $config = $app['config'];
21
        $dbSettings = (array) $config->get('database');
22
23
        if (isset($dbSettings['dsn'])) {
24
            $dsn = $dbSettings['dsn'];
25
        } else { // generate the dsn
26
            $dsn = $dbSettings['type'].':host='.$dbSettings['host'].';dbname='.$dbSettings['name'];
27
        }
28
29
        $user = array_value($dbSettings, 'user');
30
        $password = array_value($dbSettings, 'password');
31
32
        $pdo = new \PDO($dsn, $user, $password);
33
34
        if ($app['environment'] === Application::ENV_PRODUCTION) {
35
            $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
36
        } else {
37
            $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
38
        }
39
40
        return $pdo;
41
    }
42
}
43