ArpDataTable::ajax()   C
last analyzed

Complexity

Conditions 8
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 1
nop 0
dl 0
loc 28
ccs 0
cts 0
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * app/DataTables/General/ArpDataTable.php
4
 *
5
 * Datatable for arp search
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\IPv4;
30
use App\Models\General\IPv4Mac;
31
32
class ArpDataTable extends BaseDataTable
33
{
34
35
    /**
36
     * Display ajax response.
37
     *
38
     * @return \Illuminate\Http\JsonResponse
39
     */
40
    public function ajax()
41
    {
42
        return $this->datatables
43
            ->eloquent($this->query())
44
            ->editColumn('hostname', function ($data) {
45
                $hostname = is_null($data->device) ? trans('devices.text.deleted') : $data->device->hostname;
46
                return '<a href="'.url("devices/".$data->device_id).'">'.$hostname.'</a>';
47
            })
48
            ->editColumn('ifName', function ($data) {
49
                $ifName = is_null($data->ifName) ? trans('devices.text.deleted') : $data->ifName;
50
                return '<a href="'.url("devices/".$data->device_id."/ports/".$data->port_id).'">'.$ifName.'</a>';
51
            })
52
            ->addColumn('remote_device', function ($data) {
53
                $remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first();
54
                $remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id;
55
                $remote_hostname = empty($remote_device->port->device->hostname) ? '' : $remote_device->port->device->hostname;
56
                return '<a href="'.url("devices/".$remote_id).'">'.$remote_hostname.'</a>';
57
            })
58
            ->addColumn('remote_interface', function ($data) {
59
                $remote_device = IPv4::where('ipv4_addresses.ipv4_address', $data->ipv4_address)->with('port.device')->first();
60
                $remote_id = empty($remote_device->port->device->device_id) ? '' : $remote_device->port->device->device_id;
61
                $remote_port_id = empty($remote_device->port->port_id) ? '' : $remote_device->port->port_id;
62
                $remote_port = empty($remote_device->port->ifName) ? '' : $remote_device->port->ifName;
63
                return '<a href="'.url("devices/".$remote_id."/ports/".$remote_port_id).'">'.$remote_port.'</a>';
64
            })
65
            ->rawColumns(['hostname', 'ifName', 'remote_device', 'remote_interface'])
66
            ->make(true);
67
    }
68
69
    /**
70
     * Get the query object to be processed by datatables.
71
     *
72
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
73
     */
74
    public function query()
75
    {
76
        $data = IPv4Mac::join('ports', 'ports.port_id', '=', 'ipv4_mac.port_id')->join('devices', 'devices.device_id', '=', 'ports.device_id')->select('ipv4_mac.*', 'ports.*', 'devices.*');
77
        //FIXME We should use this once laravel-datatables supports it upstream $data = IPv4Mac::with('port.device')->select('ipv4_mac.*');
78
        return $this->applyScopes($data);
79
    }
80
81
    /**
82
     * Get columns.
83
     *
84
     * @return array
85
     */
86 1
    public function getColumns()
87
    {
88
        return [
89
            'hostname'  => [
90 1
                'title' => trans('devices.label.hostname'),
91 1
            ],
92
            'ifName'    => [
93 1
                'title' => trans('general.text.interface'),
94
            ],
95
            'mac_address' => [
96 1
                'title'   => trans('general.text.mac_address'),
97
            ],
98
            'ipv4_address' => [
99 1
                'title'    => trans('general.text.address'),
100
            ],
101
            'remote_device'  => [
102
                'searchable' => false,
103
            ],
104
            'remote_interface' => [
105
                'searchable'   => false,
106
            ]
107
        ];
108
    }
109
110
    /**
111
     * Get filename for export.
112
     *
113
     * @return string
114
     */
115
    protected function filename()
116
    {
117
        return 'arp';
118
    }
119
}
120