Completed
Push — laravel-5.4 ( 381f13 )
by Tony
02:37
created

SyslogDataTable::getBuilderParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * app/DataTables/General/SyslogDataTable.php
4
 *
5
 * Datatable for syslog
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace App\DataTables\General;
27
28
use App\DataTables\BaseDataTable;
29
use App\Models\General\Syslog;
30
31
class SyslogDataTable extends BaseDataTable
32
{
33
    /**
34
     * Display ajax response.
35
     *
36
     * @return \Illuminate\Http\JsonResponse
37
     */
38 View Code Duplication
    public function ajax()
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...
39
    {
40
        return $this->datatables
41
            ->eloquent($this->query())
42
            ->orderColumn('name', '-name $1')
43
            ->editColumn('device_id', 'datatables.syslog.hostname')
44
            ->rawColumns(['device_id'])
45
            ->make(true);
46
    }
47
48
    /**
49
     * Get the query object to be processed by datatables.
50
     *
51
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
52
     */
53
    public function query()
54
    {
55
        $syslog = Syslog::with(['device' => function ($query) {
56
            return $query->addSelect(['device_id', 'hostname']);
57
        }])->select('syslog.*');
58
        return $this->applyScopes($syslog);
59
    }
60
61
    public function getBuilderParameters()
62
    {
63
        $params = parent::getBuilderParameters();
64
        $params['order'] = [[3, 'desc']];
65
        return $params;
0 ignored issues
show
Best Practice introduced by
The expression return $params; seems to be an array, but some of its elements' types (array<integer|string>[]) are incompatible with the return type of the parent method App\DataTables\BaseDataTable::getBuilderParameters of type array<string,string|arra...ring>[]|string[]|false>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
66
    }
67
68
    /**
69
     * Get columns.
70
     *
71
     * @return array
72
     */
73 View Code Duplication
    public function getColumns()
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...
74
    {
75
        return [
76
            'device_id' => [
77
                'title'       => trans('devices.label.hostname'),
78
            ],
79
            'program'      => [
80
                'title' => trans('general.text.program'),
81
            ],
82
            'msg'   => [
83
                'title' => trans('general.text.message'),
84
            ],
85
            'timestamp'  => [
86
                'title' => trans('general.text.timestamp'),
87
            ],
88
        ];
89
    }
90
91
    /**
92
     * Get filename for export.
93
     *
94
     * @return string
95
     */
96
    protected function filename()
97
    {
98
        return 'syslog';
99
    }
100
101
    /**
102
     * Get ajax url.
103
     *
104
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
105
     */
106
    public function getAjax()
107
    {
108
        return url('syslog');
109
    }
110
}
111