Passed
Push — master ( 19f46e...73ce88 )
by Mr
02:06
created

Model::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace DrMVC\Database;
4
5
use DrMVC\Config\ConfigInterface;
6
use DrMVC\Database\Drivers\QueryInterface;
7
8
class Model implements ModelInterface
9
{
10
    /**
11
     * @var QueryInterface
12
     */
13
    private $_instance;
14
15
    /**
16
     * Default connection name
17
     * @var string
18
     */
19
    protected $connection = Database::DEFAULT_CONNECTION;
20
21
    /**
22
     * Name of collection for query
23
     * @var string|null
24
     */
25
    protected $collection;
26
27
    /**
28
     * Model constructor.
29
     * @param   ConfigInterface $config
30
     * @param   string $collection name of active collection
31
     */
32
    public function __construct(ConfigInterface $config, string $collection = null)
33
    {
34
        // Create database object with config from above
35
        $config_db = $this->getConfigDB($config);
36
        $database = new Database($config_db);
37
38
        // If collection name is not provided
39
        if (null === $collection) {
40
            // Get current collection name
41
            $collection = $this->getCollection();
42
        }
43
44
        // Extract instance created by driver and put collection name
45
        $instance = $database->getInstance($collection);
46
47
        // Keep driver's instance as local parameter
48
        $this->setInstance($instance);
49
    }
50
51
    /**
52
     * Extract configuration of current database
53
     *
54
     * @param   ConfigInterface $config
55
     * @return  ConfigInterface
56
     */
57
    private function getConfigDB(ConfigInterface $config): ConfigInterface
58
    {
59
        // get current connection
60
        $connection = $this->getConnection();
61
        // Get config of required connection
62
        $config_db = $config->get($connection);
63
        try {
64
            if (null === $config_db) {
65
                throw new Exception("Connection \"$connection\" is not found in database config");
66
            }
67
        } catch (Exception $e) {
68
            // __constructor
69
        }
70
        return $config_db;
71
    }
72
73
    /**
74
     * Get current database name
75
     *
76
     * @return string
77
     */
78
    public function getConnection(): string
79
    {
80
        return $this->connection;
81
    }
82
83
    /**
84
     * Get current collection
85
     *
86
     * @return  string|null
87
     */
88
    public function getCollection()
89
    {
90
        $collection = $this->collection;
91
        try {
92
            if (null === $collection) {
93
                throw new Exception('Collection is not set');
94
            }
95
        } catch (Exception $e) {
96
            // __constructor
97
        }
98
        return $collection;
99
    }
100
101
    /**
102
     * Set name of collection for queries
103
     *
104
     * @param   null|string $collection
105
     * @return  ModelInterface
106
     */
107
    public function setCollection(string $collection): ModelInterface
108
    {
109
        $this->collection = $collection;
110
        return $this;
111
    }
112
113
    /**
114
     * Set database instance
115
     *
116
     * @param   QueryInterface $instance
117
     */
118
    private function setInstance(QueryInterface $instance)
119
    {
120
        $this->_instance = $instance;
121
    }
122
123
    /**
124
     * Get database instance
125
     *
126
     * @return QueryInterface
127
     */
128
    public function getInstance(): QueryInterface
129
    {
130
        return $this->_instance;
131
    }
132
133
    public function update(array $data, array $where = [])
134
    {
135
        return $this->getInstance()->update($data, $where);
136
    }
137
138
    public function delete(array $where)
139
    {
140
        return $this->getInstance()->delete($where);
141
    }
142
143
    public function exec(string $query)
144
    {
145
        return $this->getInstance()->exec($query);
146
    }
147
148
    public function insert(array $data)
149
    {
150
        return $this->getInstance()->insert($data);
151
    }
152
153
    public function select(string $query, array $data = [])
154
    {
155
        return $this->getInstance()->select($query, $data);
156
    }
157
158
    public function truncate()
159
    {
160
        return $this->getInstance()->truncate();
161
    }
162
}
163