Completed
Push — master ( ffb5ad...fa2375 )
by Freek
02:04
created

CheckDefinition   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 76
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCheck() 0 6 1
A handleFinishedProcess() 0 16 3
handleSuccessfulProcess() 0 1 ?
A handleFailedProcess() 0 4 1
A throttleFailingNotificationsForMinutes() 0 4 1
A performNextRunInMinutes() 0 8 2
A timeoutInSeconds() 0 4 1
A command() 0 4 1
1
<?php
2
3
namespace Spatie\ServerMonitor\CheckDefinitions;
4
5
use Exception;
6
use Spatie\ServerMonitor\Models\Check;
7
use Symfony\Component\Process\Process;
8
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
9
10
abstract class CheckDefinition
11
{
12
    /** @var \Spatie\ServerMonitor\Models\Check */
13
    protected $check;
14
15
    /**
16
     * @param \Spatie\ServerMonitor\Models\Check $check
17
     *
18
     * @return $this
19
     */
20
    public function setCheck(Check $check)
21
    {
22
        $this->check = $check;
23
24
        return $this;
25
    }
26
27
    public function command(): string
28
    {
29
        return $this->command;
0 ignored issues
show
Bug introduced by
The property command does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
    }
31
32
    public function handleFinishedProcess(Process $process)
33
    {
34
        $this->check->storeProcessOutput($process);
35
36
        try {
37
            if (! $process->isSuccessful()) {
38
                $this->handleFailedProcess($process);
39
40
                return;
41
            }
42
43
            $this->handleSuccessfulProcess($process);
44
        } catch (Exception $exception) {
45
            $this->check->failed('Exception occurred: '.$exception->getMessage());
46
        }
47
    }
48
49
    abstract public function handleSuccessfulProcess(Process $process);
50
51
    public function handleFailedProcess(Process $process)
52
    {
53
        $this->check->failed("failed to run: {$process->getErrorOutput()}");
54
    }
55
56
    /**
57
     * When a check is emitting a warning or is failing, a notification will only
58
     * be sent once in given amount of minutes.
59
     *
60
     * @return int
61
     */
62
    public function throttleFailingNotificationsForMinutes(): int
63
    {
64
        return config('server-monitor.notifications.throttle_failing_notifications_for_minutes');
65
    }
66
67
    public function performNextRunInMinutes(): int
68
    {
69
        if ($this->check->hasStatus(CheckStatus::SUCCESS)) {
70
            return 10;
71
        }
72
73
        return 0;
74
    }
75
76
    /**
77
     * The amount of seconds that check may run.
78
     *
79
     * @return int
80
     */
81
    public function timeoutInSeconds(): int
82
    {
83
        return 10;
84
    }
85
}
86