LegacyDatabase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 83
ccs 33
cts 33
cp 1
rs 10

3 Methods

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

 

OSZAR »