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

UpdateDevice   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
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 52
rs 10
wmc 8
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 30 8
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=}';
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
        if ($this->option('list') || is_null($this->option())) {
56
            event(new ListDevices());
57
            echo "Sending Device List Event\n";
58
        } else {
59
            // Fire off an event, just randomly grabbing the first user for now
60
            if ($this->option('id') !== null) {
61
                if ($this->option('id') == 'all') {
62
                    $devices = Device::all();
63
                } else {
64
                    $devices = [Device::find($this->option('id'))];
65
                }
66
            } else {
67
                $devices = [Device::first()];
68
            }
69
70
            foreach ($devices as $device) {
71
                if ($this->option('status') !== null) {
72
                    $device->status = $this->option('status');
73
                }
74
75
                if ($this->option('uptime') !== null) {
76
                    $device->uptime = $this->option('uptime');
77
                }
78
79
                $device->save();
80
            }
81
        }
82
    }
83
}
84