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

DatabaseTrait::generateAddQuery()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 8
nop 3
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\AbstractDatabase as Db;
5
6
trait DatabaseTrait
7
{
8
    use DatabaseAddQueryTrait;
9
    
10
    final public function insert($tableName, $addData = [], $updateData = [])
11
    {
12
        return $this->add(Db::QUERY_TYPE_INSERT, $tableName, $addData, $updateData);
13
    }
14
    
15
    final public function insertIgnore($tableName, $data = [])
16
    {
17
        return $this->add(Db::QUERY_TYPE_INSERT_IGNORE, $tableName, $data);
18
    }
19
    
20
    final public function replace($tableName, $data = [])
21
    {
22
        return $this->add(Db::QUERY_TYPE_REPLACE, $tableName, $data);
23
    }
24
    
25
    final protected function add($queryType, $tableName, $addData = [], $updateData = [])
26
    {
27
        if (empty($tableName) || empty($addData)) {
28
            throw new \ErrorException('No data specified');
29
        }
30
        
31
        $query = $this->generateAddQuery($queryType, $tableName, $addData, $updateData);
32
        
33
        $queryData = [];
34
        foreach ($addData as $item) {
35
            $queryData[] = $item;
36
        }
37
        foreach ($updateData as $item) {
38
            $queryData[] = $item;
39
        }
40
        
41
        return $this->query($query, $queryData);
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

41
        return $this->/** @scrutinizer ignore-call */ query($query, $queryData);
Loading history...
42
    }
43
    
44
    final public function valueExists($table, $field, $value)
45
    {
46
        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

46
        return (bool) $this->/** @scrutinizer ignore-call */ getColumn(
Loading history...
47
            sprintf(
48
                "SELECT 1 FROM %s WHERE %s = ? LIMIT 1",
49
                $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

49
                $this->/** @scrutinizer ignore-call */ 
50
                       escapeIdentifier($table),
Loading history...
50
                $this->escapeIdentifier($field)
51
            ),
52
            [$value]
53
        );
54
    }
55
}
56