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

UpdateDevice::handle()   C

Complexity

Conditions 12
Paths 38

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
nc 38
nop 0
dl 0
loc 49
rs 5.1474
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 {--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
        // Fire off an event, just randomly grabbing the first user for now
68
        $id = $this->option('id');
69
70
        if ($id == 'all') {
71
            $this->info('all');
72
            $devices = Device::all();
73
        } elseif ($this->option('push')) {
74
            $this->info('push');
75
            $devices = [new Device(['hostname' => 'Mockery'])];
76
        } elseif ($id === null) {
77
            $this->info('first');
78
            $devices = [Device::first()];
79
        } else {
80
            $this->info('findOrNew');
81
            $devices = [Device::findOrNew($id)];
82
        }
83
84
        /** @var Device $device */
85
        foreach ($devices as $device) {
86
            if ($this->option('status') !== null) {
87
                $device->status = $this->option('status');
88
            }
89
90
            if ($this->option('uptime') !== null) {
91
                $device->uptime = $this->option('uptime');
92
            }
93
94
            $device->save();
95
96
            if (empty($device->hostname) || $device->hostname == 'Mockery') {
97
                $device->hostname = 'Mockery'.$device->device_id;
98
                $device->save();
99
            }
100
        }
101
    }
102
}
103