SQL   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 78.72%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 2
cbo 4
dl 0
loc 91
ccs 37
cts 47
cp 0.7872
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 4 1
A __construct() 0 5 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 query() 0 4 1
A find() 0 16 3
1
<?php
2
3
namespace Soupmix;
4
5
/*
6
SQL Adapter
7
*/
8
9
use Doctrine\DBAL\Connection;
10
11
final 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(string $collection, array $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(string $collection)
35
    {
36 3
        $schemaManager = $this->doctrine->getSchemaManager();
37 3
        if ($schemaManager->tablesExist([$collection])) {
38
            return $schemaManager->dropTable($collection);
39
        }
40 3
        return null;
41
    }
42
43
    public function truncate(string $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(string $collection, array $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(string $collection, array $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(string $collection, array $filter, array $values)
65
    {
66
        return $this->doctrine->update($collection, $values, $filter);
67
    }
68
69 2
    public function delete(string $collection, array $filter)
70
    {
71 2
        $numberOfDeletedItems = $this->doctrine->delete($collection, $filter);
72 2
        return ($numberOfDeletedItems > 0) ? 1 : 0;
73
    }
74
75 1
    public function get(string $collection, $docId)
76
    {
77 1
        return $this->doctrine->fetchAssoc('SELECT * FROM ' . $collection . ' WHERE id = ?', array($docId));
78
    }
79
80 1
    public function find(string $collection, ?array $filters, ?array $fields = null, ?array $sort = null, ?int $offset = 0, ?int $limit = 25)
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
        }
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(string $collection)
98
    {
99 2
        return new SQLQueryBuilder($collection, $this);
100
    }
101
}
102