Completed
Push — laravel-5.4 ( 381f13...cdea23 )
by Tony
08:45
created

EventlogDataTable::filename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * app/DataTables/General/EventlogDataTable.php
4
 *
5
 * Datatable for eventlogs
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\Device;
30
use App\Models\General\Eventlog;
31
32
class EventlogDataTable extends BaseDataTable
33
{
34
35
    protected $device_id;
36
37
    /**
38
     * Display ajax response.
39
     *
40
     * @return \Illuminate\Http\JsonResponse
41
     */
42
    public function ajax()
43
    {
44
        return $this->datatables
45
            ->eloquent($this->query())
46
            ->addColumn('hostname', 'datatables.generic.hostname')
47
            ->rawColumns(['hostname'])
48
            ->make(true);
49
    }
50
51
    /**
52
     * Get the query object to be processed by datatables.
53
     *
54
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
55
     */
56
    public function query()
57
    {
58
        if (is_numeric($this->device_id)) {
59
            // eventlogs for a single device
60
            $eventlogs = Device::find($this->device_id)->eventlogs()->select('eventlog.*');
61
        } else {
62
            $eventlogs = Eventlog::with([
63
                'device' => function ($query) {
64
                    return $query->addSelect(['device_id', 'hostname']);
65
                },
66
            ])->select('eventlog.*');
67
        }
68
        return $this->applyScopes($eventlogs);
69
    }
70
71
    /**
72
     * Get columns.
73
     *
74
     * @return array
75
     */
76
    public function getColumns()
77
    {
78
        return [
79
            'hostname' => [
80
                'title'     => trans('devices.label.hostname'),
81
                'orderable' => false,
82
            ],
83
            'type'     => [
84
                'title' => trans('general.text.type'),
85
                'name'  => 'eventlog.type',
86
            ],
87
            'message'  => [
88
                'title' => trans('general.text.message'),
89
            ],
90
            'datetime' => [
91
                'title' => trans('general.text.timestamp'),
92
            ],
93
        ];
94
    }
95
96
    /**
97
     * Sort by timestamp descending
98
     *
99
     * @return array
100
     */
101
    public function getBuilderParameters()
102
    {
103
        $params = parent::getBuilderParameters();
104
        $params['order'] = [[3, 'desc']];
105
        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...
106
    }
107
108
    /**
109
     * Get filename for export.
110
     *
111
     * @return string
112
     */
113
    protected function filename()
114
    {
115
        return 'eventlog';
116
    }
117
118
    /**
119
     * Get ajax url.
120
     *
121
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
122
     */
123
    public function getAjax()
124
    {
125
        return url('eventlog?device_id=' . $this->device_id);
0 ignored issues
show
Bug Compatibility introduced by
The expression url('eventlog?device_id=' . $this->device_id); of type Illuminate\Contracts\Routing\UrlGenerator|string adds the type Illuminate\Contracts\Routing\UrlGenerator to the return on line 125 which is incompatible with the return type of the parent method App\DataTables\BaseDataTable::getAjax of type string.
Loading history...
126
    }
127
128
    public function forDevice($device_id)
129
    {
130
        $this->device_id = $device_id;
131
        return $this;
132
    }
133
}
134