Completed
Push — master ( 650783...0c9838 )
by wen
15:06
created

Component::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Display\DisplayInterface;
16
use Sco\Admin\Contracts\WithNavigation;
17
18
abstract class Component implements ComponentInterface, WithNavigation
19
{
20
    use HasAccess, HasEvents, HasNavigation;
21
22
    /**
23
     * @var
24
     */
25
    protected $name;
26
27
    /**
28
     * @var \Illuminate\Foundation\Application
29
     */
30
    protected $app;
31
32
    /**
33
     * The component display name
34
     *
35
     * @var string
36
     */
37
    protected $title;
38
39
    /**
40
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
41
     */
42
    protected $repository;
43
44
    /**
45
     * @var \Illuminate\Database\Eloquent\Model
46
     */
47
    protected $model;
48
49
    protected static $booted = [];
50
51
    /**
52
     * @var \Illuminate\Contracts\Events\Dispatcher
53
     */
54
    protected static $dispatcher;
55
56
    abstract public function model();
57
58
    public function __construct(Application $app, RepositoryInterface $repository)
59
    {
60
        $this->app = $app;
61
62
        $this->makeModel();
63
64
        $this->repository = $repository;
65
        $this->repository->setModel($this->getModel());
66
67
        if (! $this->name) {
68
            $this->setDefaultName();
69
        }
70
71
        $this->bootIfNotBooted();
72
    }
73
74
    protected function setDefaultName()
75
    {
76
        $this->name = $this->getModelClassName();
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function getModelClassName()
83
    {
84
        return snake_case( // 蛇形命名
85
            str_plural( // 复数
86
                class_basename(
87
                    get_class($this->getModel())
88
                )
89
            )
90
        );
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getModel()
97
    {
98
        return $this->model;
99
    }
100
101
    /**
102
     * @return \Illuminate\Database\Eloquent\Model|mixed
103
     */
104
    protected function makeModel()
105
    {
106
        $class = $this->model();
107
        if (empty($class)) {
108
            throw new InvalidArgumentException(
109
                sprintf(
110
                    'The component(%s) method "model()" not found value',
111
                    get_class($this)
112
                )
113
            );
114
        }
115
116
        $model = $this->app->make($this->model());
117
118
        if (! ($model instanceof Model)) {
119
            throw new InvalidArgumentException(
120
                sprintf(
121
                    "Class %s must be an instance of %s",
122
                    $this->model(),
123
                    Model::class
124
                )
125
            );
126
        }
127
128
        return $this->model = $model;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getName()
135
    {
136
        return $this->name;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getTitle()
143
    {
144
        return $this->title;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getRepository()
151
    {
152
        return $this->repository;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getConfigs()
159
    {
160
        return collect([
161
            'title'    => $this->getTitle(),
162
            'accesses' => $this->getAccesses(),
163
            'display'  => $this->fireDisplay(),
164
        ]);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 View Code Duplication
    final public function fireDisplay()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        if (! method_exists($this, 'callDisplay')) {
173
            throw new BadMethodCallException('Not Found Method "callDisplay"');
174
        }
175
176
        $display = $this->app->call([$this, 'callDisplay']);
177
178
        if (! $display instanceof DisplayInterface) {
179
            throw new InvalidArgumentException(
180
                sprintf(
181
                    'callDisplay must be instanced of "%s".',
182
                    DisplayInterface::class
183
                )
184
            );
185
        }
186
187
        $display->setModel($this->getModel());
188
        $display->initialize();
189
190
        return $display;
191
    }
192
193
    public function get()
194
    {
195
        $display = $this->fireDisplay();
196
197
        return $display->get();
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 View Code Duplication
    final public function fireCreate()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        if (! method_exists($this, 'callCreate')) {
206
            return;
207
        }
208
209
        $form = $this->app->call([$this, 'callCreate']);
210
        if (! $form instanceof FormInterface) {
211
            throw new InvalidArgumentException(
212
                sprintf(
213
                    'callCreate must be instanced of "%s".',
214
                    FormInterface::class
215
                )
216
            );
217
        }
218
219
        $form->setModel($this->getModel());
220
221
        return $form;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function store()
228
    {
229
        $form = $this->fireCreate();
230
231
        $form->validate()->save();
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    final public function fireEdit($id)
238
    {
239
        if (! method_exists($this, 'callEdit')) {
240
            return;
241
        }
242
243
        $form = $this->app->call([$this, 'callEdit'], ['id' => $id]);
244
245
        if (! $form instanceof FormInterface) {
246
            throw new InvalidArgumentException(
247
                sprintf(
248
                    'callEdit must be instanced of "%s".',
249
                    FormInterface::class
250
                )
251
            );
252
        }
253
254
        $model = $this->getRepository()->findOrFail($id);
255
256
        $form->setModel($model);
257
258
        return $form;
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function update($id)
265
    {
266
        $form = $this->fireEdit($id);
267
        $form->validate()->save();
268
    }
269
270
    public function delete($id)
271
    {
272
        $this->getRepository()->delete($id);
273
274
        return true;
275
    }
276
277
    public function forceDelete($id)
278
    {
279
        $this->getRepository()->forceDelete($id);
280
281
        return true;
282
    }
283
284
    public function restore($id)
285
    {
286
        $this->getRepository()->restore($id);
287
288
        return true;
289
    }
290
291
    protected function bootIfNotBooted()
292
    {
293
        if (! isset(static::$booted[static::class])) {
294
            static::$booted[static::class] = true;
295
296
            $this->fireEvent('booting', false);
297
298
            $this->boot();
299
300
            $this->fireEvent('booted', false);
301
        }
302
    }
303
304
    /**
305
     * The "booting" method of the model.
306
     *
307
     * @return void
308
     */
309
    protected function boot()
310
    {
311
        $this->bootTraits();
312
    }
313
314
    /**
315
     * Boot all of the bootable traits on the model.
316
     *
317
     * @return void
318
     */
319
    protected function bootTraits()
320
    {
321
        foreach (class_uses_recursive($this) as $trait) {
322
            if (method_exists($this, $method = 'boot' . class_basename($trait))) {
323
                $this->$method();
324
            }
325
        }
326
    }
327
}
328