Issues (9)

src/Database/Model.php (9 issues)

Labels
1
<?php
2
3
namespace DrMVC\Database;
4
5
use MongoDB\Driver\Manager;
6
use PDO;
7
use DrMVC\Config\ConfigInterface;
8
use DrMVC\Database\Drivers\Interfaces\QueryInterface;
9
use DrMVC\Database\Drivers\Interfaces\SQLInterface;
10
use DrMVC\Database\Drivers\Interfaces\MongodbInterface;
11
use DrMVC\Database\Interfaces\ModelInterface;
12
use Stringy\Stringy;
13
14
/**
15
 * Wrapper for simple work with database objects
16
 *
17
 * @package DrMVC\Database
18
 * @since   3.0
19
 *
20
 * Virtual methods from SQLInterface:
21
 * @method SQLInterface rawSQL(string $query, array $bind = null, bool $fetch = true);
22
 * @method SQLInterface truncate();
23
 *
24
 * Virtual methods from MoSQLInterface:
25
 * @method MongodbInterface command(array $query);
26
 */
27
class Model implements ModelInterface
28
{
29
    /**
30
     * Connection with database will be stored here
31
     * @var PDO|Manager|QueryInterface
32
     */
33
    private $_instance;
34
35
    /**
36
     * Default connection name
37
     * @var string
38
     */
39
    protected $connection = Database::DEFAULT_CONNECTION;
40
41
    /**
42
     * Name of collection for query
43
     * @var string|null
44
     */
45
    protected $collection;
46
47
    /**
48
     * Model constructor.
49
     * @param   ConfigInterface $config
50
     * @param   string $collection name of active collection
51
     */
52
    public function __construct(ConfigInterface $config, string $collection = null)
53
    {
54
        // Create database object with config from above
55
        $config_db = $this->getConfigDB($config);
56
        $database = new Database($config_db);
57
58
        // If collection name is not provided
59
        if (null === $collection) {
60
            // Get current collection name
61
            $collection = $this->getCollection();
62
        }
63
64
        // Extract instance created by driver and put collection name
65
        $instance = $database->getInstance($collection);
66
67
        // Keep driver's instance as local parameter
68
        $this->setInstance($instance);
0 ignored issues
show
It seems like $instance can also be of type DrMVC\Database\Drivers\Interfaces\QueryInterface; however, parameter $instance of DrMVC\Database\Model::setInstance() does only seem to accept PDO|MongoDB\Driver\Manager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $this->setInstance(/** @scrutinizer ignore-type */ $instance);
Loading history...
69
    }
70
71
    /**
72
     * Extract configuration of current database
73
     *
74
     * @param   ConfigInterface $config
75
     * @return  ConfigInterface
76
     */
77
    private function getConfigDB(ConfigInterface $config): ConfigInterface
78
    {
79
        // get current connection
80
        $connection = $this->getConnection();
81
        // Get config of required connection
82
        $config_db = $config->get($connection);
83
        try {
84
            if (null === $config_db) {
85
                throw new Exception("Connection \"$connection\" is not found in database config");
86
            }
87
        } catch (Exception $e) {
88
            // __constructor
89
        }
90
        return $config_db;
91
    }
92
93
    /**
94
     * Get current connection
95
     *
96
     * @return  string
97
     */
98
    public function getConnection(): string
99
    {
100
        return $this->connection;
101
    }
102
103
    /**
104
     * Convert class name for snake case
105
     *
106
     * @return  string
107
     */
108
    private function classToShake(): string
109
    {
110
        $className = \get_class($this);
111
        $classArray = explode('\\', $className);
112
        $class = end($classArray);
113
        try {
114
            $class = Stringy::create($class)->underscored();
115
        } catch (\InvalidArgumentException $e) {
116
            new Exception('Invalid argument provided');
117
        }
118
        return $class;
119
    }
120
121
    /**
122
     * Get current collection
123
     *
124
     * @return  string|null
125
     */
126
    public function getCollection()
127
    {
128
        return $this->collection ?? $this->classToShake();
129
    }
130
131
    /**
132
     * Set name of collection for queries
133
     *
134
     * @param   null|string $collection
135
     * @return  ModelInterface
136
     */
137
    public function setCollection(string $collection): ModelInterface
138
    {
139
        $this->collection = $collection;
140
        return $this;
141
    }
142
143
    /**
144
     * Set database instance
145
     *
146
     * @param   PDO|Manager $instance
147
     */
148
    private function setInstance($instance)
149
    {
150
        $this->_instance = $instance;
151
    }
152
153
    /**
154
     * Get database instance
155
     *
156
     * @return  PDO|Manager|QueryInterface
157
     */
158
    public function getInstance()
159
    {
160
        return $this->_instance;
161
    }
162
163
    /**
164
     * Insert data into table/collection
165
     *
166
     * @param   array $data array of columns and values
167
     * @return  mixed
168
     */
169
    public function insert(array $data)
170
    {
171
        return $this->getInstance()->insert($data);
0 ignored issues
show
The method insert() does not exist on PDO. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
        return $this->getInstance()->/** @scrutinizer ignore-call */ insert($data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method insert() does not exist on MongoDB\Driver\Manager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
        return $this->getInstance()->/** @scrutinizer ignore-call */ insert($data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
    }
173
174
    /**
175
     * Read data from table/collection
176
     *
177
     * @param   array $where
178
     * @param   array $nosql_options
179
     * @return  mixed
180
     */
181
    public function select(array $where = [], array $nosql_options = [])
182
    {
183
        return $this->getInstance()->select($where, $nosql_options);
0 ignored issues
show
The method select() does not exist on PDO. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
        return $this->getInstance()->/** @scrutinizer ignore-call */ select($where, $nosql_options);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method select() does not exist on MongoDB\Driver\Manager. Did you maybe mean selectServer()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
        return $this->getInstance()->/** @scrutinizer ignore-call */ select($where, $nosql_options);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
    }
185
186
    /**
187
     * Update data in table/collection
188
     *
189
     * @param   array $data
190
     * @param   array $where
191
     * @return  mixed
192
     */
193
    public function update(array $data, array $where = [])
194
    {
195
        return $this->getInstance()->update($data, $where);
0 ignored issues
show
The method update() does not exist on PDO. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
        return $this->getInstance()->/** @scrutinizer ignore-call */ update($data, $where);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method update() does not exist on MongoDB\Driver\Manager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
        return $this->getInstance()->/** @scrutinizer ignore-call */ update($data, $where);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
    }
197
198
    /**
199
     * Delete data from table/collection
200
     *
201
     * @param   array $where
202
     * @return  mixed
203
     */
204
    public function delete(array $where)
205
    {
206
        return $this->getInstance()->delete($where);
0 ignored issues
show
The method delete() does not exist on PDO. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        return $this->getInstance()->/** @scrutinizer ignore-call */ delete($where);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method delete() does not exist on MongoDB\Driver\Manager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        return $this->getInstance()->/** @scrutinizer ignore-call */ delete($where);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207
    }
208
209
    /**
210
     * Call some dynamic method
211
     *
212
     * @param   string $name
213
     * @param   array $arguments
214
     * @return  mixed
215
     */
216
    public function __call($name, $arguments)
217
    {
218
        return $this->getInstance()->$name($arguments);
219
    }
220
}
221