Completed
Push — master ( 47fad5...a0a464 )
by Mr
02:35
created

Model::classToShake()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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