Completed
Push — master ( e167bf...f8a4e6 )
by Freek
13s
created

src/Commands/SyncFile.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\ServerMonitor\Commands;
4
5
use Spatie\ServerMonitor\Models\Host;
6
use Spatie\ServerMonitor\Models\Check;
7
8
class SyncFile extends BaseCommand
9
{
10
    protected $signature = 'server-monitor:sync-file
11
                            {path : Path to JSON file with hosts}
12
                            {--delete-missing : Delete hosts from the database which are not in the hosts file}';
13
14
    protected $description = 'One way sync hosts from JSON file to database';
15
16
    public function handle()
17
    {
18
        $json = file_get_contents($this->argument('path'));
19
20
        $hostsInFile = collect(json_decode($json, true));
21
22
        $this->createOrUpdateHostsFromFile($hostsInFile);
23
24
        $this->deleteMissingHosts($hostsInFile);
25
    }
26
27
    protected function createOrUpdateHostsFromFile($hostsInFile)
28
    {
29
        $hostsInFile->each(function ($hostAttributes) {
30
            $host = $this->createOrUpdateHost($hostAttributes);
31
32
            $this->syncChecks($host, $hostAttributes['checks']);
33
        });
34
35
        $this->info("Synced {$hostsInFile->count()} host(s) to database");
36
    }
37
38
    protected function deleteMissingHosts($hostsInFile)
39
    {
40
        if (! $this->option('delete-missing')) {
41
            return;
42
        }
43
44
        $this->determineHostModelClass()::all()
45
            ->reject(function (Host $host) use ($hostsInFile) {
46
                return $hostsInFile->contains('name', $host->name);
0 ignored issues
show
The property name does not exist on object<Spatie\ServerMonitor\Models\Host>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
            })
48
            ->each(function (Host $host) {
49
                $this->comment("Deleted host `{$host->name}` from database because was not found in hosts file");
0 ignored issues
show
The property name does not exist on object<Spatie\ServerMonitor\Models\Host>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
                $host->delete();
51
            });
52
    }
53
54
    protected function createOrUpdateHost(array $hostAttributes): Host
55
    {
56
        unset($hostAttributes['checks']);
57
58
        return tap($this->determineHostModelClass()::firstOrNew([
59
            'name' => $hostAttributes['name'],
60
        ]), function (Host $hostModel) use ($hostAttributes) {
61
            $hostModel
62
                ->fill($hostAttributes)
63
                ->save();
64
        });
65
    }
66
67
    protected function syncChecks(Host $host, array $checkTypes): Host
68
    {
69
        $this->removeChecksNotInArray($host, $checkTypes);
70
71
        $this->addChecksFromArray($host, $checkTypes);
72
73
        return $host;
74
    }
75
76
    protected function removeChecksNotInArray(Host $host, array $checkTypes)
77
    {
78
        $host->checks
79
            ->reject(function (Check $check) use ($checkTypes) {
80
                return in_array($check->type, $checkTypes);
81
            })
82
            ->each(function (Check $check) use ($host) {
83
                $this->comment("Deleted `{$check->type}` from host `{$host->name}` (not found in hosts file)");
0 ignored issues
show
The property type does not exist on object<Spatie\ServerMonitor\Models\Check>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
85
                return $check->delete();
86
            });
87
    }
88
89
    protected function addChecksFromArray(Host $host, array $checkTypes)
90
    {
91
        collect($checkTypes)
92
            ->reject(function (string $checkType) use ($host) {
93
                return $host->hasCheckType($checkType);
94
            })
95
            ->each(function (string $checkType) use ($host) {
96
                $host->checks()->create(['type' => $checkType]);
97
            });
98
    }
99
}
100