Completed
Push — dev ( 979e1f...726aeb )
by Marc
02:08
created

Model   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 15
Bugs 3 Features 1
Metric Value
wmc 33
c 15
b 3
f 1
lcom 3
cbo 6
dl 0
loc 258
rs 9.3999

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentModelData() 0 12 1
A isCurrent() 0 6 2
A getCurrent() 0 4 2
A getCurrentClass() 0 4 1
A getRouteName() 0 6 3
A getOptions() 0 8 3
A __construct() 0 11 2
A share() 0 6 1
A getCurrentModelsData() 0 9 2
A isHidden() 0 4 2
A hasGuarded() 0 4 1
A hasFillable() 0 4 1
A getCurrentModelName() 0 14 4
A prepareCurrentModel() 0 9 1
A setCurrent() 0 5 1
A getDefaultOptions() 0 6 2
A getOption() 0 7 3
A getRelations() 0 4 1
1
<?php namespace Mascame\Artificer\Model;
2
3
use Illuminate\Contracts\Database\ModelIdentifier;
4
use View;
5
use Route;
6
use \Illuminate\Support\Str as Str;
7
use Mascame\Artificer\Options\AdminOption;
8
use Mascame\Artificer\Options\ModelOption;
9
10
// Todo: get column type http://stackoverflow.com/questions/18562684/how-to-get-database-field-type-in-laravel
11
class Model
12
{
13
14
    /**
15
     * @var ModelSchema
16
     */
17
    public $schema;
18
19
    /**
20
     * @var array
21
     */
22
    public $models;
23
24
    /**
25
     * @var array
26
     */
27
    public $columns;
28
29
    /**
30
     * @var \Illuminate\Database\Eloquent\Model
31
     */
32
    public $model;
33
34
    /**
35
     * @var string
36
     */
37
    public $class;
38
39
    /**
40
     * @var
41
     */
42
    public $name;
43
44
    /**
45
     * @var string
46
     */
47
    public $keyname;
48
49
    /**
50
     * @var
51
     */
52
    public $table;
53
54
    /**
55
     * @var
56
     */
57
    public $fillable;
58
59
    /**
60
     * @var array|mixed
61
     */
62
    protected $options = [];
63
    protected $defaultOptions = null;
64
65
    /**
66
     * @var array|mixed
67
     */
68
    public $relations = [];
69
70
    /**
71
     * @var
72
     */
73
    public static $current = null;
74
75
    /**
76
     * @param ModelSchema $schema
77
     */
78
    public function __construct(ModelSchema $schema)
79
    {
80
        $this->schema = $schema;
81
        $this->relations = new ModelRelation();
82
83
        if (Str::startsWith(Route::currentRouteName(), 'admin.model.')) {
84
            $this->prepareCurrentModel();
85
        }
86
87
        $this->share();
88
    }
89
90
91
    public function share()
92
    {
93
        View::share('tables', $this->schema->tables);
94
        View::share('models', $this->getCurrentModelsData());
95
        View::share('model', $this->getCurrentModelData());
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    private function getCurrentModelsData()
102
    {
103
        foreach ($this->schema->models as $modelName => $model) {
104
            $this->schema->models[$modelName]['options'] = $this->getOptions($modelName);
105
            $this->schema->models[$modelName]['hidden'] = $this->isHidden($modelName);
106
        }
107
108
        return $this->schema->models;
109
    }
110
111
    /**
112
     * @param $modelName
113
     * @return bool
114
     */
115
    public function isHidden($modelName)
116
    {
117
        return (in_array($modelName, AdminOption::get('model.hidden'))) ? true : false;
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function hasGuarded()
124
    {
125
        return ! empty($this->getOption('guarded', []));
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function hasFillable()
132
    {
133
        return ! empty($this->getOption('fillable', []));
134
    }
135
136
    /**
137
     * @return int|null|string
138
     */
139
    private function getCurrentModelName()
140
    {
141
        if ($this->name) return $this->name;
142
143
        foreach ($this->schema->models as $modelName => $model) {
144
            if ($this->isCurrent($modelName)) {
145
                $this->setCurrent($modelName);
146
147
                return $this->name = $modelName;
148
            }
149
        }
150
151
        return null;
152
    }
153
154
    public function prepareCurrentModel()
155
    {
156
        $this->name = $this->getCurrentModelName();
157
        $this->class = $this->schema->getClass($this->name);
158
        $this->model = $this->schema->getInstance($this->name);
0 ignored issues
show
Bug introduced by
It seems like $this->name can also be of type integer or string; however, Mascame\Artificer\Model\ModelSchema::getInstance() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
159
        $this->table = $this->model->getTable();
160
        $this->columns = $this->schema->getColumns($this->table);
161
        $this->fillable = $this->model->getFillable();
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    private function getCurrentModelData()
168
    {
169
        return array(
170
            'class' => $this->class,
171
            'name' => $this->getCurrentModelName(),
172
            'route' => $this->getRouteName(),
173
            'table' => $this->table,
174
            'columns' => $this->schema->columns,
175
            'fillable' => $this->fillable,
176
            'hidden' => $this->isHidden($this->name),
177
        );
178
    }
179
180
    /**
181
     * @param $model
182
     * @return bool
183
     */
184
    protected function isCurrent($modelName)
185
    {
186
        $slug = Route::current()->parameter('slug');
187
188
        return (isset($this->schema->models[$modelName]['route']) && $this->schema->models[$modelName]['route'] == $slug);
189
    }
190
191
    /**
192
     * @return Model
193
     */
194
    public static function getCurrent()
195
    {
196
        return (isset(self::$current)) ? self::$current : null;
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    public static function getCurrentClass()
203
    {
204
        return '\\' . self::$current;
205
    }
206
207
    /**
208
     * @param null $model
209
     * @return null
210
     */
211
    public function getRouteName($model = null)
212
    {
213
        if ($model) return $this->schema->models[$model]['route'];
214
215
        return (isset($this->schema->models[self::$current]['route'])) ? $this->schema->models[self::$current]['route'] : null;
216
    }
217
218
    /**
219
     * @param $modelName
220
     */
221
    protected function setCurrent($modelName)
222
    {
223
        self::$current = $modelName;
224
//        ModelOption::set('current', $modelName);
225
    }
226
227
    /**
228
     * @param null $model
229
     * @return mixed
230
     */
231
    public function getOptions($model = null)
232
    {
233
        $model = ($model) ? $model : $this->name;
234
235
        if (isset($this->options[$model])) return $this->options[$model];
236
237
        return $this->options[$model] = config('admin.models.' . $model);
238
    }
239
240
    public function getDefaultOptions()
241
    {
242
        if ($this->defaultOptions) return $this->defaultOptions;
243
244
        return $this->defaultOptions = config('admin.model.default_model');
245
    }
246
247
    /**
248
     * @param $key
249
     * @param null $model
250
     * @return mixed
251
     */
252
    public function getOption($key, $default = null, $model = null)
253
    {
254
        $model = ($model) ? $model : $this->name;
255
        $options = $this->getOptions($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $model ? $model : $this->name on line 254 can also be of type integer or string; however, Mascame\Artificer\Model\Model::getOptions() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
256
257
        return (isset($options[$key])) ? $options[$key] : $default;
258
    }
259
260
    /**
261
     * @return array|mixed
262
     */
263
    public function getRelations()
264
    {
265
        return $this->relations->get();
266
    }
267
268
}