Completed
Push — master ( ccece0...29ccf9 )
by Adrian
04:44
created

AbstractionLayer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 9
c 6
b 1
f 0
lcom 1
cbo 1
dl 0
loc 67
ccs 24
cts 28
cp 0.8571
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTableForeignKeys() 0 8 1
A getModel() 0 4 1
A getSchemaManager() 0 6 1
A getTableColumns() 0 8 1
A getTableColumn() 0 10 2
A getModelAttributes() 0 11 2
1
<?php
2
namespace ANavallaSuiza\Laravel\Database\Dbal\Eloquent;
3
4
use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer as AbstractionLayerContract;
5
use Illuminate\Database\Eloquent\Model as EloquentModel;
6
use Schema;
7
8
class AbstractionLayer implements AbstractionLayerContract
9
{
10
    /**
11
     * @var EloquentModel
12
     */
13
    protected $model;
14
15
    /**
16
     */
17 9
    public function __construct(EloquentModel $model)
18
    {
19 9
        $this->model = $model;
20 9
    }
21
22 1
    public function getModel()
23
    {
24 1
        return $this->model;
25
    }
26
27 4
    protected function getSchemaManager()
28
    {
29 4
        $connection = Schema::getConnection();
30
31 4
        return $connection->getDoctrineSchemaManager();
32
    }
33
34 4
    public function getTableColumns()
35
    {
36 4
        $sm = $this->getSchemaManager();
37
38 4
        $columns = $sm->listTableColumns($this->model->getTable());
39
40 4
        return $columns;
41
    }
42
43 2
    public function getTableColumn($name)
44
    {
45 2
        $columns = $this->getTableColumns();
46
47 2
        if (! array_key_exists($name, $columns)) {
48 1
            throw new \Exception("Column ".$name." not found on ".$this->model->getTable());
49
        }
50
51 1
        return $columns[$name];
52
    }
53
54 1
    public function getModelAttributes()
55
    {
56 1
        $columns = $this->getTableColumns();
57
58 1
        $attributes = [];
59 1
        foreach ($columns as $fieldName => $field) {
60 1
            $attributes[] = $fieldName;
61 1
        }
62
63 1
        return $attributes;
64
    }
65
66
    public function getTableForeignKeys()
67
    {
68
        $sm = $this->getSchemaManager();
69
70
        $foreignKeys = $sm->listTableForeignKeys($this->model->getTable());
71
72
        return $foreignKeys;
73
    }
74
}
75