GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 414922...a9bc98 )
by butschster
12:35
created

Control::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Column;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Display\ControlLink;
8
use SleepingOwl\Admin\Display\TableColumn;
9
use SleepingOwl\Admin\Display\ControlButton;
10
use SleepingOwl\Admin\Contracts\Display\ControlButtonInterface;
11
12
class Control extends TableColumn
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $view = 'column.control';
18
19
    /**
20
     * @var string
21
     */
22
    protected $width = '90px';
23
24
    /**
25
     * @var Collection
26
     */
27
    protected $buttons;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $editable = true;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $deletable = true;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $destroyable = true;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $restorable = true;
48
49
    /**
50
     * Control constructor.
51
     *
52
     * @param string|null $label
53
     */
54
    public function __construct($label = null)
55
    {
56
        parent::__construct($label);
57
58
        $this->buttons = new Collection();
59
60
        $this->setHtmlAttribute('class', 'text-right');
61
    }
62
63
    public function initialize()
64
    {
65
        parent::initialize();
66
67
        $this->buttons->put('edit', $button = new ControlLink(function (Model $model) {
68
            return $this->getModelConfiguration()->getEditUrl($model->getKey());
69
        }, trans('sleeping_owl::lang.table.edit'), 100));
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.table.edit') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Displa...trolLink::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
        $button->hideText();
71
        $button->setCondition(function () {
72
            return $this->isEditable();
73
        });
74
        $button->setIcon('fa fa-pencil');
75
        $button->setHtmlAttribute('class', 'btn-primary');
76
77
        $this->buttons->put('delete', $button = new ControlButton(function (Model $model) {
78
            return $this->getModelConfiguration()->getDeleteUrl($model->getKey());
79
        }, trans('sleeping_owl::lang.table.delete'), 200));
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.table.delete') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Displa...trolLink::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
        $button->setCondition(function () {
81
            return $this->isDeletable();
82
        });
83
84
        $button->setMethod('delete');
85
        $button->hideText();
86
        $button->setIcon('fa fa-trash');
87
        $button->setHtmlAttribute('class', 'btn-danger btn-delete');
88
89
        $this->buttons->put('destroy', $button = new ControlButton(function (Model $model) {
90
            return $this->getModelConfiguration()->getDestroyUrl($model->getKey());
91
        }, trans('sleeping_owl::lang.table.destroy'), 300));
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.table.destroy') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Displa...trolLink::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
        $button->setCondition(function () {
93
            return $this->isDestroyable();
94
        });
95
96
        $button->setMethod('delete');
97
        $button->hideText();
98
        $button->setIcon('fa fa-trash');
99
        $button->setHtmlAttribute('class', 'btn-danger btn-destroy');
100
101
        $this->buttons->put('restore', $button = new ControlButton(function (Model $model) {
102
            return $this->getModelConfiguration()->getRestoreUrl($model->getKey());
103
        }, trans('sleeping_owl::lang.table.restore'), 400));
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.table.restore') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, SleepingOwl\Admin\Displa...trolLink::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
        $button->setCondition(function () {
105
            return $this->isRestorable();
106
        });
107
        $button->hideText();
108
        $button->setIcon('fa fa-reply');
109
        $button->setHtmlAttribute('class', 'btn-warning');
110
    }
111
112
    /**
113
     * @param ControlButtonInterface $button
114
     *
115
     * @return $this
116
     */
117
    public function addButton(ControlButtonInterface $button)
118
    {
119
        $this->buttons->push($button);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param bool $editable
126
     * @return $this
127
     */
128
    public function setEditable($editable)
129
    {
130
        $this->editable = (bool) $editable;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @param bool $deletable
137
     * @return $this
138
     */
139
    public function setDeletable($deletable)
140
    {
141
        $this->deletable = (bool) $deletable;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param bool $destroyable
148
     * @return $this
149
     */
150
    public function setDestroyable($destroyable)
151
    {
152
        $this->destroyable = (bool) $destroyable;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param bool $restorable
159
     * @return $this
160
     */
161
    public function setRestorable($restorable)
162
    {
163
        $this->restorable = (bool) $restorable;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Check if instance supports soft-deletes and trashed.
170
     *
171
     * @return bool
172
     */
173
    protected function isTrashed()
174
    {
175
        if (method_exists($this->getModel(), 'trashed')) {
176
            return $this->getModel()->trashed();
177
        }
178
179
        return false;
180
    }
181
182
    /**
183
     * Check if instance editable.
184
     *
185
     * @return bool
186
     */
187
    protected function isEditable()
188
    {
189
        return
190
            $this->editable
191
            &&
192
            ! $this->isTrashed()
193
            &&
194
            $this->getModelConfiguration()->isEditable(
195
                $this->getModel()
196
            );
197
    }
198
199
    /**
200
     * Check if instance is deletable.
201
     *
202
     * @return bool
203
     */
204
    protected function isDeletable()
205
    {
206
        return
207
            $this->deletable
208
            &&
209
            ! $this->isTrashed()
210
            &&
211
            $this->getModelConfiguration()->isDeletable(
212
                $this->getModel()
213
            );
214
    }
215
216
    /**
217
     * Check if instance is force deletable.
218
     *
219
     * @return bool
220
     */
221
    protected function isDestroyable()
222
    {
223
        return
224
            $this->destroyable
225
            &&
226
            $this->isTrashed()
227
            &&
228
            $this->getModelConfiguration()->isDestroyable(
229
                $this->getModel()
230
            );
231
    }
232
233
    /**
234
     * Check if instance is restorable.
235
     *
236
     * @return bool
237
     */
238
    protected function isRestorable()
239
    {
240
        return
241
            $this->restorable
242
            &&
243
            $this->isTrashed()
244
            &&
245
            $this->getModelConfiguration()->isRestorable(
246
                $this->getModel()
247
            );
248
    }
249
250
    /**
251
     * @param Model $model
252
     *
253
     * @return $this
254
     */
255
    public function setModel(Model $model)
256
    {
257
        parent::setModel($model);
258
259
        return $this;
260
    }
261
262
    /**
263
     * @return array
264
     */
265
    public function toArray()
266
    {
267
        return parent::toArray() + [
268
            'buttons' => $this->buttons
269
                ->each(function (ControlButtonInterface $button) {
270
                    $button->setModel($this->getModel());
271
                })
272
                ->filter(function (ControlButtonInterface $button) {
273
                    return $button->isActive();
274
                })
275
                ->sortBy(function (ControlButtonInterface $button) {
276
                    return $button->getPosition();
277
                }),
278
        ];
279
    }
280
}
281