Completed
Push — master ( 084441...17dbbc )
by Mehmet
03:59
created

SQL::buildQueryForAnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 6
Ratio 37.5 %

Code Coverage

Tests 9
CRAP Score 2.1165

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 16
ccs 9
cts 13
cp 0.6923
rs 9.4285
cc 2
eloc 11
nc 2
nop 3
crap 2.1165
1
<?php
2
3
namespace Soupmix;
4
/*
5
SQL Adapter
6
*/
7
8
use Doctrine\DBAL\Connection;
9
10
11
class SQL implements Base
12
{
13
    protected $doctrine = null;
14
    protected $dbName = null;
15
16
17 3
    public function __construct($config, Connection $client)
18
    {
19 3
        $this->doctrine = $client;
20 3
        $this->dbName = $config['db_name'];
21 3
    }
22
23 2
    public function getConnection()
24
    {
25 2
        return $this->doctrine;
26
    }
27
28 3
    public function create($collection, $fields)
29
    {
30 3
        $schemaManager = $this->doctrine->getSchemaManager();
31 3
        $table = new SQLTable($schemaManager, $collection, $fields);
32 3
        return $table->createTable();
33
    }
34
35
36 3
    public function drop($collection)
37
    {
38 3
        $schemaManager = $this->doctrine->getSchemaManager();
39 3
        if ($schemaManager->tablesExist([$collection])) {
40 3
            return $schemaManager->dropTable($collection);
41
        }
42 1
        return null;
43
    }
44
45
    public function truncate($collection)
46
    {
47
        return $this->client->doctrine->query('TRUNCATE TABLE `' . $collection . '`');
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
    }
49
50
    public function createIndexes($collection, $fields)
51
    {
52
53
        $schemaManager = $this->doctrine->getSchemaManager();
54
        $table = new SQLTable($schemaManager, $collection, $fields);
55
        return $table->createOnlyIndexes();
56
    }
57
58 3
    public function insert($collection, $values)
59
    {
60 3
        $insertion = $this->doctrine->insert($collection, $values);
61 3
        if($insertion !== 0) {
62 3
            return $this->doctrine->lastInsertId();
63
        }
64
        return null;
65
    }
66
67
    public function update($collection, $filter, $values)
68
    {
69
        return $this->doctrine->update($collection, $values, $filter);
70
    }
71
72 2
    public function delete($collection, $filter)
73
    {
74 2
        $numberOfDeletedItems = $this->doctrine->delete($collection, $filter);
75 2
        if ($numberOfDeletedItems>0) {
76 2
            return 1;
77
        }
78
        return 0;
79
    }
80
81 1
    public function get($collection, $docId)
82
    {
83 1
        return $this->doctrine->fetchAssoc('SELECT * FROM ' . $collection . ' WHERE id = ?', array($docId));
84
    }
85
86 1
    public function find($collection, $filters, $fields = null, $sort = null, $offset = 0, $limit = 25, $debug = false)
87
    {
88 1
        $query = $this->query($collection);
89 1
        foreach ($filters as $filter => $value) {
90 1
            if (is_array($value)) {
91 1
                $query->orFilters($value);
92 1
            } else {
93 1
                $query->andFilter($filter, $value);
94
            }
95 1
        }
96 1
        return $query->returnFields($fields)
97 1
            ->sortFields($sort)
98 1
            ->offset($offset)
99 1
            ->limit($limit)
100 1
            ->run();
101
    }
102
103 2
    public function query($collection)
104
    {
105 2
        return new SQLQueryBuilder($collection, $this);
106
    }
107
108
109
}
110