Completed
Push — psr2 ( d57791 )
by Tony
09:39
created

EventlogDataTable::forDevice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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\General\Eventlog;
30
31
class EventlogDataTable extends BaseDataTable
32
{
33
34
    protected $device_id;
35
36
    /**
37
     * Display ajax response.
38
     *
39
     * @return \Illuminate\Http\JsonResponse
40
     */
41 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...
42
    {
43
        return $this->datatables
44
            ->eloquent($this->query())
45
            ->editColumn('device.hostname', function ($eventlog) {
46
                $hostname = is_null($eventlog->device) ? trans('devices.text.deleted') : $eventlog->device->hostname;
47
                return '<a href="'.url("devices/".$eventlog->device_id).'">'.$hostname.'</a>';
48
            })
49
            ->make(true);
50
    }
51
52
    /**
53
     * Get the query object to be processed by datatables.
54
     *
55
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
56
     */
57
    public function query()
58
    {
59
        if (is_numeric($this->device_id)) {
60
            $eventlogs = Eventlog::with('device')->where('eventlog.device_id', $this->device_id)->select('eventlog.*');
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
61
        } else {
62
            $eventlogs = Eventlog::with('device')->select('eventlog.*');
63
        }
64
        return $this->applyScopes($eventlogs);
65
    }
66
67
    /**
68
     * Get columns.
69
     *
70
     * @return array
71
     */
72 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...
73
    {
74
        return [
75
            'device.hostname' => [
76
                'title'       => trans('devices.label.hostname'),
77
            ],
78
            'type'      => [
79
                'title' => trans('general.text.type'),
80
                'name'  => 'eventlog.type',
81
            ],
82
            'message'   => [
83
                'title' => trans('general.text.message'),
84
            ],
85
            'datetime'  => [
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 'eventlog';
99
    }
100
101
    /**
102
     * Get ajax url.
103
     *
104
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
105
     */
106
    public function getAjax()
107
    {
108
        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 108 which is incompatible with the return type of the parent method App\DataTables\BaseDataTable::getAjax of type string.
Loading history...
109
    }
110
111
    public function forDevice($device_id)
112
    {
113
        $this->device_id = $device_id;
114
        return $this;
115
    }
116
}
117