Completed
Push — dev ( 917cb5...992eab )
by Marc
10:46
created

ModelSchema   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 21
c 7
b 1
f 1
lcom 2
cbo 3
dl 0
loc 151
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasColumn() 0 4 3
A hasTable() 0 4 1
A getTable() 0 10 3
A getTables() 0 12 2
A getColumns() 0 4 1
A instantiate() 0 12 2
A isFake() 0 4 2
A hasInstance() 0 4 1
A getInstance() 0 10 3
A getClass() 0 8 2
1
<?php namespace Mascame\Artificer\Model;
2
3
use Schema;
4
5
// Todo: get column type http://stackoverflow.com/questions/18562684/how-to-get-database-field-type-in-laravel
6
class ModelSchema
7
{
8
9
    /**
10
     * @var array
11
     */
12
    public $tables;
13
14
    /**
15
     * @var array
16
     */
17
    public $models;
18
19
    /**
20
     * @var string
21
     */
22
    public $class;
23
24
25
    /**
26
     * @var array
27
     */
28
    public $columns;
29
30
    /**
31
     * @param ModelObtainer $modelObtainer
32
     */
33
    public function __construct(ModelObtainer $modelObtainer)
34
    {
35
        $this->models = $modelObtainer->models;
36
        $this->tables = $this->getTables($this->models);
37
    }
38
39
    /**
40
     * @param $column
41
     * @return bool
42
     */
43
    public function hasColumn($column)
44
    {
45
        return (is_array($column) && in_array($column, $this->columns)) ? true : false;
46
    }
47
48
    /**
49
     * @param $table
50
     * @return bool
51
     */
52
    public function hasTable($table)
53
    {
54
        return Schema::hasTable($table);
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getTable($modelName)
61
    {
62
        if ( ! $modelName) $modelName = Model::getCurrent()->name;
63
64
        if (isset($this->models[$modelName]['table'])) {
65
            return $this->models[$modelName]['table'];
66
        }
67
68
        return $this->getInstance($modelName)->getTable();
69
    }
70
71
    /**
72
     * @param $models
73
     * @return array
74
     */
75
    public function getTables($models)
76
    {
77
        $tables = array();
78
79
        foreach ($models as $model) {
80
            $table = $this->getTable($model['name']);
81
            $this->models[$model['name']]['table'] = $table;
82
            $tables[] = $table;
83
        }
84
85
        return $tables;
86
    }
87
88
    /**
89
     * @param $table
90
     * @return array
91
     */
92
    public function getColumns($table)
93
    {
94
        return Schema::getColumnListing($table);
95
    }
96
97
    /**
98
     * @param $modelName
99
     * @return mixed
100
     */
101
    public function instantiate($modelName)
102
    {
103
        $modelClass = $this->getClass($modelName);
104
105
        if ($this->isFake($modelName)) {
106
            $instance = (new FakeModel())->setup($this->models[$modelName]['fake']);
107
        } else {
108
            $instance = new $modelClass;
109
        }
110
111
        return $this->models[$modelName]['instance'] = $instance;
112
    }
113
114
    public function isFake($modelName)
115
    {
116
        return (isset($this->models[$modelName]['fake']) && $this->models[$modelName]['fake'] !== false);
117
    }
118
119
    /**
120
     * @param $modelName
121
     * @return bool
122
     */
123
    public function hasInstance($modelName)
124
    {
125
        return isset($this->models[$modelName]['instance']);
126
    }
127
128
    /**
129
     * @param null $modelName
130
     * @return mixed
131
     */
132
    public function getInstance($modelName = null)
133
    {
134
        ($modelName) ?: $modelName = Model::getCurrent();
135
136
        if ($this->hasInstance($modelName)) {
137
            return $this->models[$modelName]['instance'];
138
        }
139
140
        return $this->instantiate($modelName);
141
    }
142
143
    /**
144
     * @param $modelName
145
     * @return string
146
     */
147
    public function getClass($modelName)
148
    {
149
        if (! in_array($modelName, array_keys($this->models))) return null;
150
151
        $model = $this->models[$modelName];
152
153
        return $model['namespace'] . '\\' . $model['name'];
154
    }
155
156
}