Completed
Push — master ( 697018...f872c4 )
by Freek
02:11
created

Check::succeeded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Check::hasStatus() 0 4 1
1
<?php
2
3
namespace Spatie\ServerMonitor\Models;
4
5
use Carbon\Carbon;
6
use Spatie\ServerMonitor\Models\Concerns\HandlesCheckResult;
7
use Spatie\ServerMonitor\Models\Concerns\HasProcess;
8
use Symfony\Component\Process\Process;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Builder;
11
use Spatie\ServerMonitor\Events\CheckFailed;
12
use Spatie\ServerMonitor\Events\CheckWarning;
13
use Spatie\ServerMonitor\Events\CheckRestored;
14
use Spatie\ServerMonitor\Events\CheckSucceeded;
15
use Spatie\ServerMonitor\Helpers\ConsoleOutput;
16
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
17
use Illuminate\Database\Eloquent\Relations\BelongsTo;
18
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition;
19
use Spatie\ServerMonitor\Models\Presenters\CheckPresenter;
20
use Spatie\ServerMonitor\Exceptions\InvalidCheckDefinition;
21
use Spatie\ServerMonitor\Models\Concerns\HasCustomProperties;
22
use Spatie\ServerMonitor\Models\Concerns\ThrottlesFailingNotifications;
23
24
class Check extends Model
25
{
26
    use CheckPresenter,
27
        HasCustomProperties,
28
        ThrottlesFailingNotifications,
29
        HasProcess,
30
        HandlesCheckResult;
31
32
    public $guarded = [];
33
34
    public $casts = [
35
        'custom_properties' => 'array',
36
        'process_output' => 'array',
37
    ];
38
39
    public $dates = [
40
        'last_ran_at', 'next_check_at',
41
    ];
42
43
    public function host(): BelongsTo
44
    {
45
        return $this->belongsTo(Host::class);
46
    }
47
48
    public function scopeHealthy($query)
49
    {
50
        return $query->where('status', CheckStatus::SUCCESS);
51
    }
52
53
    public function scopeUnhealthy($query)
54
    {
55
        return $query->where('status', '!=', CheckStatus::SUCCESS);
56
    }
57
58
    public function scopeEnabled(Builder $query)
59
    {
60
        $query->where('enabled', 1);
61
    }
62
63
    public function getAttribute($key)
64
    {
65
        if (array_key_exists($key, $this->attributes)) {
66
            return parent::getAttribute($key);
67
        }
68
69
        $properties = json_decode($this->attributes['custom_properties'], true);
70
71
        return array_get($properties, $key, parent::getAttribute($key));
72
    }
73
74
    public function shouldRun(): bool
75
    {
76
        if (! $this->enabled) {
0 ignored issues
show
Documentation introduced by
The property enabled 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
            return false;
78
        }
79
80
        if (is_null($this->last_ran_at)) {
0 ignored issues
show
Documentation introduced by
The property last_ran_at 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...
81
            return true;
82
        }
83
84
        return ! $this->last_ran_at
0 ignored issues
show
Documentation introduced by
The property last_ran_at 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...
85
            ->addMinutes($this->next_run_in_minutes)
0 ignored issues
show
Documentation introduced by
The property next_run_in_minutes 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...
86
            ->isFuture();
87
    }
88
89
    public function getDefinition(): CheckDefinition
90
    {
91
        if (! $definitionClass = config("server-monitor.checks.{$this->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...
92
            throw InvalidCheckDefinition::unknownCheckType($this);
93
        }
94
95
        if (! class_exists($definitionClass)) {
96
            throw InvalidCheckDefinition::definitionClassDoesNotExist($this, $definitionClass);
97
        }
98
99
        return app($definitionClass)->setCheck($this);
100
    }
101
102
    public function handleFinishedProcess()
103
    {
104
        $originalStatus = $this->status;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Spatie\ServerMonitor\Models\Check>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
105
106
        $this->getDefinition()->handleFinishedProcess($this->getProcess());
107
108
        $this->scheduleNextRun();
109
110
        if ($this->shouldFireRestoredEvent($originalStatus, $this->status)) {
0 ignored issues
show
Documentation introduced by
The property status 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...
111
            event(new CheckRestored($this));
112
        }
113
114
        return $this;
115
    }
116
117
    protected function shouldFireRestoredEvent(?string $originalStatus, ?string $newStatus)
118
    {
119
        if (! in_array($originalStatus, [CheckStatus::FAILED, CheckStatus::WARNING])) {
120
            return false;
121
        }
122
123
        return $newStatus === CheckStatus::SUCCESS;
124
    }
125
126
    protected function scheduleNextRun()
127
    {
128
        $this->last_ran_at = Carbon::now();
0 ignored issues
show
Documentation introduced by
The property last_ran_at does not exist on object<Spatie\ServerMonitor\Models\Check>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
129
130
        $this->next_run_in_minutes = $this->getDefinition()->performNextRunInMinutes();
0 ignored issues
show
Documentation introduced by
The property next_run_in_minutes does not exist on object<Spatie\ServerMonitor\Models\Check>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
131
        $this->save();
132
133
        return $this;
134
    }
135
136
    public function hasStatus(string $status): bool
137
    {
138
        return $this->status === $status;
0 ignored issues
show
Documentation introduced by
The property status 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...
139
    }
140
141
    public function storeProcessOutput(Process $process)
142
    {
143
        $this->process_output = [
0 ignored issues
show
Documentation introduced by
The property process_output does not exist on object<Spatie\ServerMonitor\Models\Check>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
144
            'output' => $process->getOutput(),
145
            'error_output' => $process->getErrorOutput(),
146
            'exit_code' => $process->getExitCode(),
147
            'exit_code_text' => $process->getExitCodeText(),
148
        ];
149
150
        $this->save();
151
    }
152
}
153