Completed
Push — master ( 0e6a09...ebc724 )
by wen
02:37
created

Component::isDestroy()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use Illuminate\Foundation\Application;
6
use Sco\Admin\Component\Concerns\HasEvents;
7
use Sco\Admin\Component\Concerns\HasNavigation;
8
use Sco\Admin\Contracts\ComponentInterface;
9
use Sco\Admin\Contracts\RepositoryInterface;
10
use Sco\Admin\Exceptions\BadMethodCallException;
11
12
abstract class Component implements ComponentInterface
13
{
14
    use HasEvents,
15
        HasNavigation;
16
17
    /**
18
     * @var
19
     */
20
    protected $name;
21
22
    /**
23
     * @var \Illuminate\Foundation\Application
24
     */
25
    protected $app;
26
27
    protected $title;
28
29
    /**
30
     * @var mixed|\Sco\Admin\Contracts\RepositoryInterface
31
     */
32
    protected $repository;
33
34
    /**
35
     * @var \Illuminate\Database\Eloquent\Model
36
     */
37
    protected $model;
38
39
    protected static $booted = [];
40
41
    /**
42
     * @var \Illuminate\Contracts\Events\Dispatcher
43
     */
44
    protected static $dispatcher;
45
46
    /**
47
     * @var string
48
     */
49
    protected $permissionObserver;
50
51
    protected $permissions;
52
53
    protected $permissionMethods = [
54
        'view', 'create', 'edit',
55
        'delete', 'destroy', 'restore',
56
    ];
57
58
    public function __construct(Application $app, $modelClass)
59
    {
60
        $this->app = $app;
61
62
        $this->repository = $this->app->make(RepositoryInterface::class);
63
        $this->repository->setClass($modelClass);
64
65
        $this->model = $this->repository->getModel();
66
        if (!$this->name) {
67
            $this->setDefaultName();
68
        }
69
70
        $this->registerObserver($this->permissionObserver);
71
72
        $this->bootIfNotBooted();
73
    }
74
75
    protected function setDefaultName()
76
    {
77
        $this->name = $this->getModelClassName();
78
    }
79
80
    protected function getModelClassName()
81
    {
82
        return snake_case(str_plural(class_basename(get_class($this->getModel()))));
83
    }
84
85
    public function getName()
86
    {
87
        return $this->name;
88
    }
89
90
    public function getTitle()
91
    {
92
        return $this->title;
93
    }
94
95
    public function getModel()
96
    {
97
        return $this->model;
98
    }
99
100
    public function getRepository()
101
    {
102
        return $this->repository;
103
    }
104
105
    public function get()
106
    {
107
        $view = $this->fireView();
108
109
        $this->getRepository();
0 ignored issues
show
Unused Code introduced by
The call to the method Sco\Admin\Component\Component::getRepository() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
110
111
        $view->setRepository($this->getRepository());
112
113
        return $view->get();
114
    }
115
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getConfigs()
121
    {
122
        return collect([
123
            'primaryKey'  => $this->getModel()->getKeyName(),
124
            'title'       => $this->getTitle(),
125
            'permissions' => $this->getPermissions(),
126
            'view'        => $this->fireView(),
127
            //'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...
128
        ]);
129
    }
130
131
    /**
132
     * @return \Sco\Admin\Contracts\ViewInterface
133
     */
134
    public function fireView()
135
    {
136
        if (!method_exists($this, 'callView')) {
137
            throw new BadMethodCallException('Not Found Method "callView"');
138
        }
139
140
        $view = $this->app->call([$this, 'callView']);
141
        return $view;
142
    }
143
144
    public function fireCreate()
145
    {
146
        if (!method_exists($this, 'callCreate')) {
147
            return;
148
        }
149
150
        $form = $this->app->call([$this, 'callCreate']);
151
152
        return $form;
153
    }
154
155
156
    protected function bootIfNotBooted()
157
    {
158
        if (!isset(static::$booted[static::class])) {
159
            static::$booted[static::class] = true;
160
161
            $this->fireEvent('booting', false);
162
163
            $this->boot();
0 ignored issues
show
Unused Code introduced by
The call to the method Sco\Admin\Component\Component::boot() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
164
165
            $this->fireEvent('booted', false);
166
        }
167
    }
168
169
    public function boot()
170
    {
171
        return true;
172
    }
173
174
    public function isView()
175
    {
176
        return method_exists($this, 'callView') && $this->can('view');
177
    }
178
179
    public function isCreate()
180
    {
181
        return method_exists($this, 'callCreate') && $this->can('create');
182
    }
183
184
    public function isEdit()
185
    {
186
        return method_exists($this, 'callEdit') && $this->can('edit');
187
    }
188
189
    public function isDelete()
190
    {
191
        return $this->can('delete');
192
    }
193
194
    public function isDestroy()
195
    {
196
        return $this->isRestorableModel() && $this->can('destroy');
197
    }
198
199
    public function isRestore()
200
    {
201
        return $this->isRestorableModel() && $this->can('restore');
202
    }
203
204
    protected function isRestorableModel()
205
    {
206
        return $this->getRepository()->isRestorable();
207
    }
208
209
    public function registerObserver($class = null)
210
    {
211
        if (!$class) {
212
            return;
213
        }
214
215
        $className = is_string($class) ? $class : get_class($class);
216
217
        foreach ($this->permissionMethods as $method) {
218
            if (method_exists($class, $method)) {
219
                $this->registerPermission($method, [$this->app->make($className), $method]);
220
            }
221
        }
222
    }
223
224
    public function registerPermission($permission, $callback)
225
    {
226
        $this->permissions[$permission] = $callback;
227
    }
228
229
    public function can($permission)
230
    {
231
        if (is_callable($this->permissions[$permission])) {
232
            return call_user_func_array($this->permissions[$permission], [$this]);
233
        }
234
        return $this->permissions[$permission] ? true : false;
235
    }
236
237
    public function getPermissions()
238
    {
239
        $data = collect();
240
        foreach ($this->permissionMethods as $perm) {
241
            $method = 'is' . ucfirst($perm);
242
            if (!method_exists($this, $method)) {
243
                $method = 'can';
244
            }
245
246
            $data->put($perm, $this->{$method}($perm));
247
        }
248
        return $data;
249
    }
250
}
251