Database::insertFrom()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 6
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
1
<?php namespace EvolutionCMS;
2
3
use AgelxNash\Modx\Evo\Database\Database as BaseDatabase;
4
use AgelxNash\Modx\Evo\Database\Drivers\IlluminateDriver;
5
use Exception;
6
use AgelxNash\Modx\Evo\Database\Exceptions;
7
8
class Database extends BaseDatabase implements Interfaces\DatabaseInterface
9
{
10
    public $config;
11
12
    /**
13
     * @param $tableName
14
     * @param bool $force
15
     * @return null|string|string[]
16
     * @throws Exceptions\TableNotDefinedException
17
     */
18 View Code Duplication
    public function replaceFullTableName($tableName, $force = false)
19
    {
20
        $tableName = trim($tableName);
21
        if ((bool)$force === true) {
22
            $result = $this->getFullTableName($tableName);
23
        } elseif (strpos($tableName, '[+prefix+]') !== false) {
24
            $dbase = trim($this->getConfig('database'), '`');
25
            $prefix = $this->getConfig('prefix');
26
27
            $result = preg_replace(
28
                '@\[\+prefix\+\](\w+)@',
29
                '`' . $dbase . '`.`' . $prefix . '$1`',
30
                $tableName
31
            );
32
        } else {
33
            $result = $tableName;
34
        }
35
36
        return $result;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function query($sql)
43
    {
44
        try {
45
            $out = [];
46 View Code Duplication
            if (\is_array($sql)) {
47
                foreach ($sql as $query) {
48
                    $out[] = parent::query($this->replaceFullTableName($query));
0 ignored issues
show
Bug introduced by
It seems like $this->replaceFullTableName($query) targeting EvolutionCMS\Database::replaceFullTableName() can also be of type null; however, AgelxNash\Modx\Evo\Datab...stractDatabase::query() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
                }
50
            } else {
51
                $out = parent::query($this->replaceFullTableName($sql));
0 ignored issues
show
Bug introduced by
It seems like $this->replaceFullTableName($sql) targeting EvolutionCMS\Database::replaceFullTableName() can also be of type null; however, AgelxNash\Modx\Evo\Datab...stractDatabase::query() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
            }
53
54
            return $out;
55
        } catch (Exception $exception) {
56
            evolutionCMS()->getService('ExceptionHandler')->messageQuit($exception->getMessage());
57
        }
58
    }
59
60
    public function insertFrom(
61
        $fields,
62
        $table,
63
        $fromFields = '*',
64
        $fromTable = '',
65
        $where = '',
66
        $limit = ''
67
    ) {
68
        if (is_array($fields)) {
69
            $onlyKeys = true;
70
            foreach ($fields as $key => $value) {
71
                if (!empty($value)) {
72
                    $onlyKeys = false;
73
                    break;
74
                }
75
            }
76
            if ($onlyKeys) {
77
                $fields = array_keys($fields);
78
            }
79
        }
80
81
        return parent::insertFrom($fields, $table, $fromFields, $fromTable, $where, $limit);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function setDebug($flag)
88
    {
89
        parent::setDebug($flag);
90
        $driver = $this->getDriver();
91
        if ($driver instanceof IlluminateDriver) {
92
            if ($this->isDebug()) {
93
                $driver->getConnect()->enableQueryLog();
94
            } else {
95
                $driver->getConnect()->disableQueryLog();
96
            }
97
        }
98
    }
99
}
100