Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — master ( b8bb45...33d313 )
by Cristian
15:15 queued 08:07
created

CrudButton::dd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel;
4
5
/**
6
 * Adds fluent syntax to Backpack CRUD Buttons.
7
 *
8
 * In addition to the existing:
9
 * - CRUD::addButton('top', 'create', 'view', 'crud::buttons.create');
10
 *
11
 * Developers can also do:
12
 * - CRUD::button('create')->stack('top')->view('crud::buttons.create');
13
 *
14
 * And if the developer uses CrudButton as Button in their CrudController:
15
 * - Button::name('create')->stack('top')->view('crud::butons.create');
16
 */
17
class CrudButton
18
{
19
    public $stack;
20
    public $name;
21
    public $type;
22
    public $content;
23
    public $position;
24
25
    public function __construct($name, $stack = null, $type = null, $content = null, $position = null)
26
    {
27
        // in case an array was passed as name
28
        // assume it's an array that includes [$name, $stack, $type, $content]
29
        if (is_array($name)) {
30
            extract($name);
31
        }
32
33
        $this->name = $name ?? 'button_'.rand(1, 999999999);
34
        $this->stack = $stack ?? 'top';
35
        $this->type = $type ?? 'view';
36
        $this->content = $content;
37
38
        // if no position was passed, the defaults are:
39
        // - 'beginning' for the 'line' stack
40
        // - 'end' for all other stacks
41
        $this->position = $position ?? ($this->stack == 'line' ? 'beginning' : 'end');
42
43
        return $this->save();
44
    }
45
46
    // ------
47
    // MAKERS
48
    // ------
49
50
    /**
51
     * Add a new button to the default stack.
52
     *
53
     * @param  string|array $attributes Button name or array that contains name, stack, type and content.
54
     */
55
    public static function name($attributes = null)
56
    {
57
        return new static($attributes);
58
    }
59
60
    /**
61
     * Add a new button to the default stack.
62
     *
63
     * @param  string|array $attributes Button name or array that contains name, stack, type and content.
64
     */
65
    public static function add($attributes = null)
66
    {
67
        return new static($attributes);
68
    }
69
70
    /**
71
     * This method allows one to create a button without attaching it to any 'real'
72
     * button stack, by moving it to a 'hidden' stack.
73
     *
74
     * It exists for one reason: so that developers can add buttons to a custom array, without
75
     * adding them to one of the button stacks.
76
     *
77
     * Ex: when developers need to pass multiple buttons as contents of the
78
     * div button. But they don't want them added to the before_content of after_content
79
     * stacks. So what they do is basically add them to a 'hidden' stack, that nobody will ever see.
80
     *
81
     * @param  string|array $attributes Button name or array that contains name, stack, type and content.
82
     * @return CrudButton
83
     */
84
    public static function make($attributes = null)
85
    {
86
        $button = static::add($attributes);
87
        $button->stack('hidden');
88
89
        return $button;
90
    }
91
92
    // -------
93
    // SETTERS
94
    // -------
95
96
    /**
97
     * Set the button stack (where the button will be shown).
98
     *
99
     * @param  string $stack The name of the stack where the button should be moved.
100
     * @return CrudButton
101
     */
102
    public function stack($stack)
103
    {
104
        $this->stack = $stack;
105
106
        return $this->save();
107
    }
108
109
    /**
110
     * Sets the button type (view or model_function).
111
     *
112
     * @param  string $type The type of button - view or model_function.
113
     * @return CrudButton
114
     */
115
    public function type($type)
116
    {
117
        $this->type = $type;
118
119
        return $this->save();
120
    }
121
122
    /**
123
     * Sets the content of the button.
124
     * For the view button type, set it to the view path, including namespace.
125
     * For the model_function button type, set it to the name of the method on the model.
126
     *
127
     * @param  string $content Path to view or name of method on Model.
128
     * @return CrudButton
129
     */
130
    public function content($content)
131
    {
132
        $this->content = $content;
133
134
        return $this->save();
135
    }
136
137
    /**
138
     * Sets the namespace and path of the view for this button.
139
     * Sets the button type as 'view'.
140
     *
141
     * @param  string $value Path to view file.
142
     * @return CrudButton
143
     */
144
    public function view($value)
145
    {
146
        $this->content = $value;
147
        $this->type = 'view';
148
149
        return $this->save();
150
    }
151
152
    /**
153
     * Sets the name of the method on the model that contains the HTML for this button.
154
     * Sets the button type as 'model_function'.
155
     *
156
     * @param  string $value Name of the method on the model.
157
     * @return CrudButton
158
     */
159
    public function modelFunction($value)
160
    {
161
        $this->content = $value;
162
        $this->type = 'model_function';
163
        $this->stack = 'line';
164
165
        return $this->save();
166
    }
167
168
    /**
169
     * Sets the name of the method on the model that contains the HTML for this button.
170
     * Sets the button type as 'model_function'.
171
     * Alias of the modelFunction() method.
172
     *
173
     * @param  string $value Name of the method on the model.
174
     * @return CrudButton
175
     */
176
    public function model_function($value)
177
    {
178
        return $this->modelFunction($value);
179
    }
180
181
    /**
182
     * Unserts an property that is set on the current button.
183
     * Possible properties: name, stack, type, content.
184
     *
185
     * @param  string $property Name of the property that should be cleared.
186
     * @return CrudButton
187
     */
188
    public function forget($property)
189
    {
190
        $this->{$property} = null;
191
192
        return $this->save();
193
    }
194
195
    // --------------
196
    // SETTER ALIASES
197
    // --------------
198
199
    /**
200
     * Moves the button to a certain button stack.
201
     * Alias of stack().
202
     *
203
     * @param  string $stack The name of the stack where the button should be moved.
204
     *
205
     * @return self
206
     */
207
    public function to($stack)
208
    {
209
        return $this->stack($stack);
210
    }
211
212
    /**
213
     * Moves the button to a certain button stack.
214
     * Alias of stack().
215
     *
216
     * @param  string $stack The name of the stack where the button should be moved.
217
     *
218
     * @return self
219
     */
220
    public function group($stack)
221
    {
222
        return $this->stack($stack);
223
    }
224
225
    /**
226
     * Moves the button to a certain button stack.
227
     * Alias of stack().
228
     *
229
     * @param  string $stack The name of the stack where the button should be moved.
230
     *
231
     * @return self
232
     */
233
    public function section($stack)
234
    {
235
        return $this->stack($stack);
236
    }
237
238
    // -------
239
    // GETTERS
240
    // -------
241
242
    /**
243
     * Get the end result that should be displayed to the user.
244
     * The HTML itself of the button.
245
     *
246
     * @param  object|null $entry The eloquent Model for the current entry or null if no current entry.
247
     *
248
     * @return HTML
0 ignored issues
show
Bug introduced by
The type Backpack\CRUD\app\Library\CrudPanel\HTML was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
249
     */
250
    public function getHtml($entry = null)
251
    {
252
        $button = $this;
253
        $crud = $this->crud();
254
255
        if ($this->type == 'model_function') {
256
            if (is_null($entry)) {
257
                return $crud->model->{$button->content}($crud);
258
            }
259
260
            return $entry->{$button->content}($crud);
261
        }
262
263
        if ($this->type == 'view') {
264
            if (view()->exists($button->content)) {
265
                return view($button->content, compact('button', 'crud', 'entry'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view($button->con...ton', 'crud', 'entry')) returns the type Illuminate\View\View which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\HTML.
Loading history...
266
            } else {
267
                abort(500, 'Button view "'.$button->content.'" does not exist');
268
            }
269
        }
270
271
        abort(500, 'Unknown button type');
272
    }
273
274
    /**
275
     * Get the key for this button in the global buttons collection.
276
     *
277
     * @return int
278
     */
279
    public function getKey()
280
    {
281
        return $this->crud()->getButtonKey($this->name);
282
    }
283
284
    // -----
285
    // ORDER
286
    // -----
287
    // Manipulate the button collection (inside the global CrudPanel object).
288
289
    /**
290
     * Move this button to be the first in the buttons list.
291
     *
292
     * @return CrudButton
293
     */
294
    public function makeFirst()
295
    {
296
        $this->remove();
297
        $this->collection()->prepend($this);
298
299
        return $this;
300
    }
301
302
    /**
303
     * Move this button to be the last one in the buttons list.
304
     *
305
     * @return CrudButton
306
     */
307
    public function makeLast()
308
    {
309
        $this->remove();
310
        $this->collection()->push($this);
311
312
        return $this;
313
    }
314
315
    /**
316
     * Move the current filter after another filter.
317
     *
318
     * @param  string $destination Name of the destination filter.
319
     * @return CrudFilter
320
     */
321
    public function after($destination)
322
    {
323
        $this->crud()->moveButton($this->name, 'after', $destinationName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $destinationName does not exist. Did you maybe mean $destination?
Loading history...
324
325
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Backpack\CRUD\app\Library\CrudPanel\CrudButton which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\CrudFilter.
Loading history...
326
    }
327
328
    /**
329
     * Move the current field before another field.
330
     *
331
     * @param  string $destination Name of the destination field.
332
     * @return CrudFilter
333
     */
334
    public function before($destination)
335
    {
336
        $this->crud()->moveButton($this->name, 'before', $destinationName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $destinationName does not exist. Did you maybe mean $destination?
Loading history...
337
338
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Backpack\CRUD\app\Library\CrudPanel\CrudButton which is incompatible with the documented return type Backpack\CRUD\app\Library\CrudPanel\CrudFilter.
Loading history...
339
    }
340
341
    /**
342
     * Remove the button from the global button collection.
343
     *
344
     * @return CrudButton
345
     */
346
    public function remove()
347
    {
348
        $this->collection()->pull($this->getKey());
349
350
        return $this;
351
    }
352
353
    // --------------
354
    // GLOBAL OBJECTS
355
    // --------------
356
    // Access to the objects stored in Laravel's service container.
357
358
    /**
359
     * Access the global collection when all buttons are stored.
360
     *
361
     * @return \Illuminate\Support\Collection
362
     */
363
    public function collection()
364
    {
365
        return $this->crud()->buttons();
366
    }
367
368
    /**
369
     * Access the global CrudPanel object.
370
     *
371
     * @return \Backpack\CRUD\app\Library\CrudPanel\CrudPanel
372
     */
373
    public function crud()
374
    {
375
        return app('crud');
376
    }
377
378
    // -----------------
379
    // DEBUGGING METHODS
380
    // -----------------
381
382
    /**
383
     * Dump the current object to the screen,
384
     * so that the developer can see its contents.
385
     *
386
     * @return CrudButton
387
     */
388
    public function dump()
389
    {
390
        dump($this);
391
392
        return $this;
393
    }
394
395
    /**
396
     * Dump and die. Duumps the current object to the screen,
397
     * so that the developer can see its contents, then stops
398
     * the execution.
399
     *
400
     * @return CrudButton
401
     */
402
    public function dd()
403
    {
404
        dd($this);
405
406
        return $this;
407
    }
408
409
    // ---------------
410
    // PRIVATE METHODS
411
    // ---------------
412
413
    /**
414
     * Update the global CrudPanel object with the current button.
415
     *
416
     * @return CrudButton
417
     */
418
    private function save()
419
    {
420
        $itemExists = $this->collection()->contains('name', $this->name);
421
422
        if (! $itemExists) {
423
            if ($this->position == 'beginning') {
424
                $this->collection()->prepend($this);
425
            } else {
426
                $this->collection()->push($this);
427
            }
428
429
            // clear the custom position, so that the next daisy chained method
430
            // doesn't move it yet again
431
            $this->position = null;
432
        } else {
433
            $this->collection()->replace([$this->getKey() => $this]);
434
        }
435
436
        return $this;
437
    }
438
}
439