Completed
Push — master ( a2dd34...253c8c )
by Song
03:46
created

LogController::grid()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Controllers;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Encore\Admin\Auth\Database\OperationLog;
7
use Encore\Admin\Grid;
8
use Encore\Admin\Layout\Content;
9
use Illuminate\Routing\Controller;
10
11
class LogController extends Controller
12
{
13
    /**
14
     * Index interface.
15
     *
16
     * @param Content $content
17
     * @return Content
18
     */
19
    public function index(Content $content)
20
    {
21
        return $content
22
            ->header(trans('admin.operation_log'))
23
            ->description(trans('admin.list'))
24
            ->body($this->grid());
25
    }
26
27
    /**
28
     * @return Grid
29
     */
30
    protected function grid()
31
    {
32
        $grid = new Grid(new OperationLog);
33
34
        $grid->model()->orderBy('id', 'DESC');
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Encore\Admin\Grid\Model. Did you maybe mean resetOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
35
36
        $grid->id('ID')->sortable();
0 ignored issues
show
Documentation Bug introduced by
The method id does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
        $grid->user()->name('User');
0 ignored issues
show
Documentation Bug introduced by
The method user does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
38
        $grid->method()->display(function ($method) {
0 ignored issues
show
Documentation Bug introduced by
The method method does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
            $color = array_get(OperationLog::$methodColors, $method, 'grey');
40
41
            return "<span class=\"badge bg-$color\">$method</span>";
42
        });
43
        $grid->path()->label('info');
0 ignored issues
show
Documentation Bug introduced by
The method path does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
        $grid->ip()->label('primary');
0 ignored issues
show
Documentation Bug introduced by
The method ip does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
45
        $grid->input()->display(function ($input) {
0 ignored issues
show
Documentation Bug introduced by
The method input does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46
            $input = json_decode($input, true);
47
            $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
48
            if (empty($input)) {
49
                return '<code>{}</code>';
50
            }
51
52
            return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
53
        });
54
55
        $grid->created_at(trans('admin.created_at'));
0 ignored issues
show
Documentation Bug introduced by
The method created_at does not exist on object<Encore\Admin\Grid>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
57
        $grid->actions(function (Grid\Displayers\Actions $actions) {
58
            $actions->disableEdit();
59
            $actions->disableView();
60
        });
61
62
        $grid->disableCreation();
0 ignored issues
show
Deprecated Code introduced by
The method Encore\Admin\Grid::disableCreation() has been deprecated.

This method has been deprecated.

Loading history...
63
64
        $grid->filter(function ($filter) {
65
            $filter->equal('user_id', 'User')->select(Administrator::all()->pluck('name', 'id'));
66
            $filter->equal('method')->select(array_combine(OperationLog::$methods, OperationLog::$methods));
67
            $filter->like('path');
68
            $filter->equal('ip');
69
        });
70
71
        return $grid;
72
    }
73
74
    /**
75
     * @param mixed $id
76
     * @return \Illuminate\Http\JsonResponse
77
     */
78
    public function destroy($id)
79
    {
80
        $ids = explode(',', $id);
81
82
        if (OperationLog::destroy(array_filter($ids))) {
83
            $data = [
84
                'status'  => true,
85
                'message' => trans('admin.delete_succeeded'),
86
            ];
87
        } else {
88
            $data = [
89
                'status'  => false,
90
                'message' => trans('admin.delete_failed'),
91
            ];
92
        }
93
94
        return response()->json($data);
95
    }
96
}
97