Test Failed
Push — develop ( 55b248...7d9761 )
by Tony
21:25
created

EventlogDataTable::forDevice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
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
    public function ajax()
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 1
        return $this->applyScopes($eventlogs);
65
    }
66 1
67 1
    /**
68 1
     * Get columns.
69
     *
70
     * @return array
71
     */
72
    public function getColumns()
73
    {
74
        return [
75
            'device.hostname' => [
76 1
                'title'       => trans('devices.label.hostname'),
77
            ],
78
            'type'      => [
79
                'title' => trans('general.text.type'),
80 1
                'name'  => 'eventlog.type',
81 1
            ],
82
            'message'   => [
83 1
                'title' => trans('general.text.message'),
84 1
            ],
85 1
            'datetime'  => [
86
                'title' => trans('general.text.timestamp'),
87 1
            ],
88 1
        ];
89
    }
90 1
91 1
    /**
92 1
     * 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);
109
    }
110 1
111
    public function forDevice($device_id)
112
    {
113 1
        $this->device_id = $device_id;
114 1
        return $this;
115
    }
116 1
117
}
118