Issues (2963)

Commands/Traits/CompletesConfigArgument.php (1 issue)

1
<?php
2
/**
3
 * CompletesConfigArgument.php
4
 *
5
 * -Description-
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 <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2020 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Console\Commands\Traits;
27
28
use App\Console\Commands\InternalHttpRequest;
29
use App\Models\User;
30
use Illuminate\Support\Str;
31
use LibreNMS\Util\DynamicConfig;
32
use LibreNMS\Util\DynamicConfigItem;
33
34
trait CompletesConfigArgument
35
{
36
    public function completeArgument($name, $value, $previous)
37
    {
38
        if ($name == 'setting') {
39
            return (new DynamicConfig())->all()->keys()->filter(function ($setting) use ($value) {
40
                return Str::startsWith($setting, $value);
41
            })->toArray();
42
        } elseif ($name == 'value') {
43
            $config = (new DynamicConfig())->get($previous);
44
45
            switch ($config->getType()) {
46
                case 'select-dynamic':
47
                    return $this->suggestionsForSelectDynamic($config, $value);
48
                case 'select':
49
                    return $this->suggestionsForSelect($config, $value);
50
            }
51
        }
52
53
        return false;
54
    }
55
56
    protected function suggestionsForSelect(DynamicConfigItem $config, ?string $value): array
57
    {
58
        $options = collect($config['options']);
59
        $keyStartsWith = $options->filter(function ($description, $key) use ($value) {
60
            return Str::startsWith($key, $value);
61
        });
62
63
        // try to see if it matches a value (aka key)
64
        if ($keyStartsWith->isNotEmpty()) {
65
            return $keyStartsWith->keys()->all();
66
        }
67
68
        // last chance to try to find by the description
69
        return $options->filter(function ($description, $key) use ($value) {
0 ignored issues
show
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
        return $options->filter(function ($description, /** @scrutinizer ignore-unused */ $key) use ($value) {

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

Loading history...
70
            return Str::contains($description, $value);
71
        })->keys()->all();
72
    }
73
74
    protected function suggestionsForSelectDynamic(DynamicConfigItem $config, ?string $value): array
75
    {
76
        // need auth to make http request
77
        if ($admin = User::adminOnly()->first()) {
78
            $target = $config['options']['target'];
79
            $data = ['limit' => 10];
80
            if ($value) {
81
                $data['term'] = $value; // filter in sql
82
            }
83
84
            // make "http" request
85
            $results = (new InternalHttpRequest())
86
                ->actingAs($admin)
87
                ->json('GET', route("ajax.select.$target"), $data)->json('results');
88
89
            return array_column($results, 'id');
90
        }
91
92
        return [];
93
    }
94
}
95