Database   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A connection() 0 8 2
A loadConfig() 0 11 3
1
<?php
2
3
namespace Vertex\Core;
4
5
use Vertex\Core\Traits\SingletonTrait;
6
use Exception;
7
use PDO;
8
9
class Database
10
{
11
    use SingletonTrait;
12
13
    /**
14
     * The default PDO connection options.
15
     * 
16
     * @var array
17
     */
18
    protected $options = [
19
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
20
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
21
    ];
22
23
    /**
24
     * Connect to the appropriate database
25
     * based on the details specified in
26
     * the configuration file.
27
     * 
28
     * @param  array  $config 
29
     * @return PDO|bool       
30
     */
31
    public function connection(array $config)
32
    {
33
        if ($config['enabled'] == 'true') {
34
            $instance = new PDO($this->loadConfig($config), $config['mysql']['username'], $config['mysql']['password'], $this->options);
35
            return $instance;
36
        }
37
        return false;
38
    }
39
40
    /**
41
     * Load the configuration.
42
     * 
43
     * @param  array  $config 
44
     * @return string
45
     */
46
    public function loadConfig(array $config)
47
    {
48
        switch ($config['connection']) {
49
            case 'mysql':
50
                return 'mysql:host=' . $config['mysql']['host'] . ';dbname=' . $config['mysql']['database'];
51
            case 'sqlite':
52
                return 'sqlite:' . __DIR__ . '/database/' . $config['sqlite']['database'] . '.sqlite';
53
            default:
54
                throw new Exception('Connection type not supported');
55
        }
56
    }
57
}
58