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

SQL   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 21
Bugs 5 Features 1
Metric Value
wmc 17
c 21
b 5
f 1
lcom 2
cbo 4
dl 0
loc 99
ccs 40
cts 50
cp 0.8
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConnection() 0 4 1
A create() 0 6 1
A drop() 0 8 2
A truncate() 0 4 1
A createIndexes() 0 7 1
A insert() 0 8 2
A update() 0 4 1
A delete() 0 8 2
A get() 0 4 1
A find() 0 16 3
A query() 0 4 1
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