Test Failed
Pull Request — master (#4)
by Dmytro
04:37
created

LegacyDatabase::replaceFullTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.4053

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 19
ccs 6
cts 13
cp 0.4615
rs 9.8333
cc 3
nc 3
nop 2
crap 4.4053
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 2
    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 2
        $database = trim($database, '`');
32
33 2
        $this->setConfig(compact(
34 2
            'host',
35 2
            'database',
36 2
            'username',
37 2
            'password',
38 2
            'prefix',
39 2
            'charset',
40 2
            'method',
41 2
            'collation'
42
        ));
43
44 2
        $this->setDriver($driver);
45 2
    }
46
47
    /**
48
     * @param $tableName
49
     * @param bool $force
50
     * @return null|string|string[]
51
     * @throws Exceptions\TableNotDefinedException
52
     */
53 1
    public function replaceFullTableName($tableName, $force = false)
54
    {
55 1
        $tableName = trim($tableName);
56 1
        if ((bool)$force === true) {
57
            $result = $this->getFullTableName($tableName);
58 1
        } elseif (strpos($tableName, '[+prefix+]') !== false) {
59
            $dbase = trim($this->getConfig('database'), '`');
60
            $prefix = $this->getConfig('prefix');
61
62
            $result = preg_replace(
63
                '@\[\+prefix\+\](\w+)@',
64
                '`' . $dbase . '`.`' . $prefix . '$1`',
65
                $tableName
66
            );
67
        } else {
68 1
            $result = $tableName;
69
        }
70
71 1
        return $result;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function query($sql)
78
    {
79
        $out = [];
80
        if (\is_array($sql)) {
81
            foreach ($sql as $query) {
82
                $out[] = parent::query($this->replaceFullTableName($query));
83
            }
84
        } else {
85
            $out = parent::query($this->replaceFullTableName($sql));
86
        }
87
88
        return $out;
89
    }
90
}
91