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

AbstractionLayer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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