Completed
Push — master ( a0a464...9c0da3 )
by Mr
02:05
created

Model::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace DrMVC\Database;
4
5
use DrMVC\Config\ConfigInterface;
6
use DrMVC\Database\Drivers\QueryInterface;
7
use DrMVC\Database\Drivers\SQLInterface;
8
use DrMVC\Database\Drivers\NoSQLInterface;
9
use Stringy\Stringy;
10
11
/**
12
 * Class Model
13
 * @package DrMVC\Database
14
 *
15
 * Virtual methods from SQLInterface:
16
 * @method SQLInterface rawSQL(string $query, array $bind = null, bool $fetch = true);
17
 * @method SQLInterface truncate();
18
 *
19
 * Virtual methods from SQLInterface:
20
 * @method NoSQLInterface command(array $query);
21
 */
22
class Model implements ModelInterface
23
{
24
    /**
25
     * @var QueryInterface
26
     */
27
    private $_instance;
28
29
    /**
30
     * Default connection name
31
     * @var string
32
     */
33
    protected $connection = Database::DEFAULT_CONNECTION;
34
35
    /**
36
     * Name of collection for query
37
     * @var string|null
38
     */
39
    protected $collection;
40
41
    /**
42
     * Model constructor.
43
     * @param   ConfigInterface $config
44
     * @param   string $collection name of active collection
45
     */
46
    public function __construct(ConfigInterface $config, string $collection = null)
47
    {
48
        // Create database object with config from above
49
        $config_db = $this->getConfigDB($config);
50
        $database = new Database($config_db);
51
52
        // If collection name is not provided
53
        if (null === $collection) {
54
            // Get current collection name
55
            $collection = $this->getCollection();
56
        }
57
58
        // Extract instance created by driver and put collection name
59
        $instance = $database->getInstance($collection);
60
61
        // Keep driver's instance as local parameter
62
        $this->setInstance($instance);
63
    }
64
65
    /**
66
     * Extract configuration of current database
67
     *
68
     * @param   ConfigInterface $config
69
     * @return  ConfigInterface
70
     */
71
    private function getConfigDB(ConfigInterface $config): ConfigInterface
72
    {
73
        // get current connection
74
        $connection = $this->getConnection();
75
        // Get config of required connection
76
        $config_db = $config->get($connection);
77
        try {
78
            if (null === $config_db) {
79
                throw new Exception("Connection \"$connection\" is not found in database config");
80
            }
81
        } catch (Exception $e) {
82
            // __constructor
83
        }
84
        return $config_db;
85
    }
86
87
    /**
88
     * Get current database name
89
     *
90
     * @return string
91
     */
92
    public function getConnection(): string
93
    {
94
        return $this->connection;
95
    }
96
97
    /**
98
     * Convert class name for snake case
99
     *
100
     * @return  string
101
     */
102
    private function classToShake(): string
103
    {
104
        $className = \get_class($this);
105
        $classArray = explode('\\', $className);
106
        $class = end($classArray);
107
        try {
108
            $class = Stringy::create($class)->underscored();
109
        } catch (\InvalidArgumentException $e) {
110
            new Exception('Invalid argument provided');
111
        }
112
        return $class;
113
    }
114
115
    /**
116
     * Get current collection
117
     *
118
     * @return  string|null
119
     */
120
    public function getCollection()
121
    {
122
        return $this->collection ?? $this->classToShake();
123
    }
124
125
    /**
126
     * Set name of collection for queries
127
     *
128
     * @param   null|string $collection
129
     * @return  ModelInterface
130
     */
131
    public function setCollection(string $collection): ModelInterface
132
    {
133
        $this->collection = $collection;
134
        return $this;
135
    }
136
137
    /**
138
     * Set database instance
139
     *
140
     * @param   QueryInterface $instance
141
     */
142
    private function setInstance(QueryInterface $instance)
143
    {
144
        $this->_instance = $instance;
145
    }
146
147
    /**
148
     * Get database instance
149
     *
150
     * @return QueryInterface
151
     */
152
    public function getInstance(): QueryInterface
153
    {
154
        return $this->_instance;
155
    }
156
157
    /**
158
     * Insert data into table/collection
159
     *
160
     * @param   array $data
161
     * @return  mixed
162
     */
163
    public function insert(array $data)
164
    {
165
        return $this->getInstance()->insert($data);
166
    }
167
168
    /**
169
     * Read data from table/collection
170
     *
171
     * @param   array $where
172
     * @return  mixed
173
     */
174
    public function select(array $where = [])
175
    {
176
        return $this->getInstance()->select($where);
177
    }
178
179
    /**
180
     * Update data in table/collection
181
     *
182
     * @param   array $data
183
     * @param   array $where
184
     * @return  mixed
185
     */
186
    public function update(array $data, array $where = [])
187
    {
188
        return $this->getInstance()->update($data, $where);
189
    }
190
191
    /**
192
     * Delete data from table/collection
193
     *
194
     * @param   array $where
195
     * @return  mixed
196
     */
197
    public function delete(array $where)
198
    {
199
        return $this->getInstance()->delete($where);
200
    }
201
202
    /**
203
     * Call some dynamic method
204
     *
205
     * @param   string $name
206
     * @param   array $arguments
207
     * @return  mixed
208
     */
209
    public function __call($name, $arguments)
210
    {
211
        return $this->getInstance()->$name($arguments);
212
    }
213
}
214