Passed
Push — master ( 73f345...78312e )
by Radu
01:42
created

DatabaseAddQueryTrait::generateAddQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 4
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\AbstractDatabase as Db;
5
6
trait DatabaseAddQueryTrait
7
{
8
    final protected function generateAddQuery($queryType, $tableName, $addData = [], $updateData = [])
9
    {
10
        $multiDimensional = is_array($addData[key($addData)]);
11
        
12
        list($keys, $data) = $this->getKeysValues($addData);
13
        
14
        $query = $this->generateAddQueryPrefix($queryType);
15
        $query .= ' ' . $this->escapeIdentifier($tableName);
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

15
        $query .= ' ' . $this->/** @scrutinizer ignore-call */ escapeIdentifier($tableName);
Loading history...
16
        $query .= $this->generateAddQueryFieldsPart($tableName, $keys);
17
        $query .= $this->generateAddQueryValuesPart($data, $multiDimensional);
18
        
19
        if ($multiDimensional) {
20
            return $query;
21
        }
22
        
23
        $query .= $this->generateAddQueryUpdatePart($updateData);
24
        
25
        return $query;
26
    }
27
    
28
    final protected function getKeysValues($data = [])
29
    {
30
        $multiDimensional = is_array($data[key($data)]);
31
        if ($multiDimensional) {
32
            $keys = array_keys(call_user_func_array('array_merge', $data));
33
            // fill any missing keys with empty data
34
            $key_pair = array_combine($keys, array_fill(0, count($keys), null));
35
            $data = array_map(function ($e) use ($key_pair) {
36
                return array_merge($key_pair, $e);
37
            }, $data);
38
        } else {
39
            $keys = array_keys($data);
40
        }
41
        
42
        return [$keys, $data];
43
    }
44
    
45
    final protected function generateAddQueryPrefix($queryType)
46
    {
47
        switch ($queryType) {
48
            case Db::QUERY_TYPE_REPLACE:
49
                $query = Db::QUERY_TYPE_REPLACE . ' INTO';
50
                break;
51
            case Db::QUERY_TYPE_INSERT_IGNORE:
52
                $query = Db::QUERY_TYPE_INSERT_IGNORE . ' INTO';
53
                break;
54
            case Db::QUERY_TYPE_INSERT:
55
            default:
56
                $query = Db::QUERY_TYPE_INSERT . ' INTO';
57
                break;
58
        }
59
        
60
        return $query;
61
    }
62
    
63
    final protected function generateAddQueryFieldsPart($tableName, $fields)
0 ignored issues
show
Unused Code introduced by
The parameter $tableName 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

63
    final protected function generateAddQueryFieldsPart(/** @scrutinizer ignore-unused */ $tableName, $fields)

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...
64
    {
65
        return ' (' . implode(
66
            ', ',
67
            array_map([$this, 'escapeIdentifier'], $fields)
68
        ) .
69
        ')';
70
    }
71
    
72
    final protected function generateAddQueryValuesPart($data, $multiDimensional)
73
    {
74
        $query = ' VALUES';
75
        if ($multiDimensional) {
76
            $valuesStrings = [];
77
            foreach ($data as $item) {
78
                $valuesStrings[] = $this->generateValuesString($item);
79
            }
80
            $query .= implode(', ', $valuesStrings);
81
        } else {
82
            $query .= $this->generateValuesString($data);
83
        }
84
        return $query;
85
    }
86
    
87
    final protected function generateAddQueryUpdatePart($data = [])
88
    {
89
        if (empty($data)) {
90
            return false;
91
        }
92
        
93
        $strings = [];
94
        foreach ($data as $k => $v) {
95
            $strings[] = sprintf('%s = ?', $this->escapeIdentifier($k));
96
        }
97
        
98
        $query = " ON DUPLICATE KEY UPDATE ";
99
        $query .= implode(', ', $strings);
100
        return $query;
101
    }
102
    
103
    final protected function generateValuesString($data)
104
    {
105
        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

105
        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...
106
            return '?';
107
        }, $data)) . ')';
108
    }
109
}
110