Completed
Push — master ( 20f1f2...9061ff )
by wen
13:56
created

Component::bootTraits()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use BadMethodCallException;
6
use InvalidArgumentException;
7
use Illuminate\Foundation\Application;
8
use Sco\Admin\Component\Concerns\HasAccess;
9
use Sco\Admin\Component\Concerns\HasEvents;
10
use Sco\Admin\Component\Concerns\HasNavigation;
11
use Sco\Admin\Contracts\ComponentInterface;
12
use Sco\Admin\Contracts\Form\FormInterface;
13
use Sco\Admin\Contracts\RepositoryInterface;
14
use Sco\Admin\Contracts\View\ViewInterface;
15
use Sco\Admin\Contracts\WithNavigation;
16
17
abstract class Component implements
18
    ComponentInterface,
19
    WithNavigation
20
{
21
    use HasAccess, HasEvents, HasNavigation;
22
23
    /**
24
     * @var
25
     */
26
    protected $name;
27
28
    /**
29
     * @var \Illuminate\Foundation\Application
30
     */
31
    protected $app;
32
33
    /**
34
     * The component display name
35
     *
36
     * @var string
37
     */
38
    protected $title;
39
40
    /**
41
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
42
     */
43
    protected $repository;
44
45
    /**
46
     * @var \Illuminate\Database\Eloquent\Model
47
     */
48
    protected $model;
49
50
    protected static $booted = [];
51
52
    /**
53
     * @var \Illuminate\Contracts\Events\Dispatcher
54
     */
55
    protected static $dispatcher;
56
57
    public function __construct(Application $app, $modelClass = null)
58
    {
59
        $this->app = $app;
60
61
        $this->repository = $this->app->make(RepositoryInterface::class);
62
        $this->repository->setClass($modelClass);
63
64
        $this->model = $this->repository->getModel();
65
        if (!$this->name) {
66
            $this->setDefaultName();
67
        }
68
69
        $this->bootIfNotBooted();
70
    }
71
72
    protected function setDefaultName()
73
    {
74
        $this->name = $this->getModelClassName();
75
    }
76
77
    protected function getModelClassName()
78
    {
79
        return snake_case(
80
            str_plural(
81
                class_basename(
82
                    get_class($this->getModel())
83
                )
84
            )
85
        );
86
    }
87
88
    public function getName()
89
    {
90
        return $this->name;
91
    }
92
93
    public function getTitle()
94
    {
95
        return $this->title;
96
    }
97
98
    public function getModel()
99
    {
100
        return $this->model;
101
    }
102
103
    public function getRepository()
104
    {
105
        return $this->repository;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getConfigs()
112
    {
113
        return collect([
114
            //'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...
115
            'title'    => $this->getTitle(),
116
            'accesses' => $this->getAccesses(),
117
            'view'     => $this->fireView(),
118
            //'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...
119
        ]);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function fireView()
126
    {
127
        if (!method_exists($this, 'callView')) {
128
            throw new BadMethodCallException('Not Found Method "callView"');
129
        }
130
131
        $view = $this->app->call([$this, 'callView']);
132
133
        if (!$view instanceof ViewInterface) {
134
            throw new InvalidArgumentException(
135
                sprintf(
136
                    'callView must be instanced of "%s".',
137
                    ViewInterface::class
138
                )
139
            );
140
        }
141
142
        return $view;
143
    }
144
145
    public function get()
146
    {
147
        $view = $this->fireView();
148
149
        $view->setRepository($this->getRepository());
150
151
        return $view->get();
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function fireCreate()
158
    {
159
        if (!method_exists($this, 'callCreate')) {
160
            return;
161
        }
162
163
        $form = $this->app->call([$this, 'callCreate']);
164
        if (!$form instanceof FormInterface) {
165
            throw new InvalidArgumentException(
166
                sprintf(
167
                    'callCreate must be instanced of "%s".',
168
                    FormInterface::class
169
                )
170
            );
171
        }
172
173
        $form->setModel($this->getModel());
174
175
        return $form;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function store()
182
    {
183
        $form = $this->fireCreate();
184
185
        $form->validate()->save();
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function fireEdit($id)
192
    {
193
        if (!method_exists($this, 'callEdit')) {
194
            return;
195
        }
196
197
        $form = $this->app->call([$this, 'callEdit'], ['id' => $id]);
198
199
        if (!$form instanceof FormInterface) {
200
            throw new InvalidArgumentException(
201
                sprintf(
202
                    'callEdit must be instanced of "%s".',
203
                    FormInterface::class
204
                )
205
            );
206
        }
207
208
        $model = $this->getRepository()->findOrFail($id);
209
210
        $form->setModel($model);
211
212
        return $form;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function update($id)
219
    {
220
        $form = $this->fireEdit($id);
221
        $form->validate()->save();
222
    }
223
224
    public function delete($id)
225
    {
226
        $this->getRepository()->findOrFail($id)->delete();
227
        return true;
228
    }
229
230
    public function forceDelete($id)
231
    {
232
        $this->getRepository()->forceDelete($id);
233
        return true;
234
    }
235
236
    public function restore($id)
237
    {
238
        $this->getRepository()->restore($id);
239
        return true;
240
    }
241
242
    protected function bootIfNotBooted()
243
    {
244
        if (!isset(static::$booted[static::class])) {
245
            static::$booted[static::class] = true;
246
247
            $this->fireEvent('booting', false);
248
249
            $this->boot();
250
251
            $this->fireEvent('booted', false);
252
        }
253
    }
254
255
    /**
256
     * The "booting" method of the model.
257
     *
258
     * @return void
259
     */
260
    protected function boot()
261
    {
262
        $this->bootTraits();
263
    }
264
265
    /**
266
     * Boot all of the bootable traits on the model.
267
     *
268
     * @return void
269
     */
270
    protected function bootTraits()
271
    {
272
        foreach (class_uses_recursive($this) as $trait) {
273
            if (method_exists($this, $method = 'boot'.class_basename($trait))) {
274
                $this->$method();
275
            }
276
        }
277
    }
278
}
279