Completed
Push — master ( 062b68...81f5ad )
by Terzi
08:48
created

Scaffolding::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Terranet\Administrator\Contracts\AutoTranslatable;
7
use Terranet\Administrator\Contracts\Module;
8
use Terranet\Administrator\Services\Breadcrumbs;
9
use Terranet\Administrator\Services\CrudActions;
10
use Terranet\Administrator\Services\Finder;
11
use Terranet\Administrator\Services\Saver;
12
use Terranet\Administrator\Services\Template;
13
use Terranet\Administrator\Traits\AutoTranslatesInstances;
14
use Terranet\Administrator\Traits\Module\AllowsNavigation;
15
use Terranet\Administrator\Traits\Module\DetectsCommentFlag;
16
use Terranet\Administrator\Traits\Module\HasColumns;
17
18
class Scaffolding implements Module, AutoTranslatable
19
{
20
    use AllowsNavigation, DetectsCommentFlag, HasColumns, AutoTranslatesInstances;
21
22
    const PAGE_INDEX = 'index';
23
    const PAGE_VIEW = 'view';
24
    const PAGE_EDIT = 'edit';
25
26
    /**
27
     * The module Eloquent model class.
28
     *
29
     * @return string
30
     */
31
    protected $model;
32
33
    /**
34
     * Breadcrumbs provider.
35
     *
36
     * @var
37
     */
38
    protected $breadcrumbs = Breadcrumbs::class;
39
40
    /**
41
     * Service layer responsible for searching items.
42
     *
43
     * @var
44
     */
45
    protected $finder = Finder::class;
46
47
    /**
48
     * Service layer responsible for persisting request.
49
     *
50
     * @var
51
     */
52
    protected $saver = Saver::class;
53
54
    /**
55
     * Actions manager class.
56
     *
57
     * @var
58
     */
59
    protected $actions = CrudActions::class;
60
61
    /**
62
     * View templates provider.
63
     *
64
     * @var
65
     */
66
    protected $template = Template::class;
67
68
    /**
69
     * Query string parameters that should be appended to whole links and forms.
70
     *
71
     * @var array
72
     */
73
    protected $magnetParams = [];
74
75
    /**
76
     * Include or not columns of Date type in index listing.
77
     *
78
     * @var bool
79
     */
80
    protected $includeDateColumns;
81
82
    /**
83
     * Global ACL Manager.
84
     *
85
     * @var mixed null
86
     */
87
    protected $guard;
88
89
    protected static $methods = [];
90
91
    public function __construct()
92
    {
93
        if (null === $this->includeDateColumns) {
94
            $this->includeDateColumns = $this->defaultIncludeDateColumnsValue();
95
        }
96
    }
97
98
    public function __call($method, $arguments)
99
    {
100
        // Call user-defined method if exists.
101
        if ($closure = array_get(static::$methods, $method)) {
102
            return call_user_func_array($closure, $arguments);
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * Extend functionality by adding new methods.
110
     *
111
     * @param $name
112
     * @param $closure
113
     *
114
     * @throws Exception
115
     */
116
    public static function addMethod($name, $closure)
117
    {
118
        if (!(new static())->hasMethod($name)) {
119
            static::$methods[$name] = $closure;
120
        }
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function guard()
127
    {
128
        // set the default guard
129
        if (null === $this->guard) {
130
            $this->guard = config('administrator.acl.manager');
131
        }
132
133
        if (is_string($this->guard)) {
134
            $this->guard = new $this->guard($this);
135
        }
136
137
        return $this->guard;
138
    }
139
140
    /**
141
     * Disable Actions column totally for Readonly Resources.
142
     *
143
     * @return bool
144
     */
145
    public function hasActions()
146
    {
147
        return true;
148
    }
149
150
    /**
151
     * Check if method exists.
152
     *
153
     * @param $name
154
     *
155
     * @return bool
156
     */
157
    public function hasMethod($name)
158
    {
159
        return method_exists($this, $name) || array_has(static::$methods, $name);
160
    }
161
162
    /**
163
     * The module Templates manager.
164
     *
165
     * @return string
166
     */
167
    public function template()
168
    {
169
        if (class_exists($file = $this->getQualifiedClassNameOfType('Templates'))) {
170
            return $file;
171
        }
172
173
        if ([/*$flag*/, $value] = $this->hasCommentFlag('template')) {
174
            return $value;
175
        }
176
177
        return $this->template;
178
    }
179
180
    /**
181
     * @return Model
182
     */
183
    public function model()
184
    {
185
        static $model = null;
186
187
        if (null === $model && ($class = $this->getModelClass())) {
188
            $model = new $class();
189
        }
190
191
        return $model;
192
    }
193
194
    /**
195
     * @param $model
196
     * @return $this
197
     */
198
    public function setModel($model)
199
    {
200
        $this->model = $model;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Define the class responsive for fetching items.
207
     *
208
     * @return mixed
209
     */
210
    public function finder()
211
    {
212
        if (class_exists($file = $this->getQualifiedClassNameOfType('Finders'))) {
213
            return $file;
214
        }
215
216
        if ([/*$flag*/, $value] = $this->hasCommentFlag('finder')) {
217
            return $value;
218
        }
219
220
        return $this->finder;
221
    }
222
223
    /**
224
     * Define the class responsive for persisting items.
225
     *
226
     * @return mixed
227
     */
228
    public function saver()
229
    {
230
        if (class_exists($file = $this->getQualifiedClassNameOfType('Savers'))) {
231
            return $file;
232
        }
233
234
        if ([/*$flag*/, $saver] = $this->hasCommentFlag('saver')) {
235
            return $saver;
236
        }
237
238
        return $this->saver;
239
    }
240
241
    /**
242
     * Breadcrumbs provider
243
     * First parse Module doc block for provider declaration.
244
     *
245
     * @return mixed
246
     */
247
    public function breadcrumbs()
248
    {
249
        if (class_exists($file = $this->getQualifiedClassNameOfType('Breadcrumbs'))) {
250
            return $file;
251
        }
252
253
        if ([/*$flag*/, $value] = $this->hasCommentFlag('breadcrumbs')) {
254
            return $value;
255
        }
256
257
        return $this->breadcrumbs;
258
    }
259
260
    /**
261
     * Define the Actions provider - object responsive for
262
     * CRUD operations, Export, etc...
263
     * as like as checks action permissions.
264
     *
265
     * @return mixed
266
     */
267
    public function actions()
268
    {
269
        if (class_exists($file = $this->getQualifiedClassNameOfType('Actions'))) {
270
            return $file;
271
        }
272
273
        if ([/*$flag*/, $value] = $this->hasCommentFlag('actions')) {
274
            return $value;
275
        }
276
277
        return $this->actions;
278
    }
279
280
    /**
281
     * @throws Exception
282
     *
283
     * @return ActionsManager
284
     */
285
    public function actionsManager()
286
    {
287
        $handler = $this->actions();
288
        $handler = new $handler($this);
289
290
        if (!$handler instanceof CrudActions) {
291
            throw new Exception('Actions handler must implement '.CrudActions::class.' contract');
292
        }
293
294
        return new ActionsManager($handler, $this);
295
    }
296
297
    /**
298
     * Return magnet params.
299
     *
300
     * @return array
301
     */
302
    public function magnetParams()
303
    {
304
        return (array) $this->magnetParams;
305
    }
306
307
    /**
308
     * The module Eloquent model.
309
     *
310
     * @throws \Exception
311
     *
312
     * @return mixed
313
     */
314
    protected function getModelClass()
315
    {
316
        if ([/*$flag*/, $value] = $this->hasCommentFlag('model')) {
317
            return $value;
318
        }
319
320
        return $this->model;
321
    }
322
323
    /**
324
     * Get the full path to class of special type.
325
     *
326
     * @param $type
327
     *
328
     * @return string
329
     */
330
    protected function getQualifiedClassNameOfType($type)
331
    {
332
        return app()->getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this);
0 ignored issues
show
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

332
        return app()->/** @scrutinizer ignore-call */ getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this);
Loading history...
333
    }
334
335
    /**
336
     * @return mixed
337
     */
338
    protected function defaultIncludeDateColumnsValue()
339
    {
340
        return config(
341
            "administrator.grid.timestamps.{$this->url()}",
342
            config('administrator.grid.timestamps.enabled')
343
        );
344
    }
345
}
346