Completed
Push — master ( 93b4ca...ada3e7 )
by wen
10:31
created

Component::makeModel()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
nc 3
cc 3
eloc 15
nop 0
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use BadMethodCallException;
6
use Illuminate\Database\Eloquent\Model;
7
use InvalidArgumentException;
8
use Illuminate\Foundation\Application;
9
use Sco\Admin\Component\Concerns\HasAccess;
10
use Sco\Admin\Component\Concerns\HasEvents;
11
use Sco\Admin\Component\Concerns\HasNavigation;
12
use Sco\Admin\Contracts\ComponentInterface;
13
use Sco\Admin\Contracts\Form\FormInterface;
14
use Sco\Admin\Contracts\RepositoryInterface;
15
use Sco\Admin\Contracts\View\ViewInterface;
16
use Sco\Admin\Contracts\WithNavigation;
17
18
abstract class Component implements
19
    ComponentInterface,
20
    WithNavigation
21
{
22
    use HasAccess, HasEvents, HasNavigation;
23
24
    /**
25
     * @var
26
     */
27
    protected $name;
28
29
    /**
30
     * @var \Illuminate\Foundation\Application
31
     */
32
    protected $app;
33
34
    /**
35
     * The component display name
36
     *
37
     * @var string
38
     */
39
    protected $title;
40
41
    /**
42
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
43
     */
44
    protected $repository;
45
46
    /**
47
     * @var \Illuminate\Database\Eloquent\Model
48
     */
49
    protected $model;
50
51
    protected static $booted = [];
52
53
    /**
54
     * @var \Illuminate\Contracts\Events\Dispatcher
55
     */
56
    protected static $dispatcher;
57
58
    abstract public function model();
59
60
    public function __construct(Application $app, RepositoryInterface $repository)
61
    {
62
        $this->app        = $app;
63
        $this->repository = $repository;
64
65
        $this->makeModel();
66
67
        $this->repository->setModel($this->getModel());
68
69
        if (!$this->name) {
70
            $this->setDefaultName();
71
        }
72
73
        $this->bootIfNotBooted();
74
    }
75
76
    protected function setDefaultName()
77
    {
78
        $this->name = $this->getModelClassName();
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    protected function getModelClassName()
85
    {
86
        return snake_case( // 蛇形命名
87
            str_plural( // 复数
88
                class_basename(
89
                    get_class($this->getModel())
90
                )
91
            )
92
        );
93
    }
94
95
    protected function makeModel()
96
    {
97
        $class = $this->model();
98
        if (empty($class)) {
99
            throw new InvalidArgumentException(
100
                sprintf(
101
                    'The component(%s) method "model()" not found value',
102
                    get_class($this)
103
                )
104
            );
105
        }
106
107
        $model = $this->app->make($this->model());
108
109
        if (!($model instanceof Model)) {
110
            throw new InvalidArgumentException(
111
                sprintf(
112
                    "Class %s must be an instance of %s",
113
                    $this->model(),
114
                    Model::class
115
                )
116
            );
117
        }
118
119
        return $this->model = $model;
120
    }
121
122
    public function getName()
123
    {
124
        return $this->name;
125
    }
126
127
    public function getTitle()
128
    {
129
        return $this->title;
130
    }
131
132
    public function getModel()
133
    {
134
        return $this->model;
135
    }
136
137
    public function getRepository()
138
    {
139
        return $this->repository;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getConfigs()
146
    {
147
        return collect([
148
            //'primaryKey'  => $this->getModel()->getKeyName(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
            'title'    => $this->getTitle(),
150
            'accesses' => $this->getAccesses(),
151
            'view'     => $this->fireView(),
152
            //'elements'    => $this->getElements()->values(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
        ]);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function fireView()
160
    {
161
        if (!method_exists($this, 'callView')) {
162
            throw new BadMethodCallException('Not Found Method "callView"');
163
        }
164
165
        $view = $this->app->call([$this, 'callView']);
166
167
        if (!$view instanceof ViewInterface) {
168
            throw new InvalidArgumentException(
169
                sprintf(
170
                    'callView must be instanced of "%s".',
171
                    ViewInterface::class
172
                )
173
            );
174
        }
175
176
        return $view;
177
    }
178
179
    public function get()
180
    {
181
        $view = $this->fireView();
182
183
        $view->setRepository($this->getRepository());
184
185
        return $view->get();
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function fireCreate()
192
    {
193
        if (!method_exists($this, 'callCreate')) {
194
            return;
195
        }
196
197
        $form = $this->app->call([$this, 'callCreate']);
198
        if (!$form instanceof FormInterface) {
199
            throw new InvalidArgumentException(
200
                sprintf(
201
                    'callCreate must be instanced of "%s".',
202
                    FormInterface::class
203
                )
204
            );
205
        }
206
207
        $form->setModel($this->getModel());
208
209
        return $form;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function store()
216
    {
217
        $form = $this->fireCreate();
218
219
        $form->validate()->save();
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function fireEdit($id)
226
    {
227
        if (!method_exists($this, 'callEdit')) {
228
            return;
229
        }
230
231
        $form = $this->app->call([$this, 'callEdit'], ['id' => $id]);
232
233
        if (!$form instanceof FormInterface) {
234
            throw new InvalidArgumentException(
235
                sprintf(
236
                    'callEdit must be instanced of "%s".',
237
                    FormInterface::class
238
                )
239
            );
240
        }
241
242
        $model = $this->getRepository()->findOrFail($id);
243
244
        $form->setModel($model);
245
246
        return $form;
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function update($id)
253
    {
254
        $form = $this->fireEdit($id);
255
        $form->validate()->save();
256
    }
257
258
    public function delete($id)
259
    {
260
        $this->getRepository()->findOrFail($id)->delete();
261
        return true;
262
    }
263
264
    public function forceDelete($id)
265
    {
266
        $this->getRepository()->forceDelete($id);
267
        return true;
268
    }
269
270
    public function restore($id)
271
    {
272
        $this->getRepository()->restore($id);
273
        return true;
274
    }
275
276
    protected function bootIfNotBooted()
277
    {
278
        if (!isset(static::$booted[static::class])) {
279
            static::$booted[static::class] = true;
280
281
            $this->fireEvent('booting', false);
282
283
            $this->boot();
284
285
            $this->fireEvent('booted', false);
286
        }
287
    }
288
289
    /**
290
     * The "booting" method of the model.
291
     *
292
     * @return void
293
     */
294
    protected function boot()
295
    {
296
        $this->bootTraits();
297
    }
298
299
    /**
300
     * Boot all of the bootable traits on the model.
301
     *
302
     * @return void
303
     */
304
    protected function bootTraits()
305
    {
306
        foreach (class_uses_recursive($this) as $trait) {
307
            if (method_exists($this, $method = 'boot' . class_basename($trait))) {
308
                $this->$method();
309
            }
310
        }
311
    }
312
}
313