Completed
Push — master ( 17dbbc...76a453 )
by Mehmet
02:16
created

SQL   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 81.25%

Importance

Changes 22
Bugs 6 Features 1
Metric Value
wmc 17
c 22
b 6
f 1
lcom 2
cbo 4
dl 0
loc 92
ccs 39
cts 48
cp 0.8125
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 6 1
A insert() 0 8 2
A update() 0 4 1
A delete() 0 5 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 3
    public function __construct($config, Connection $client)
17
    {
18 3
        $this->doctrine = $client;
19 3
        $this->dbName = $config['db_name'];
20 3
    }
21
22 2
    public function getConnection()
23
    {
24 2
        return $this->doctrine;
25
    }
26
27 3
    public function create($collection, $fields)
28
    {
29 3
        $schemaManager = $this->doctrine->getSchemaManager();
30 3
        $table = new SQLTable($schemaManager, $collection, $fields);
31 3
        return $table->createTable();
32
    }
33
34 3
    public function drop($collection)
35
    {
36 3
        $schemaManager = $this->doctrine->getSchemaManager();
37 3
        if ($schemaManager->tablesExist([$collection])) {
38 2
            return $schemaManager->dropTable($collection);
39
        }
40 1
        return null;
41
    }
42
43
    public function truncate($collection)
44
    {
45
        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...
46
    }
47
48
    public function createIndexes($collection, $fields)
49
    {
50
        $schemaManager = $this->doctrine->getSchemaManager();
51
        $table = new SQLTable($schemaManager, $collection, $fields);
52
        return $table->createOnlyIndexes();
53
    }
54
55 3
    public function insert($collection, $values)
56
    {
57 3
        $insertion = $this->doctrine->insert($collection, $values);
58 3
        if ($insertion !== 0) {
59 3
            return $this->doctrine->lastInsertId();
60
        }
61
        return null;
62
    }
63
64
    public function update($collection, $filter, $values)
65
    {
66
        return $this->doctrine->update($collection, $values, $filter);
67
    }
68
69 2
    public function delete($collection, $filter)
70
    {
71 2
        $numberOfDeletedItems = $this->doctrine->delete($collection, $filter);
72 2
        return ($numberOfDeletedItems > 0) ? 1 : 0;
73
    }
74
75 1
    public function get($collection, $docId)
76
    {
77 1
        return $this->doctrine->fetchAssoc('SELECT * FROM ' . $collection . ' WHERE id = ?', array($docId));
78
    }
79
80 1
    public function find($collection, $filters, $fields = null, $sort = null, $offset = 0, $limit = 25, $debug = false)
81
    {
82 1
        $query = $this->query($collection);
83 1
        foreach ($filters as $filter => $value) {
84 1
            if (is_array($value)) {
85 1
                $query->orFilters($value);
86 1
                continue;
87
            }
88 1
            $query->andFilter($filter, $value);
89 1
        }
90 1
        return $query->returnFields($fields)
91 1
            ->sortFields($sort)
92 1
            ->offset($offset)
93 1
            ->limit($limit)
94 1
            ->run();
95
    }
96
97 2
    public function query($collection)
98
    {
99 2
        return new SQLQueryBuilder($collection, $this);
100
    }
101
102
}
103