Completed
Push — 9.0-dev ( 007112...d6cf5f )
by Radu
01:33
created

DatabaseTrait::generateAddQuery()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
    }
26
    
27
    protected function getKeysValues($data = [])
28
    {
29
        $multiDimensional = is_array($data[key($data)]);
30
        if ($multiDimensional) {
31
            $keys = array_keys(call_user_func_array('array_merge', $data));
32
            // fill any missing keys with empty data
33
            $key_pair = array_combine($keys, array_fill(0, count($keys), null));
34
            $data = array_map(function ($e) use ($key_pair) {
35
                return array_merge($key_pair, $e);
36
            }, $data);
37
        } else {
38
            $keys = array_keys($data);
39
        }
40
        
41
        return [$keys, $data];
42
    }
43
    
44
    protected function generateAddQuery($queryType, $tableName, $data)
45
    {
46
        $multiDimensional = is_array($data[key($data)]);
47
        
48
        list($keys, $data) = $this->getKeysValues($data);
49
        
50
        switch ($queryType) {
51
            case self::QUERY_TYPE_REPLACE:
52
                $query = self::QUERY_TYPE_REPLACE . ' INTO';
53
                break;
54
            case self::QUERY_TYPE_INSERT_IGNORE:
55
                $query = self::QUERY_TYPE_INSERT_IGNORE . ' INTO';
56
                break;
57
            case self::QUERY_TYPE_INSERT:
58
            default:
59
                $query = self::QUERY_TYPE_INSERT . ' INTO';
60
                break;
61
        }
62
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
        implode(', ', array_map([$this, 'escapeIdentifier'], $keys)) .
64
        ') VALUES';
65
        if ($multiDimensional) {
66
            $valuesStrings = [];
67
            foreach ($data as $item) {
68
                $valuesStrings[] = $this->generateValuesString($item);
69
            }
70
            $query .= implode(', ', $valuesStrings);
71
        } else {
72
            $query .= $this->generateValuesString($data);
73
        }
74
        return $query;
75
    }
76
    
77
    protected function generateValuesString($data)
78
    {
79
        return ' (' . implode(', ', array_map(function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed.

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

Loading history...
80
            return '?';
81
        }, $data)) . ')';
82
    }
83
}
84