Completed
Push — master ( d781ee...2f5f2b )
by wen
26:40
created

Component   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 298
Duplicated Lines 14.09 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 7
dl 42
loc 298
rs 9.3999
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A setDefaultName() 0 4 1
model() 0 1 ?
A __construct() 0 15 2
A getModelClassName() 0 10 1
A getRepository() 0 4 1
A getConfigs() 0 10 1
B makeModel() 0 26 3
A getName() 0 4 1
A getTitle() 0 4 1
A fireView() 22 22 3
A get() 0 8 1
A fireCreate() 20 20 3
A store() 0 6 1
A fireEdit() 0 23 3
A update() 0 5 1
A delete() 0 5 1
A forceDelete() 0 5 1
A restore() 0 5 1
A bootIfNotBooted() 0 12 2
A boot() 0 4 1
A bootTraits() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
64
        $this->makeModel();
65
66
        $this->repository = $repository;
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
    public function getModel()
96
    {
97
        return $this->model;
98
    }
99
100
    protected function makeModel()
101
    {
102
        $class = $this->model();
103
        if (empty($class)) {
104
            throw new InvalidArgumentException(
105
                sprintf(
106
                    'The component(%s) method "model()" not found value',
107
                    get_class($this)
108
                )
109
            );
110
        }
111
112
        $model = $this->app->make($this->model());
113
114
        if (!($model instanceof Model)) {
115
            throw new InvalidArgumentException(
116
                sprintf(
117
                    "Class %s must be an instance of %s",
118
                    $this->model(),
119
                    Model::class
120
                )
121
            );
122
        }
123
124
        return $this->model = $model;
125
    }
126
127
    public function getName()
128
    {
129
        return $this->name;
130
    }
131
132
    public function getTitle()
133
    {
134
        return $this->title;
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 View Code Duplication
    final public function fireView()
0 ignored issues
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...
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
        $view->setModel($this->getModel());
177
        $view->initialize();
178
179
        return $view;
180
    }
181
182
    public function get()
183
    {
184
        $view = $this->fireView();
185
186
        // $view->setRepository($this->getRepository());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
187
188
        return $view->get();
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194 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...
195
    {
196
        if (!method_exists($this, 'callCreate')) {
197
            return;
198
        }
199
200
        $form = $this->app->call([$this, 'callCreate']);
201
        if (!$form instanceof FormInterface) {
202
            throw new InvalidArgumentException(
203
                sprintf(
204
                    'callCreate must be instanced of "%s".',
205
                    FormInterface::class
206
                )
207
            );
208
        }
209
210
        $form->setModel($this->getModel());
211
212
        return $form;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function store()
219
    {
220
        $form = $this->fireCreate();
221
222
        $form->validate()->save();
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    final public function fireEdit($id)
229
    {
230
        if (!method_exists($this, 'callEdit')) {
231
            return;
232
        }
233
234
        $form = $this->app->call([$this, 'callEdit'], ['id' => $id]);
235
236
        if (!$form instanceof FormInterface) {
237
            throw new InvalidArgumentException(
238
                sprintf(
239
                    'callEdit must be instanced of "%s".',
240
                    FormInterface::class
241
                )
242
            );
243
        }
244
245
        $model = $this->getRepository()->findOrFail($id);
246
247
        $form->setModel($model);
248
249
        return $form;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function update($id)
256
    {
257
        $form = $this->fireEdit($id);
258
        $form->validate()->save();
259
    }
260
261
    public function delete($id)
262
    {
263
        $this->getRepository()->delete($id);
264
        return true;
265
    }
266
267
    public function forceDelete($id)
268
    {
269
        $this->getRepository()->forceDelete($id);
270
        return true;
271
    }
272
273
    public function restore($id)
274
    {
275
        $this->getRepository()->restore($id);
276
        return true;
277
    }
278
279
    protected function bootIfNotBooted()
280
    {
281
        if (!isset(static::$booted[static::class])) {
282
            static::$booted[static::class] = true;
283
284
            $this->fireEvent('booting', false);
285
286
            $this->boot();
287
288
            $this->fireEvent('booted', false);
289
        }
290
    }
291
292
    /**
293
     * The "booting" method of the model.
294
     *
295
     * @return void
296
     */
297
    protected function boot()
298
    {
299
        $this->bootTraits();
300
    }
301
302
    /**
303
     * Boot all of the bootable traits on the model.
304
     *
305
     * @return void
306
     */
307
    protected function bootTraits()
308
    {
309
        foreach (class_uses_recursive($this) as $trait) {
310
            if (method_exists($this, $method = 'boot' . class_basename($trait))) {
311
                $this->$method();
312
            }
313
        }
314
    }
315
}
316