Completed
Pull Request — master (#9)
by
unknown
02:22
created

ListChecks::healthyChecks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
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 Illuminate\Support\Collection;
6
use Spatie\ServerMonitor\Models\Host;
7
use Spatie\ServerMonitor\Models\Check;
8
9
class ListChecks extends BaseCommand
10
{
11
    protected $signature = 'server-monitor:list-checks
12
                            {--host= : Only show checks for certain host}
13
                            {--check= : Only show certain check type}';
14
15
    protected $description = 'List all checks for host(s)';
16
17
    public function handle()
18
    {
19
        if (Host::count() === 0) {
20
            return $this->info('There are no hosts configured');
21
        }
22
23
        $this->unhealthyChecks();
24
25
        $this->healthyChecks();
26
    }
27
28 View Code Duplication
    protected function unhealthyChecks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $this->tableWithTitle(
31
            'Unhealthy checks',
32
            ['Host', 'Check', 'Message', 'Last checked', 'Next check'],
33
            $this->getTableRows(Check::unhealthy()->get())
0 ignored issues
show
Bug introduced by
The method unhealthy() does not exist on Spatie\ServerMonitor\Models\Check. Did you maybe mean scopeUnhealthy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
34
        );
35
    }
36
37 View Code Duplication
    protected function healthyChecks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $this->tableWithTitle(
40
            'Healthy checks',
41
            ['Host', 'Check', 'Message', 'Last checked', 'Next check'],
42
            $this->getTableRows(Check::healthy()->get())
0 ignored issues
show
Bug introduced by
The method healthy() does not exist on Spatie\ServerMonitor\Models\Check. Did you maybe mean scopeHealthy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
        );
44
    }
45
46
    protected function tableWithTitle(string $title, array $header, array $rows)
47
    {
48
        if (count($rows) === 0) {
49
            return;
50
        }
51
52
        $this->info($title);
53
        $this->info('================');
54
        $this->table($header, $rows);
55
        $this->comment('');
56
    }
57
58
    protected function getTableRows(Collection $checks): array
59
    {
60 View Code Duplication
        if ($hostName = $this->option('host')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            $checks = $checks->filter(function (Check $check) use ($hostName) {
62
                return $check->host->name === $hostName;
0 ignored issues
show
Documentation introduced by
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...
63
            });
64
        }
65
66 View Code Duplication
        if ($checkType = $this->option('check')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $checks = $checks->filter(function (Check $check) use ($checkType) {
68
                return $check->type === $checkType;
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...
69
            });
70
        }
71
72
        return $checks
73
            ->map(function (Check $check) {
74
                return [
75
                    'name' => $check->host->name,
0 ignored issues
show
Documentation introduced by
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...
76
                    'check' => $check->type,
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...
77
                    'message' => $check->message,
0 ignored issues
show
Documentation introduced by
The property 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...
78
                    'last_checked' => $check->getLatestRunDiffAttribute(),
79
                    'next_check' => $check->getNextRunDiffAttribute(),
80
                ];
81
            })
82
            ->toArray();
83
    }
84
}
85