Completed
Push — master ( 2ee94f...e70fcb )
by Terzi
06:41
created

Scaffolding::finderInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Terranet\Administrator\Contracts\ActionsManager as ActionsManagerContract;
7
use Terranet\Administrator\Contracts\AutoTranslatable;
8
use Terranet\Administrator\Contracts\Module;
9
use Terranet\Administrator\Contracts\Services\Finder as FinderContract;
10
use Terranet\Administrator\Contracts\Services\TemplateProvider;
11
use Terranet\Administrator\Services\Breadcrumbs;
12
use Terranet\Administrator\Services\CrudActions;
13
use Terranet\Administrator\Services\Finder;
14
use Terranet\Administrator\Services\Saver;
15
use Terranet\Administrator\Services\Template;
16
use Terranet\Administrator\Traits\AutoTranslatesInstances;
17
use Terranet\Administrator\Traits\Module\AllowsNavigation;
18
use Terranet\Administrator\Traits\Module\HasColumns;
19
20
class Scaffolding implements Module, AutoTranslatable
21
{
22
    use AllowsNavigation, HasColumns, AutoTranslatesInstances;
23
24
    const PAGE_INDEX = 'index';
25
    const PAGE_VIEW = 'view';
26
    const PAGE_EDIT = 'edit';
27
28
    /**
29
     * The module Eloquent model class.
30
     *
31
     * @return string
32
     */
33
    protected $model;
34
35
    /**
36
     * Breadcrumbs provider.
37
     *
38
     * @var Breadcrumbs
39
     */
40
    protected $breadcrumbs = Breadcrumbs::class;
41
42
    /**
43
     * Service layer responsible for searching items.
44
     *
45
     * @var FinderContract
46
     */
47
    protected $finder = Finder::class;
48
49
    /**
50
     * Service layer responsible for persisting request.
51
     *
52
     * @var \Terranet\Administrator\Contracts\Services\Saver
53
     */
54
    protected $saver = Saver::class;
55
56
    /**
57
     * Actions manager class.
58
     *
59
     * @var \Terranet\Administrator\Contracts\Services\CrudActions
60
     */
61
    protected $actions = CrudActions::class;
62
63
    /**
64
     * View templates provider.
65
     *
66
     * @var TemplateProvider
67
     */
68
    protected $template = Template::class;
69
70
    /**
71
     * Include or not columns of Date type in index listing.
72
     *
73
     * @var bool
74
     */
75
    protected $includeDateColumns;
76
77
    /**
78
     * Global ACL Manager.
79
     *
80
     * @var mixed null
81
     */
82
    protected $guard;
83
84
    /** @var array */
85
    protected static $methods = [];
86
87
    /**
88
     * Scaffolding constructor.
89
     */
90
    public function __construct()
91
    {
92
        if (null === $this->includeDateColumns) {
93
            $this->includeDateColumns = $this->defaultIncludeDateColumnsValue();
94
        }
95
    }
96
97
    /**
98
     * @param $method
99
     * @param $arguments
100
     *
101
     * @return null|mixed
102
     */
103
    public function __call($method, $arguments)
104
    {
105
        // Call user-defined method if exists.
106
        if ($closure = array_get(static::$methods, $method)) {
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

106
        if ($closure = /** @scrutinizer ignore-deprecated */ array_get(static::$methods, $method)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107
            return \call_user_func_array($closure, $arguments);
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * Extend functionality by adding new methods.
115
     *
116
     * @param $name
117
     * @param $closure
118
     *
119
     * @throws Exception
120
     */
121
    public static function addMethod($name, $closure)
122
    {
123
        if (!(new static())->hasMethod($name)) {
124
            static::$methods[$name] = $closure;
125
        }
126
    }
127
128
    /**
129
     * Disable Actions column totally for Readonly Resources.
130
     *
131
     * @return bool
132
     */
133
    public function readonly()
134
    {
135
        return false;
136
    }
137
138
    /**
139
     * Check if method exists.
140
     *
141
     * @param $name
142
     *
143
     * @return bool
144
     */
145
    public function hasMethod($name)
146
    {
147
        return method_exists($this, $name) || array_has(static::$methods, $name);
0 ignored issues
show
Deprecated Code introduced by
The function array_has() has been deprecated: Arr::has() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

147
        return method_exists($this, $name) || /** @scrutinizer ignore-deprecated */ array_has(static::$methods, $name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
148
    }
149
150
    /**
151
     * The module Templates manager.
152
     *
153
     * @return string
154
     */
155
    public function template()
156
    {
157
        if (class_exists($file = $this->getQualifiedClassNameOfType('Templates'))) {
158
            return $file;
159
        }
160
161
        return $this->template;
162
    }
163
164
    /**
165
     * @return Model
166
     */
167
    public function model()
168
    {
169
        static $model = null;
170
171
        if (null === $model && ($class = $this->getModelClass())) {
172
            $model = new $class();
173
        }
174
175
        return $model;
176
    }
177
178
    /**
179
     * @param $model
180
     *
181
     * @return $this
182
     */
183
    public function setModel($model)
184
    {
185
        $this->model = $model;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Define the class responsive for fetching items.
192
     *
193
     * @return mixed
194
     */
195
    public function finder()
196
    {
197
        if (class_exists($file = $this->getQualifiedClassNameOfType('Finders'))) {
198
            return $file;
199
        }
200
201
        return $this->finder;
202
    }
203
204
    /**
205
     * @return mixed
206
     */
207
    public function finderInstance(): FinderContract
208
    {
209
        $className = $this->finder();
210
211
        return once(function () use ($className) {
212
//            $this->columns();
213
214
            return new $className($this);
215
        });
216
    }
217
218
    /**
219
     * Define the class responsive for persisting items.
220
     *
221
     * @return mixed
222
     */
223
    public function saver()
224
    {
225
        if (class_exists($file = $this->getQualifiedClassNameOfType('Savers'))) {
226
            return $file;
227
        }
228
229
        return $this->saver;
230
    }
231
232
    /**
233
     * Breadcrumbs provider
234
     * First parse Module doc block for provider declaration.
235
     *
236
     * @return mixed
237
     */
238
    public function breadcrumbs()
239
    {
240
        if (class_exists($file = $this->getQualifiedClassNameOfType('Breadcrumbs'))) {
241
            return $file;
242
        }
243
244
        return $this->breadcrumbs;
245
    }
246
247
    /**
248
     * Define the Actions provider - object responsive for
249
     * CRUD operations, Export, etc...
250
     * as like as checks action permissions.
251
     *
252
     * @return mixed
253
     */
254
    public function actions()
255
    {
256
        if (class_exists($file = $this->getQualifiedClassNameOfType('Actions'))) {
257
            return $file;
258
        }
259
260
        return $this->actions;
261
    }
262
263
    /**
264
     * @return ActionsManager
265
     * @throws Exception
266
     *
267
     */
268
    public function actionsManager(): ActionsManagerContract
269
    {
270
        $handler = $this->actions();
271
        $handler = new $handler($this);
272
273
        if (!$handler instanceof CrudActions) {
274
            throw new Exception('Actions handler must implement '.CrudActions::class.' contract');
275
        }
276
277
        return new ActionsManager($handler, $this);
278
    }
279
280
    /**
281
     * The module Eloquent model.
282
     *
283
     * @return mixed
284
     * @throws \Exception
285
     *
286
     */
287
    protected function getModelClass()
288
    {
289
        return $this->model;
290
    }
291
292
    /**
293
     * Get the full path to class of special type.
294
     *
295
     * @param $type
296
     *
297
     * @return string
298
     */
299
    protected function getQualifiedClassNameOfType($type)
300
    {
301
        return app()->getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

301
        return /** @scrutinizer ignore-call */ app()->getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this);
Loading history...
302
    }
303
304
    /**
305
     * @return mixed
306
     */
307
    protected function defaultIncludeDateColumnsValue()
308
    {
309
        return config(
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

309
        return /** @scrutinizer ignore-call */ config(
Loading history...
310
            "administrator.grid.timestamps.{$this->url()}",
311
            config('administrator.grid.timestamps.enabled')
312
        );
313
    }
314
}
315