Passed
Push — master ( 76d3e9...73f345 )
by Radu
01:24
created

DatabaseTrait::valueExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\AbstractDatabase as Db;
5
6
trait DatabaseTrait
7
{
8
    public function insert($tableName, $data = [])
9
    {
10
        return $this->add(Db::QUERY_TYPE_INSERT, $tableName, $data);
11
    }
12
    
13
    public function insertIgnore($tableName, $data = [])
14
    {
15
        return $this->add(Db::QUERY_TYPE_INSERT_IGNORE, $tableName, $data);
16
    }
17
    
18
    public function replace($tableName, $data = [])
19
    {
20
        return $this->add(Db::QUERY_TYPE_REPLACE, $tableName, $data);
21
    }
22
    
23
    protected function add($queryType, $tableName, $data = [])
24
    {
25
        if (empty($tableName) || empty($data)) {
26
            throw new \ErrorException('No data specified');
27
        }
28
        
29
        $query = $this->generateAddQuery($queryType, $tableName, $data);
30
        
31
        return $this->query($query, $data);
0 ignored issues
show
Bug introduced by
It seems like query() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return $this->/** @scrutinizer ignore-call */ query($query, $data);
Loading history...
32
    }
33
    
34
    public function valueExists($table, $field, $value)
35
    {
36
        return (bool) $this->getColumn(
0 ignored issues
show
Bug introduced by
It seems like getColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        return (bool) $this->/** @scrutinizer ignore-call */ getColumn(
Loading history...
37
            sprintf(
38
                "SELECT 1 FROM %s WHERE %s = ? LIMIT 1",
39
                $this->escapeIdentifier($table),
0 ignored issues
show
Bug introduced by
It seems like escapeIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
                $this->/** @scrutinizer ignore-call */ 
40
                       escapeIdentifier($table),
Loading history...
40
                $this->escapeIdentifier($field)
41
            ),
42
            [$value]
43
        );
44
    }
45
    
46
    protected function getKeysValues($data = [])
47
    {
48
        $multiDimensional = is_array($data[key($data)]);
49
        if ($multiDimensional) {
50
            $keys = array_keys(call_user_func_array('array_merge', $data));
51
            // fill any missing keys with empty data
52
            $key_pair = array_combine($keys, array_fill(0, count($keys), null));
53
            $data = array_map(function ($e) use ($key_pair) {
54
                return array_merge($key_pair, $e);
55
            }, $data);
56
        } else {
57
            $keys = array_keys($data);
58
        }
59
        
60
        return [$keys, $data];
61
    }
62
    
63
    protected function generateAddQuery($queryType, $tableName, $data)
64
    {
65
        $multiDimensional = is_array($data[key($data)]);
66
        
67
        list($keys, $data) = $this->getKeysValues($data);
68
        
69
        switch ($queryType) {
70
            case Db::QUERY_TYPE_REPLACE:
71
                $query = Db::QUERY_TYPE_REPLACE . ' INTO';
72
                break;
73
            case Db::QUERY_TYPE_INSERT_IGNORE:
74
                $query = Db::QUERY_TYPE_INSERT_IGNORE . ' INTO';
75
                break;
76
            case Db::QUERY_TYPE_INSERT:
77
            default:
78
                $query = Db::QUERY_TYPE_INSERT . ' INTO';
79
                break;
80
        }
81
        $query .= ' '.$this->escapeIdentifier($tableName).' (' .
82
        implode(', ', array_map([$this, 'escapeIdentifier'], $keys)) .
83
        ') VALUES';
84
        if ($multiDimensional) {
85
            $valuesStrings = [];
86
            foreach ($data as $item) {
87
                $valuesStrings[] = $this->generateValuesString($item);
88
            }
89
            $query .= implode(', ', $valuesStrings);
90
        } else {
91
            $query .= $this->generateValuesString($data);
92
        }
93
        return $query;
94
    }
95
    
96
    protected function generateValuesString($data)
97
    {
98
        return ' (' . implode(', ', array_map(function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

98
        return ' (' . implode(', ', array_map(function (/** @scrutinizer ignore-unused */ $v) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
            return '?';
100
        }, $data)) . ')';
101
    }
102
}
103