1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\ApiHealth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Pbmedia\ApiHealth\Checkers\CheckerIsScheduled; |
7
|
|
|
use Pbmedia\ApiHealth\Checkers\Executor; |
8
|
|
|
|
9
|
|
|
class Runner |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* A collection of failed checkers. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Support\Collection |
15
|
|
|
*/ |
16
|
|
|
private $failed; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A collection of passing checkers. |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Support\Collection |
22
|
|
|
*/ |
23
|
|
|
private $passes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Boolean wether to use the scheduling. |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $scheduled = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Collection of executors. |
34
|
|
|
* |
35
|
|
|
* @var \Illuminate\Support\Collection |
36
|
|
|
*/ |
37
|
|
|
private $executors; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates an instance with the given executors. |
41
|
|
|
* |
42
|
|
|
* @param array|\Illuminate\Support\Collection $executors |
43
|
|
|
*/ |
44
|
|
|
public function __construct($executors) |
45
|
|
|
{ |
46
|
|
|
$this->executors = Collection::wrap($executors); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates an instance of this class with the configured checkers. |
51
|
|
|
* |
52
|
|
|
* @return \Pbmedia\ApiHealth\Runner |
53
|
|
|
*/ |
54
|
|
|
public static function fromConfig(): Runner |
55
|
|
|
{ |
56
|
|
|
return Collection::make(config('api-health.checkers')) |
57
|
|
|
->map(function ($checker): Executor { |
58
|
|
|
return Executor::make($checker); |
59
|
|
|
}) |
60
|
|
|
->pipe(function ($executors) { |
61
|
|
|
return new static($executors); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Disables the scheduling. |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function ignoreScheduling() |
71
|
|
|
{ |
72
|
|
|
$this->scheduled = false; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Handles all the checkers if that has not been done yet and |
79
|
|
|
* returns the collection of passing checkers. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Support\Collection |
82
|
|
|
*/ |
83
|
|
|
public function passes(): Collection |
84
|
|
|
{ |
85
|
|
|
if (!$this->passes) { |
86
|
|
|
$this->handle(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->passes; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Handles all the checkers if that has not been done yet and |
94
|
|
|
* returns the collection of failed checkers. |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Support\Collection |
97
|
|
|
*/ |
98
|
|
|
public function failed(): Collection |
99
|
|
|
{ |
100
|
|
|
if (!$this->failed) { |
101
|
|
|
$this->handle(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->failed; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Clears both collections and runs through all the configured checkers. |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function handle() |
113
|
|
|
{ |
114
|
|
|
$this->failed = new Collection; |
115
|
|
|
|
116
|
|
|
$this->passes = new Collection; |
117
|
|
|
|
118
|
|
|
$this->executors->filter(function (Executor $executor) { |
119
|
|
|
if (!$this->scheduled) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!$executor->getChecker() instanceof CheckerIsScheduled) { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $executor->getChecker()->isDue(); |
128
|
|
|
})->each(function (Executor $executor) { |
129
|
|
|
($executor->fails() ? $this->failed : $this->passes)->push($executor); |
130
|
|
|
}); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|