Completed
Push — master ( 55ab89...0c5b4e )
by Song
06:39
created

Tree::variables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin;
4
5
use Encore\Admin\Facades\Admin as AdminManager;
6
use Illuminate\Contracts\Support\Renderable;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Http\Request;
9
10
class Tree implements Renderable
11
{
12
    protected $items = [];
13
14
    protected $script;
15
16
    protected $elementId = 'tree-';
17
18
    protected $model;
19
20
    public function __construct(Model $model = null)
21
    {
22
        $this->model = $model;
23
24
        $this->path = app('router')->current()->getPath();
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->elementId .= uniqid();
26
    }
27
28
    /**
29
     * Variables in tree template.
30
     *
31
     * @return array
32
     */
33
    public function variables()
34
    {
35
        return [
36
            'items' => $this->model->toTree(),
37
            'id'    => $this->elementId,
38
        ];
39
    }
40
41
    /**
42
     * Build tree grid scripts.
43
     *
44
     * @return void
45
     */
46
    protected function buildupScript()
47
    {
48
        $confirm = trans('admin::lang.delete_confirm');
49
        $token = csrf_token();
50
51
        $this->script = <<<SCRIPT
52
53
        $('#{$this->elementId}').nestable({});
54
55
        $('._delete').click(function() {
56
            var id = $(this).data('id');
57
            if(confirm("{$confirm}")) {
58
                $.post('/{$this->path}/' + id, {_method:'delete','_token':'{$token}'}, function(data){
59
                    $.pjax.reload('#pjax-container');
60
                });
61
            }
62
        });
63
64
        $('.{$this->elementId}-save').click(function () {
65
            var serialize = $('#{$this->elementId}').nestable('serialize');
66
            $.get('/{$this->path}', {'_tree':JSON.stringify(serialize)}, function(data){
67
                $.pjax.reload('#pjax-container');
68
            });
69
        });
70
71
        $('.{$this->elementId}-refresh').click(function () {
72
            $.pjax.reload('#pjax-container');
73
        });
74
75
76
SCRIPT;
77
    }
78
79
    /**
80
     * Render a tree.
81
     *
82
     * @return \Illuminate\Http\JsonResponse|string
83
     */
84
    public function render()
85
    {
86
        if (Request::capture()->has('_tree')) {
87
            return response()->json([
88
                'status' => $this->saveTree(Request::capture()->get('_tree')),
89
            ]);
90
        }
91
92
        $this->buildupScript();
93
94
        AdminManager::script($this->script);
95
96
        view()->share(['path'  => $this->path]);
0 ignored issues
show
Bug introduced by
The method share does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
97
98
        return view('admin::tree', $this->variables())->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
99
    }
100
101
    /**
102
     * Build menu tree presented by array.
103
     *
104
     * @param string $serialize
105
     *
106
     * @return bool
107
     */
108
    public function saveTree($serialize)
109
    {
110
        $tree = json_decode($serialize, true);
111
112
        if (json_last_error() != JSON_ERROR_NONE) {
113
            throw new \InvalidArgumentException(json_last_error_msg());
114
        }
115
116
        $this->model->saveTree($tree);
117
118
        return true;
119
    }
120
121
    /**
122
     * Get the string contents of the grid view.
123
     *
124
     * @return string
125
     */
126
    public function __toString()
127
    {
128
        return $this->render();
129
    }
130
}
131