1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Health; |
4
|
|
|
|
5
|
|
|
use PragmaRX\Health\Support\Cache; |
6
|
|
|
use PragmaRX\Health\Support\ResourceChecker; |
7
|
|
|
|
8
|
|
|
class Service |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ResourceChecker |
12
|
|
|
*/ |
13
|
|
|
private $resourceChecker; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Cache |
17
|
|
|
*/ |
18
|
|
|
private $cache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Service constructor. |
22
|
|
|
* |
23
|
|
|
* @param ResourceChecker $resourceChecker |
24
|
|
|
* @param Cache $cache |
25
|
|
|
*/ |
26
|
10 |
|
public function __construct(ResourceChecker $resourceChecker, Cache $cache) |
27
|
|
|
{ |
28
|
10 |
|
$this->resourceChecker = $resourceChecker; |
29
|
|
|
|
30
|
10 |
|
$this->cache = $cache; |
31
|
10 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check Resources. |
35
|
|
|
* |
36
|
|
|
* @param bool $force |
37
|
|
|
* @return \Illuminate\Support\Collection |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
8 |
|
public function checkResources($force = false) |
41
|
|
|
{ |
42
|
8 |
|
return $this->resourceChecker->checkResources($force); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Check one resource. |
47
|
|
|
* |
48
|
|
|
* @param $slug |
49
|
|
|
* @return array |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
1 |
|
public function checkResource($slug) |
53
|
|
|
{ |
54
|
1 |
|
return $this->resourceChecker->checkResource($slug); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get services health. |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
2 |
|
public function health() |
64
|
|
|
{ |
65
|
2 |
|
return $this->checkResources(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get resources. |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
1 |
|
public function getResources() |
75
|
|
|
{ |
76
|
1 |
|
return $this->resourceChecker->getResources(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get a silent checker and notifier closure. |
81
|
|
|
* |
82
|
|
|
* @return \Closure |
83
|
|
|
*/ |
84
|
1 |
|
public function getSilentChecker() |
85
|
|
|
{ |
86
|
|
|
return function () { |
87
|
1 |
|
return $this->checkResources(); |
88
|
1 |
|
}; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Make a string result of all resources. |
93
|
|
|
* |
94
|
|
|
* @param $string |
95
|
|
|
* @param $checkSystem |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function makeString($string, $checkSystem) |
99
|
|
|
{ |
100
|
|
|
return |
101
|
|
|
$string. |
102
|
|
|
($checkSystem |
103
|
|
|
? config('health.string.ok') |
104
|
|
|
: config('health.string.fail')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check and get a resource. |
109
|
|
|
* |
110
|
|
|
* @param $name |
111
|
|
|
* @return mixed |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
1 |
|
public function resource($slug) |
115
|
|
|
{ |
116
|
1 |
|
return $this->checkResource($slug); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the action. |
121
|
|
|
* |
122
|
|
|
* @param $action |
123
|
|
|
*/ |
124
|
1 |
|
public function setAction($action) |
125
|
|
|
{ |
126
|
1 |
|
$this->resourceChecker->setCurrentAction($action); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
* @throws \Exception |
132
|
|
|
*/ |
133
|
|
|
public function string() |
134
|
|
|
{ |
135
|
|
|
return collect($this->health())->reduce(function ($current, $resource) { |
136
|
|
|
return |
137
|
|
|
$current. |
138
|
|
|
($current ? config('health.string.glue') : ''). |
139
|
|
|
$this->makeString( |
140
|
|
|
$resource->abbreviation, |
141
|
|
|
$resource->isHealthy() |
142
|
|
|
); |
143
|
|
|
}); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|