AddHost::handle()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 0
dl 0
loc 45
rs 8.8888
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Commands;
4
5
use InvalidArgumentException;
6
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
7
8
class AddHost extends BaseCommand
9
{
10
    protected $signature = 'server-monitor:add-host';
11
12
    protected $description = 'Add a host';
13
14
    public static $allChecksLabel = '<all checks>';
15
16
    public function handle()
17
    {
18
        $this->info("Let's add a host!");
19
20
        $hostName = $this->ask('What is the name of the host');
21
22
        $sshUser = $this->confirm('Should a custom ssh user be used?')
23
            ? $this->ask('Which user?')
24
            : null;
25
26
        $port = $this->confirm('Should a custom port be used?')
27
            ? $this->ask('Which port?')
28
            : null;
29
30
        $ip = $this->confirm('Should a specific ip address be used?')
31
            ? $this->ask('Which ip address?')
32
            : null;
33
34
        $checkNames = array_merge([static::$allChecksLabel], $this->getAllCheckNames());
35
36
        $chosenChecks = $this->choice('Which checks should be performed?', $checkNames, 0, null, true);
37
38
        $chosenChecks = $this->determineChecks($chosenChecks, $checkNames);
0 ignored issues
show
Bug introduced by
It seems like $chosenChecks can also be of type string; however, Spatie\ServerMonitor\Com...Host::determineChecks() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
40
        if ($this->determineHostModelClass()::where('name', $hostName)->first()) {
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->determineHostModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
41
            throw new InvalidArgumentException("Host `{$hostName}` already exists");
42
        }
43
44
        $this->determineHostModelClass()::create([
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->determineHostModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
            'name' => $hostName,
46
            'ssh_user' => $sshUser,
47
            'port' => $port,
48
            'ip' => $ip,
49
        ])->checks()->saveMany(collect($chosenChecks)->map(function (string $checkName) {
50
            $checkModel = $this->determineCheckModelClass();
51
52
            return new $checkModel([
53
                'type' => $checkName,
54
                'status' => CheckStatus::NOT_YET_CHECKED,
55
                'custom_properties' => [],
56
            ]);
57
        }));
58
59
        $this->info("Host `{$hostName}` added");
60
    }
61
62
    protected function determineChecks(array $chosenChecks, array $checkNames): array
0 ignored issues
show
Unused Code introduced by
The parameter $checkNames is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        if (in_array(static::$allChecksLabel, $chosenChecks)) {
65
            return $this->getAllCheckNames();
66
        }
67
68
        return array_diff($chosenChecks, [static::$allChecksLabel]);
69
    }
70
71
    protected function getAllCheckNames(): array
72
    {
73
        return array_keys(config('server-monitor.checks'));
74
    }
75
}
76