Completed
Push — master ( e12ed1...1e692e )
by wen
03:09
created

Component::isEdit()   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
    protected 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
    protected function bootIfNotBooted()
145
    {
146
        if (!isset(static::$booted[static::class])) {
147
            static::$booted[static::class] = true;
148
149
            $this->fireEvent('booting', false);
150
151
            $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...
152
153
            $this->fireEvent('booted', false);
154
        }
155
    }
156
157
    public function boot()
158
    {
159
        return true;
160
    }
161
162
    public function isView()
163
    {
164
        return method_exists($this, 'callView') && $this->can('view');
165
    }
166
167
    public function isCreate()
168
    {
169
        return method_exists($this, 'callCreate') && $this->can('create');
170
    }
171
172
    public function isEdit()
173
    {
174
        return method_exists($this, 'callEdit') && $this->can('edit');
175
    }
176
177
    public function isDelete()
178
    {
179
        return $this->can('delete');
180
    }
181
182
    public function isDestroy()
183
    {
184
        return $this->isRestorableModel() && $this->can('destroy');
185
    }
186
187
    public function isRestore()
188
    {
189
        return $this->isRestorableModel() && $this->can('restore');
190
    }
191
192
    protected function isRestorableModel()
193
    {
194
        return $this->getRepository()->isRestorable();
195
    }
196
197
    public function registerObserver($class = null)
198
    {
199
        if (!$class) {
200
            return;
201
        }
202
203
        $className = is_string($class) ? $class : get_class($class);
204
205
        foreach ($this->permissionMethods as $method) {
206
            if (method_exists($class, $method)) {
207
                $this->registerPermission($method, [$this->app->make($className), $method]);
208
            }
209
        }
210
    }
211
212
    public function registerPermission($permission, $callback)
213
    {
214
        $this->permissions[$permission] = $callback;
215
    }
216
217
    public function can($permission)
218
    {
219
        if (is_callable($this->permissions[$permission])) {
220
            return call_user_func_array($this->permissions[$permission], [$this]);
221
        }
222
        return $this->permissions[$permission] ? true : false;
223
    }
224
225
    public function getPermissions()
226
    {
227
        $data = collect();
228
        foreach ($this->permissions as $perm) {
229
            $method = 'is' . ucfirst($perm);
230
            if (!method_exists($this, $method)) {
231
                $method = 'can';
232
            }
233
234
            $data->put($perm, $this->{$method}($perm));
235
        }
236
        return $data;
237
    }
238
}
239