Completed
Push — master ( 76b9d6...f2a606 )
by Terzi
04:33
created

Scaffolding::readonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
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;
6
use Terranet\Administrator\Contracts\AutoTranslatable;
7
use Terranet\Administrator\Contracts\Module;
8
use Terranet\Administrator\Contracts\Services\TemplateProvider;
9
use Terranet\Administrator\Services\Breadcrumbs;
10
use Terranet\Administrator\Services\CrudActions;
11
use Terranet\Administrator\Services\Finder;
12
use Terranet\Administrator\Services\Saver;
13
use Terranet\Administrator\Services\Template;
14
use Terranet\Administrator\Traits\AutoTranslatesInstances;
15
use Terranet\Administrator\Traits\Module\AllowsNavigation;
16
use Terranet\Administrator\Traits\Module\HasColumns;
17
18
class Scaffolding implements Module, AutoTranslatable
19
{
20
    use AllowsNavigation, 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 Breadcrumbs
37
     */
38
    protected $breadcrumbs = Breadcrumbs::class;
39
40
    /**
41
     * Service layer responsible for searching items.
42
     *
43
     * @var \Terranet\Administrator\Contracts\Services\Finder
44
     */
45
    protected $finder = Finder::class;
46
47
    /**
48
     * Service layer responsible for persisting request.
49
     *
50
     * @var \Terranet\Administrator\Contracts\Services\Saver
51
     */
52
    protected $saver = Saver::class;
53
54
    /**
55
     * Actions manager class.
56
     *
57
     * @var \Terranet\Administrator\Contracts\Services\CrudActions
58
     */
59
    protected $actions = CrudActions::class;
60
61
    /**
62
     * View templates provider.
63
     *
64
     * @var TemplateProvider
65
     */
66
    protected $template = Template::class;
67
68
    /**
69
     * Include or not columns of Date type in index listing.
70
     *
71
     * @var bool
72
     */
73
    protected $includeDateColumns;
74
75
    /**
76
     * Global ACL Manager.
77
     *
78
     * @var mixed null
79
     */
80
    protected $guard;
81
82
    /** @var array */
83
    protected static $methods = [];
84
85
    /**
86
     * Scaffolding constructor.
87
     */
88
    public function __construct()
89
    {
90
        if (null === $this->includeDateColumns) {
91
            $this->includeDateColumns = $this->defaultIncludeDateColumnsValue();
92
        }
93
    }
94
95
    /**
96
     * @param $method
97
     * @param $arguments
98
     *
99
     * @return null|mixed
100
     */
101
    public function __call($method, $arguments)
102
    {
103
        // Call user-defined method if exists.
104
        if ($closure = array_get(static::$methods, $method)) {
105
            return \call_user_func_array($closure, $arguments);
106
        }
107
108
        return null;
109
    }
110
111
    /**
112
     * Extend functionality by adding new methods.
113
     *
114
     * @param $name
115
     * @param $closure
116
     *
117
     * @throws Exception
118
     */
119
    public static function addMethod($name, $closure)
120
    {
121
        if (!(new static())->hasMethod($name)) {
122
            static::$methods[$name] = $closure;
123
        }
124
    }
125
126
    /**
127
     * Disable Actions column totally for Readonly Resources.
128
     *
129
     * @return bool
130
     */
131
    public function readonly()
132
    {
133
        return false;
134
    }
135
136
    /**
137
     * Check if method exists.
138
     *
139
     * @param $name
140
     *
141
     * @return bool
142
     */
143
    public function hasMethod($name)
144
    {
145
        return method_exists($this, $name) || array_has(static::$methods, $name);
146
    }
147
148
    /**
149
     * The module Templates manager.
150
     *
151
     * @return string
152
     */
153
    public function template()
154
    {
155
        if (class_exists($file = $this->getQualifiedClassNameOfType('Templates'))) {
156
            return $file;
157
        }
158
159
        return $this->template;
160
    }
161
162
    /**
163
     * @return Model
164
     */
165
    public function model()
166
    {
167
        static $model = null;
168
169
        if (null === $model && ($class = $this->getModelClass())) {
170
            $model = new $class();
171
        }
172
173
        return $model;
174
    }
175
176
    /**
177
     * @param $model
178
     *
179
     * @return $this
180
     */
181
    public function setModel($model)
182
    {
183
        $this->model = $model;
184
185
        return $this;
186
    }
187
188
    /**
189
     * Define the class responsive for fetching items.
190
     *
191
     * @return mixed
192
     */
193
    public function finder()
194
    {
195
        if (class_exists($file = $this->getQualifiedClassNameOfType('Finders'))) {
196
            return $file;
197
        }
198
199
        return $this->finder;
200
    }
201
202
    /**
203
     * Define the class responsive for persisting items.
204
     *
205
     * @return mixed
206
     */
207
    public function saver()
208
    {
209
        if (class_exists($file = $this->getQualifiedClassNameOfType('Savers'))) {
210
            return $file;
211
        }
212
213
        return $this->saver;
214
    }
215
216
    /**
217
     * Breadcrumbs provider
218
     * First parse Module doc block for provider declaration.
219
     *
220
     * @return mixed
221
     */
222
    public function breadcrumbs()
223
    {
224
        if (class_exists($file = $this->getQualifiedClassNameOfType('Breadcrumbs'))) {
225
            return $file;
226
        }
227
228
        return $this->breadcrumbs;
229
    }
230
231
    /**
232
     * Define the Actions provider - object responsive for
233
     * CRUD operations, Export, etc...
234
     * as like as checks action permissions.
235
     *
236
     * @return mixed
237
     */
238
    public function actions()
239
    {
240
        if (class_exists($file = $this->getQualifiedClassNameOfType('Actions'))) {
241
            return $file;
242
        }
243
244
        return $this->actions;
245
    }
246
247
    /**
248
     * @throws Exception
249
     *
250
     * @return ActionsManager
251
     */
252
    public function actionsManager()
253
    {
254
        $handler = $this->actions();
255
        $handler = new $handler($this);
256
257
        if (!$handler instanceof CrudActions) {
258
            throw new Exception('Actions handler must implement '.CrudActions::class.' contract');
259
        }
260
261
        return new ActionsManager($handler, $this);
262
    }
263
264
    /**
265
     * The module Eloquent model.
266
     *
267
     * @throws \Exception
268
     *
269
     * @return mixed
270
     */
271
    protected function getModelClass()
272
    {
273
        return $this->model;
274
    }
275
276
    /**
277
     * Get the full path to class of special type.
278
     *
279
     * @param $type
280
     *
281
     * @return string
282
     */
283
    protected function getQualifiedClassNameOfType($type)
284
    {
285
        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

285
        return app()->/** @scrutinizer ignore-call */ getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this);
Loading history...
286
    }
287
288
    /**
289
     * @return mixed
290
     */
291
    protected function defaultIncludeDateColumnsValue()
292
    {
293
        return config(
294
            "administrator.grid.timestamps.{$this->url()}",
295
            config('administrator.grid.timestamps.enabled')
296
        );
297
    }
298
}
299