Completed
Pull Request — master (#16)
by
unknown
02:50 queued 33s
created

SyncFile::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ServerMonitor\Commands;
4
5
use File;
6
use Spatie\ServerMonitor\Models\Host;
7
use Spatie\ServerMonitor\Models\Check;
8
9
class SyncFile extends BaseCommand
10
{
11
    protected $signature = 'server-monitor:sync-file
12
                            {filename : JSON file with hosts}
13
                            {--delete-missing : Delete hosts from the database that are not in the hosts file}';
14
15
    protected $description = 'One way sync hosts from JSON file to database';
16
17
    public function handle()
18
    {
19
        $json = File::get($this->argument('filename'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('filename') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Filesystem\Filesystem::get() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
20
21
        $hostsInFile = collect(json_decode($json, true));
22
23
        $this->updateOrCreateHosts($hostsInFile);
24
25
        $this->deleteMissingHosts($hostsInFile);
26
    }
27
28
    /**
29
     * @param $hostsInFile
30
     */
31
    protected function deleteMissingHosts($hostsInFile)
32
    {
33
        if ($this->option('delete-missing')) {
34
            Host::all()->each(function (Host $host) use ($hostsInFile) {
35
                if (! $hostsInFile->contains('name', $host->name)) {
0 ignored issues
show
Documentation introduced by
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...
36
                    $this->comment("Deleted host '{$host->name}' from database (was not found in hosts file)");
0 ignored issues
show
Documentation introduced by
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...
37
                    $host->delete();
38
                }
39
            });
40
        }
41
    }
42
43
    /**
44
     * @param $hostsInFile
45
     */
46
    protected function updateOrCreateHosts($hostsInFile)
47
    {
48
        $hostsInFile->each(function ($host) {
49
            $host = collect($host);
50
51
            $hostModel = Host::firstOrNew(['name' => $host['name']]);
52
53
            $hostModel
54
                ->fill($host->except('checks')->toArray())
55
                ->save();
56
57
            // Delete checks that were deleted from the file
58
            $hostModel->checks->each(function (Check $check) use ($host) {
59
                if (! in_array($check->type, $host['checks'])) {
0 ignored issues
show
Documentation introduced by
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...
60
                    $this->comment("Deleted '{$check->type}' from host '{$host['name']}' (not found in hosts file)");
0 ignored issues
show
Documentation introduced by
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...
61
                    $check->delete();
62
                }
63
            });
64
65
            // Add checks that do not exist in db
66
            foreach ($host['checks'] as $check) {
67
                if ($hostModel->checks->where('type', $check)->count() === 0) {
68
                    $hostModel->checks()->create(['type' => $check]);
69
                }
70
            }
71
        });
72
73
        $this->info("Synced {$hostsInFile->count()} host(s) to database");
74
    }
75
}
76