Status::getEndpointStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Infinitypaul\LaravelUptime\Commands;
4
5
use Illuminate\Console\Command;
6
use Infinitypaul\LaravelUptime\Commands\Traits\CanForce;
7
use Infinitypaul\LaravelUptime\Endpoint;
8
9
class Status extends Command
10
{
11
    use CanForce;
12
13
    protected $client;
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'uptime:status {--F|force}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Display The Status Of All Endpoint In A Table';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return mixed
32
     */
33
    public function handle()
34
    {
35
        if ($this->isForced()) {
36
            $this->call('uptime:run', ['--force'=> true]);
37
        }
38
        $headers = ['ID', 'URI', 'Frequency', 'Last Checked', 'Human Date', 'Status', 'Response Code'];
39
40
        Endpoint::with('statuses')->chunk(100, function ($endpoints) use ($headers) {
41
            $this->table($headers, $endpoints->map(function ($endpoint) {
42
                return array_merge(
43
                    $endpoint->only(['id', 'uri', 'frequency']),
44
                    $endpoint->status ? $this->getEndpointStatus($endpoint) : []
45
                );
46
            })->toArray()
47
            );
48
        });
49
    }
50
51
    protected function getEndpointStatus(Endpoint $endpoint)
52
    {
53
        return [
54
            'created_at' => $endpoint->status->created_at,
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Infinitypaul\LaravelUptime\Endpoint>. 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...
55
            'human_date' => $endpoint->status->created_at->diffForHumans(),
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Infinitypaul\LaravelUptime\Endpoint>. 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...
56
            'status' => $this->formatStatus($endpoint->status),
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Infinitypaul\LaravelUptime\Endpoint>. 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...
57
            'status_code' => $endpoint->status->status_code,
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Infinitypaul\LaravelUptime\Endpoint>. 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...
58
        ];
59
    }
60
61
    protected function formatStatus($status)
62
    {
63
        if ($status->isDown()) {
64
            return '<error>Down</error>';
65
        }
66
67
        return '<bg=green;fg=black>Up</>';
68
    }
69
}
70