Completed
Push — master ( b7fed8...fcc65d )
by Freek
10s
created

src/Commands/ListChecks.php (6 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 Illuminate\Support\Collection;
6
use Spatie\ServerMonitor\Models\Check;
7
8
class ListChecks extends BaseCommand
9
{
10
    protected $signature = 'server-monitor:list-checks
11
                            {--host= : Only show checks for certain host}
12
                            {--check= : Only show certain check type}';
13
14
    protected $description = 'List all checks for host(s)';
15
16
    public function handle()
17
    {
18
        if ($this->determineHostModelClass()::count() === 0) {
19
            return $this->info('There are no hosts configured');
20
        }
21
22
        $this->unhealthyChecks();
23
24
        $this->healthyChecks();
25
    }
26
27 View Code Duplication
    protected function unhealthyChecks()
28
    {
29
        $this->tableWithTitle(
30
            'Unhealthy checks',
31
            ['Host', 'Check', 'Status', 'Message', 'Last checked', 'Next check'],
32
            $this->getTableRows($this->determineCheckModelClass()::unhealthy()->get())
33
        );
34
    }
35
36 View Code Duplication
    protected function healthyChecks()
37
    {
38
        $this->tableWithTitle(
39
            'Healthy checks',
40
            ['Host', 'Check', 'Message', 'Status', 'Last checked', 'Next check'],
41
            $this->getTableRows(self::determineCheckModelClass()::healthy()->get())
42
        );
43
    }
44
45
    protected function tableWithTitle(string $title, array $header, array $rows)
46
    {
47
        if (count($rows) === 0) {
48
            return;
49
        }
50
51
        $this->info($title);
52
        $this->info('================');
53
        $this->table($header, $rows);
54
        $this->comment('');
55
    }
56
57
    protected function getTableRows(Collection $checks): array
58
    {
59 View Code Duplication
        if ($hostName = $this->option('host')) {
60
            $checks = $checks->filter(function (Check $check) use ($hostName) {
61
                return $check->host->name === $hostName;
0 ignored issues
show
The property host 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...
62
            });
63
        }
64
65 View Code Duplication
        if ($checkType = $this->option('check')) {
66
            $checks = $checks->filter(function (Check $check) use ($checkType) {
67
                return $check->type === $checkType;
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...
68
            });
69
        }
70
71
        return $checks
72
            ->map(function (Check $check) {
73
                return [
74
                    'name' => $check->host->name,
0 ignored issues
show
The property host 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...
75
                    'check' => $check->type,
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...
76
                    'last_run_message' => $check->last_run_message,
0 ignored issues
show
The property last_run_message 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...
77
                    'status' => $check->status_as_emoji,
0 ignored issues
show
The property status_as_emoji 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...
78
                    'last_checked' => $check->getLatestRunDiffAttribute(),
79
                    'next_check' => $check->getNextRunDiffAttribute(),
80
                ];
81
            })
82
            ->toArray();
83
    }
84
}
85