Completed
Push — master ( ab2e37...f0a9c0 )
by Henry
13:19 queued 11:13
created

Db::getSetting()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 7
cp 0.8571
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4.0466
1
<?php
2
namespace Redaxscript;
3
4
use ORM;
5
use PDO;
6
use PDOException;
7
use function implode;
8
use function strstr;
9
10
/**
11
 * children class to handle the database
12
 *
13
 * @since 2.2.0
14
 *
15
 * @package Redaxscript
16
 * @category Db
17
 * @author Henry Ruhs
18
 *
19
 * @method $this _addJoinSource(string $operator, string $table, string|array $constraint, string $tableAlias)
20
 * @method $this _addOrderBy(string $column, string $value)
21
 * @method $this _addWhere(string $clause, string|array $value)
22
 */
23
24
class Db extends ORM
25
{
26
	/**
27
	 * instance of the config class
28
	 *
29
	 * @var Config
30
	 */
31
32
	protected static $_config;
33
34
	/**
35
	 * constructor of the class
36
	 *
37
	 * @since 2.6.0
38
	 *
39
	 * @param Config $config instance of the config class
40
	 */
41
42 4
	public static function construct(Config $config) : void
43
	{
44 4
		self::$_config = $config;
45 4
	}
46
47
	/**
48
	 * init the class
49
	 *
50
	 * @since 3.1.0
51
	 */
52
53 4
	public static function init() : void
54
	{
55 4
		$dbType = self::$_config->get('dbType');
56 4
		$dbHost = self::$_config->get('dbHost');
57 4
		$dbName = self::$_config->get('dbName');
58 4
		$dbUser = self::$_config->get('dbUser');
59 4
		$dbPassword = self::$_config->get('dbPassword');
60 4
		$dbSocket = strstr($dbHost, '.sock');
61
62
		/* handle various types */
63
64 4
		if ($dbType === 'mssql' || $dbType === 'mysql' || $dbType === 'pgsql')
65
		{
66 3
			if ($dbType === 'mssql')
67
			{
68 1
				self::configure('connection_string', 'sqlsrv:server=' . $dbHost . ';database=' . $dbName);
69
			}
70 3
			if ($dbType === 'mysql')
71
			{
72 1
				self::configure('connection_string', 'mysql:' . ($dbSocket ? 'unix_socket' : 'host') . '=' . $dbHost . ';dbname=' . $dbName . ';charset=utf8');
73
			}
74 3
			if ($dbType === 'pgsql')
75
			{
76 1
				self::configure('connection_string', 'pgsql:' . ($dbSocket ? 'unix_socket' : 'host') . '=' . $dbHost . ';dbname=' . $dbName . ';options=--client_encoding=utf8');
77
			}
78
79
			/* username and password */
80
81 3
			self::configure('username', $dbUser);
82 3
			if ($dbPassword)
83
			{
84 3
				self::configure('password', $dbPassword);
85
			}
86
		}
87
88
		/* else handle sqlite */
89
90 4
		if ($dbType === 'sqlite')
91
		{
92 1
			self::configure('sqlite:' . $dbHost);
93
		}
94
95
		/* general */
96
97 4
		self::configure(
98
		[
99 4
			'caching' => true,
100
			'caching_auto_clear' => true,
101
			'return_result_sets' => true
102
		]);
103 4
	}
104
105
	/**
106
	 * get the database status
107
	 *
108
	 * @since 3.1.0
109
	 *
110
	 * @return int
111
	 */
112
113 1
	public static function getStatus() : int
114
	{
115 1
		$output = 0;
116
117
		/* has connection */
118
119
		try
120
		{
121 1
			$dbType = self::$_config->get('dbType') === 'mssql' ? 'sqlsrv' : self::$_config->get('dbType');
122 1
			$dbDriver = self::getDb()->getAttribute(PDO::ATTR_DRIVER_NAME);
123 1
			if ($dbType === $dbDriver)
124
			{
125 1
				$output = self::countTablePrefix() > 7 ? 2 : 1;
126
			}
127
		}
128
		catch (PDOException $exception)
129
		{
130
			$output = 0;
131
		}
132 1
		return $output;
133
	}
134
135
	/**
136
	 * set the auto increment
137
	 *
138
	 * @since 4.0.0
139
	 *
140
	 * @param string $table name of the table
141
	 * @param int $increment value of the auto increment
142
	 *
143
	 * @return bool
144
	 */
145
146
	public static function setAutoIncrement(string $table = null, int $increment = 0) : bool
147
	{
148
		$dbType = self::$_config->get('dbType');
149
		$dbPrefix = self::$_config->get('dbPrefix');
150
151
		/* mysql */
152
153
		if ($dbType === 'mysql')
154
		{
155
			return self::rawExecute('ALTER TABLE ' . $dbPrefix . $table . ' AUTO_INCREMENT = ' . $increment);
156
		}
157
		return false;
158
	}
159
160
	/**
161
	 * count table with prefix
162
	 *
163
	 * @since 3.1.0
164
	 *
165
	 * @return int
166
	 */
167
168 2
	public static function countTablePrefix() : int
169
	{
170 2
		$output = 0;
171 2
		$dbType = self::$_config->get('dbType');
172 2
		$dbName = self::$_config->get('dbName');
173 2
		$dbPrefix = self::$_config->get('dbPrefix');
174
175
		/* mssql and mysql */
176
177 2
		if ($dbType === 'mssql' || $dbType === 'mysql')
178
		{
179
			$output = self::forTable('information_schema.tables')
180
				->where($dbType === 'mssql' ? 'table_catalog' : 'table_schema', $dbName)
181
				->whereLike('table_name', $dbPrefix . '%')
182
				->count();
183
		}
184
185
		/* pgsql */
186
187 2
		if ($dbType === 'pgsql')
188
		{
189
			$output = self::forTable('pg_catalog.pg_tables')
190
				->whereLike('tablename', $dbPrefix . '%')
191
				->whereNotLike('tablename', 'pg_%')
192
				->whereNotLike('tablename', 'sql_%')
193
				->count();
194
		}
195
196
		/* sqlite */
197
198 2
		if ($dbType === 'sqlite')
199
		{
200 2
			$output = self::forTable('sqlite_master')
201 2
				->whereLike('tbl_name', $dbPrefix . '%')
202 2
				->whereNotLike('tbl_name', 'sql_%')
203 2
				->count();
204
		}
205 2
		return $output;
206
	}
207
208
	/**
209
	 * for table with prefix
210
	 *
211
	 * @since 2.2.0
212
	 *
213
	 * @param string $table name of the table
214
	 * @param string $connection which connection to use
215
	 *
216
	 * @return self
217
	 */
218
219 14
	public static function forTablePrefix(string $table = null, string $connection = self::DEFAULT_CONNECTION) : self
220
	{
221 14
		return new self(self::$_config->get('dbPrefix') . $table, [], $connection);
222
	}
223
224
	/**
225
	 * left join with prefix
226
	 *
227
	 * @since 2.2.0
228
	 *
229
	 * @param string $table name of the table
230
	 * @param string|array $constraint constraint as needed
231
	 * @param string $tableAlias alias of the table
232
	 *
233
	 * @return self
234
	 */
235
236 1
	public function leftJoinPrefix(string $table = null, string $constraint = null, string $tableAlias = null) : self
237
	{
238 1
		return $this->_addJoinSource('LEFT', self::$_config->get('dbPrefix') . $table, $constraint, $tableAlias);
239
	}
240
241
	/**
242
	 * where like with many
243
	 *
244
	 * @since 3.0.0
245
	 *
246
	 * @param array $columnArray array of column names
247
	 * @param array $likeArray array of the like
248
	 *
249
	 * @return self
250
	 */
251
252 1
	public function whereLikeMany(array $columnArray = [], array $likeArray = []) : self
253
	{
254 1
		return $this->_addWhere('(' . implode($columnArray, ' LIKE ? OR ') . ' LIKE ?)', $likeArray);
255
	}
256
257
	/**
258
	 * where language is
259
	 *
260
	 * @since 3.0.0
261
	 *
262
	 * @param string $language value of the language
263
	 *
264
	 * @return self
265
	 */
266
267 3
	public function whereLanguageIs(string $language = null) : self
268
	{
269 3
		return $this->_addWhere('(language = ? OR language IS NULL)', $language);
270
	}
271
272
	/**
273
	 * order by global setting
274
	 *
275
	 * @since 3.3.0
276
	 *
277
	 * @param string $column name of the column
278
	 *
279
	 * @return self
280
	 */
281
282 1
	public function orderBySetting(string $column = null) : self
283
	{
284 1
		$order = $this->getSetting('order');
285 1
		return $this->_addOrderBy($column, $order);
286
	}
287
288
	/**
289
	 * limit by global setting
290
	 *
291
	 * @since 3.3.0
292
	 *
293
	 * @param int $step step of the offset
294
	 *
295
	 * @return self
296
	 */
297
298 1
	public function limitBySetting(int $step = null) : self
299
	{
300 1
		$limit = $this->getSetting('limit');
301 1
		$this->_limit = $step > 0 ? $step * $limit . ',' . $limit : $limit;
302 1
		return $this;
303
	}
304
305
	/**
306
	 * get the value from settings
307
	 *
308
	 * @since 4.0.0
309
	 *
310
	 * @param string $key key of the item
311
	 *
312
	 * @return string|null
313
	 */
314
315 2
	public function getSetting(string $key = null) : ?string
316
	{
317 2
		$settings = self::forTablePrefix('settings')->findMany();
318
319
		/* process settings */
320
321 2
		if ($key)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
322
		{
323 2
			foreach ($settings as $setting)
324
			{
325 2
				if ($setting->name === $key)
326
				{
327 2
					return $setting->value;
328
				}
329
			}
330
		}
331
		return null;
332
	}
333
}
334