Completed
Push — master ( 1d3c0c...8bf0fe )
by Song
03:06
created

BatchAction::retrieveModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Actions;
4
5
use Illuminate\Http\Request;
6
7
abstract class BatchAction extends GridAction
8
{
9
    /**
10
     * @var string
11
     */
12
    public $selectorPrefix = '.grid-batch-action-';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function actionScript()
18
    {
19
        $warning = __('No data selected!');
20
21
        return <<<SCRIPT
22
        var key = $.admin.grid.selected();
23
        
24
        if (key.length === 0) {
25
            $.admin.toastr.warning('{$warning}', '', {positionClass: 'toast-top-center'});
26
            return ;
27
        }
28
        
29
        Object.assign(data, {_key:key});
30
SCRIPT;
31
    }
32
33
    /**
34
     * @param Request $request
35
     * @return mixed
36
     */
37
    public function retrieveModel(Request $request)
38
    {
39
        if (!$key = $request->get('_key')) {
40
            return false;
41
        }
42
43
        $modelClass = str_replace('_', '\\', $request->get('_model'));
44
45
        if (is_string($key)) {
46
            $key = explode(',', $key);
47
        }
48
49
        return $modelClass::findOrFail($key);
50
    }
51
52
    /**
53
     * @return string
54
     */
55 View Code Duplication
    public function render()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this->addScript();
58
59
        $content = $this->html();
60
61
        if ($content && $this->interactor instanceof Interactor\Form) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $content of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
62
            return $this->interactor->addElementAttr($content, $this->selector);
63
        }
64
65
        return sprintf(
66
            "<a href='javascript:void(0);' class='%s'>%s</a>",
67
            $this->getElementClass(),
68
            $this->name()
69
        );
70
    }
71
}