Test Setup Failed
Pull Request — develop (#200)
by Tony
04:06
created

UpdateDevice   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 14
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 54 14
1
<?php
2
/**
3
 * UpdateDevice.php
4
 *
5
 * Little command for tweaking database for ui testing
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  2017 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Console\Commands;
27
28
use App\Events\ListDevices;
29
use App\Models\Device;
30
use Illuminate\Console\Command;
31
32
class UpdateDevice extends Command
33
{
34
    /**
35
     * The name and signature of the console command.
36
     *
37
     * @var string
38
     */
39
    protected $signature = 'device:update {--list} {--id=} {--status=} {--uptime=} {--pop} {--push} {--delete=}';
40
41
    /**
42
     * The console command description.
43
     *
44
     * @var string
45
     */
46
    protected $description = 'Set a device to up (1) or down (0)';
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return mixed
52
     */
53
    public function handle()
54
    {
55
        $delete = $this->option('delete');
56
        if ($this->option('pop') || $delete) {
57
            if ($delete) {
58
                $device = Device::find($delete);
59
            } else {
60
                $device = Device::orderBy('device_id', 'desc')->first();
61
            }
62
63
            $device->delete();
64
            return;
65
        }
66
67
        if ($this->option('list') || is_null($this->option())) {
68
            event(new ListDevices());
69
            echo "Sending Device List Event\n";
70
        } else {
71
            // Fire off an event, just randomly grabbing the first user for now
72
            $id = $this->option('id');
73
74
            if ($id == 'all') {
75
                $this->info('all');
76
                $devices = Device::all();
77
            } elseif ($this->option('push')) {
78
                $this->info('push');
79
                $devices = [new Device(['hostname' => 'Mockery'])];
80
            } elseif ($id === null) {
81
                $this->info('first');
82
                $devices = [Device::first()];
83
            } else {
84
                $this->info('findOrNew');
85
                $devices = [Device::findOrNew($id)];
86
            }
87
88
            /** @var Device $device */
89
            foreach ($devices as $device) {
90
                if ($this->option('status') !== null) {
91
                    $device->status = $this->option('status');
92
                }
93
94
                if ($this->option('uptime') !== null) {
95
                    $device->uptime = $this->option('uptime');
96
                }
97
98
                $device->save();
99
100
                if (empty($device->hostname) || $device->hostname == 'Mockery') {
101
                    $device->hostname = 'Mockery'.$device->device_id;
102
                    $device->save();
103
                }
104
            }
105
        }
106
    }
107
}
108