Database   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 22
dl 0
loc 81
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 5 2
A __construct() 0 3 1
A getInstance() 0 9 2
A setConfig() 0 4 1
A getDriver() 0 19 4
1
<?php
2
3
namespace DrMVC\Database;
4
5
use DrMVC\Config\ConfigInterface;
6
use DrMVC\Database\Interfaces\DatabaseInterface;
7
use DrMVC\Database\Drivers\Interfaces\QueryInterface;
8
use MongoDB\Driver\Manager;
9
use PDO;
10
11
/**
12
 * Class for work with databases
13
 *
14
 * @package Modules\Database\Core
15
 * @since   3.0
16
 */
17
class Database implements DatabaseInterface
18
{
19
    /**
20
     * @var ConfigInterface
21
     */
22
    private $_config;
23
24
    /**
25
     * Database constructor.
26
     *
27
     * @param   ConfigInterface $config
28
     */
29
    public function __construct(ConfigInterface $config)
30
    {
31
        $this->setConfig($config);
32
    }
33
34
    /**
35
     * @param   string $collection
36
     * @return  QueryInterface|PDO|Manager
37
     */
38
    public function getInstance(string $collection = null)
39
    {
40
        $class = $this->getDriver();
41
        $instance = new $class($this->getConfig(), $collection);
42
43
        // If collection name is not provided then need return clean instance for ORM
44
        return (null !== $collection)
45
            ? $instance->connect()
46
            : $instance->connect()->getInstance();
47
    }
48
49
    /**
50
     * Get driver of current database
51
     *
52
     * @return  string
53
     */
54
    public function getDriver(): string
55
    {
56
        // Extract driver name from current config
57
        $driver = $this->getConfig('driver');
58
59
        try {
60
            if (!\is_string($driver)) {
61
                throw new Exception('Wrong type of "driver" item in config, must be a string');
62
            }
63
64
            if (!\in_array($driver, self::ALLOWED_DRIVERS, false)) {
65
                $allowed = implode(',', self::ALLOWED_DRIVERS);
66
                throw new Exception("Driver \"$driver\" is not in allowed list [" . $allowed . ']');
67
            }
68
        } catch (Exception $e) {
69
            // __constructor
70
        }
71
72
        return __NAMESPACE__ . '\\Drivers\\' . ucfirst(strtolower($driver));
73
    }
74
75
    /**
76
     * Set configuration of database
77
     *
78
     * @param   ConfigInterface $config
79
     * @return  DatabaseInterface
80
     */
81
    public function setConfig(ConfigInterface $config): DatabaseInterface
82
    {
83
        $this->_config = $config;
84
        return $this;
85
    }
86
87
    /**
88
     * Get database configuration object or array with single database
89
     *
90
     * @param   string|null $name
91
     * @return  mixed
92
     */
93
    public function getConfig(string $name = null)
94
    {
95
        return (null !== $name)
96
            ? $this->_config->get($name)
97
            : $this->_config;
98
    }
99
100
}
101