Passed
Push — master ( be51ba...35c865 )
by Agel_Nash
02:38
created

LegacyDatabase::replaceFullTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 3
1
<?php namespace AgelxNash\Modx\Evo\Database;
2
3
/**
4
 * @deprecated
5
 */
6
class LegacyDatabase extends AbstractDatabase
7
{
8
    /**
9
     * @param string $host
10
     * @param string $database
11
     * @param string $username
12
     * @param string $password
13
     * @param string $prefix
14
     * @param string $charset
15
     * @param string $method
16
     * @param string $collation
17
     * @param string $driver
18
     * @throws Exceptions\Exception
19
     */
20 5
    public function __construct(
21
        $host = '',
22
        $database = '',
23
        $username = '',
24
        $password = '',
25
        $prefix = '',
26
        $charset = 'utf8mb4',
27
        $method = 'SET CHARACTER SET',
28
        $collation = 'utf8mb4_unicode_ci',
29
        $driver = Drivers\MySqliDriver::class
30
    ) {
31 5
        $database = trim($database, '`');
32
33 5
        $this->setConfig(compact(
34 5
            'host',
35 5
            'database',
36 5
            'username',
37 5
            'password',
38 5
            'prefix',
39 5
            'charset',
40 5
            'method',
41 5
            'collation'
42
        ));
43
44 5
        $this->setDriver($driver);
45 5
    }
46
47
    /**
48
     * @param $tableName
49
     * @param bool $force
50
     * @return null|string|string[]
51
     * @throws Exceptions\Exception
52
     */
53 2
    public function replaceFullTableName($tableName, $force = false)
54
    {
55 2
        $tableName = trim($tableName);
56 2
        if ((bool)$force === true) {
57 1
            $result = $this->getFullTableName($tableName);
58 2
        } elseif (strpos($tableName, '[+prefix+]') !== false) {
59 2
            $dbase = trim($this->getConfig('database'), '`');
60 2
            $prefix = $this->getConfig('prefix');
61
62 2
            $result = preg_replace('@\[\+prefix\+\]([0-9a-zA-Z_]+)@', "`{$dbase}`.`{$prefix}$1`", $tableName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $1 seems to be never defined.
Loading history...
63
        } else {
64 2
            $result = $tableName;
65
        }
66
67 2
        return $result;
68
    }
69
70
    /**
71
     * @param mixed $sql
72
     * @return mixed
73
     * @throws Exceptions\Exception
74
     */
75 1
    public function query($sql)
76
    {
77 1
        return parent::query($this->replaceFullTableName($sql));
78
    }
79
}
80