DumpChecks::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Commands;
4
5
use Spatie\ServerMonitor\HostRepository;
6
use Spatie\ServerMonitor\Models\Check;
7
use Spatie\ServerMonitor\Models\Host;
8
9
class DumpChecks extends BaseCommand
10
{
11
    protected $signature = 'server-monitor:dump-checks
12
                            {path : Path to json file}';
13
14
    protected $description = 'Dump current configuration to json file';
15
16
    public function handle()
17
    {
18
        $jsonEncodedHosts = json_encode($this->getHostsWithChecks(), JSON_PRETTY_PRINT).PHP_EOL;
19
20
        file_put_contents($this->argument('path'), $jsonEncodedHosts);
21
    }
22
23
    protected function getHostsWithChecks(): array
24
    {
25
        return HostRepository::all()
26
            ->map(function (Host $host) {
27
                return array_filter([
28
                    'name' => $host->name,
29
                    'ssh_user' => $host->ssh_user,
30
                    'port' => $host->port,
31
                    'ip' => $host->ip,
32
                    'checks' => $host->checks
33
                        ->map(
34
                            function (Check $check) {
35
                                return $check->type;
36
                            })
37
                        ->toArray(),
38
                ]);
39
            })
40
            ->toArray();
41
    }
42
}
43