These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use PragmaRX\Health\Service as HealthService; |
||
7 | |||
8 | class Commands |
||
9 | { |
||
10 | /** |
||
11 | * @var Service |
||
12 | */ |
||
13 | private $healthService; |
||
14 | |||
15 | /** |
||
16 | * Commands constructor. |
||
17 | * |
||
18 | * @param Service $healthService |
||
19 | */ |
||
20 | 10 | public function __construct(HealthService $healthService) |
|
21 | { |
||
22 | 10 | $this->healthService = $healthService; |
|
23 | 10 | } |
|
24 | |||
25 | /** |
||
26 | * @param $rows |
||
27 | * @return \Illuminate\Support\Collection|\IlluminateAgnostic\Arr\Support\Collection|\IlluminateAgnostic\Collection\Support\Collection|\IlluminateAgnostic\Str\Support\Collection|\Tightenco\Collect\Support\Collection|\Vanilla\Support\Collection |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | 1 | protected function getTargetsFomResources($resources) |
|
31 | { |
||
32 | 1 | $targets = collect(); |
|
33 | |||
34 | 1 | foreach ($resources as $resource) { |
|
35 | 1 | foreach ($resource->targets as $target) { |
|
36 | 1 | $targets->push($target); |
|
37 | } |
||
38 | } |
||
39 | |||
40 | 1 | return $targets; |
|
41 | } |
||
42 | |||
43 | 1 | private function normalizeMessage($message) |
|
44 | { |
||
45 | 1 | $message = str_replace("\n", '', $message); |
|
46 | 1 | $message = str_replace("\r", '', $message); |
|
47 | 1 | $message = str_replace("\t", ' ', $message); |
|
48 | 1 | $message = str_replace('<br>', ' ', $message); |
|
49 | 1 | $message = str_replace(' ', ' ', $message); |
|
50 | |||
51 | 1 | $message = wordwrap($message, 60); |
|
52 | |||
53 | 1 | return $message; |
|
54 | } |
||
55 | |||
56 | 1 | public function panel(Command $command = null) |
|
57 | { |
||
58 | 1 | $columns = ['Resource', 'State', 'Message']; |
|
59 | |||
60 | 1 | $rows = $this->getTargetsFomResources($this->healthService->health()) |
|
61 | ->map(function ($target) { |
||
62 | return [ |
||
63 | 1 | "{$target->resource->name} ({$target->display})", |
|
64 | 1 | $target->result->healthy |
|
65 | 1 | ? '<info>healthy</info>' |
|
66 | 1 | : '<fg=red>failing</fg=red>', |
|
67 | 1 | $this->normalizeMessage($target->result->errorMessage), |
|
68 | ]; |
||
69 | 1 | }) |
|
70 | 1 | ->toArray(); |
|
71 | |||
72 | 1 | $this->table($command, $columns, $rows); |
|
73 | 1 | } |
|
74 | |||
75 | 1 | public function check(Command $command = null) |
|
76 | { |
||
77 | 1 | $checker = $this->healthService->getSilentChecker(); |
|
78 | |||
79 | $errors = $this->getTargetsFomResources($checker()->filter(function ($resource) { |
||
80 | 1 | return ! $resource->isGlobal; |
|
81 | }))->reduce(function ($carry, $target) { |
||
82 | 1 | return $carry + ($target->result->healthy ? 0 : 1); |
|
83 | 1 | }, 0); |
|
84 | |||
85 | 1 | if ($errors) { |
|
86 | 1 | $this->error( |
|
87 | 1 | $command, |
|
88 | 1 | "Application needs attention, $errors ". |
|
89 | 1 | str_plural('resouce', $errors). |
|
90 | 1 | ' '. |
|
91 | 1 | ($errors > 1 ? 'are' : 'is'). |
|
92 | 1 | ' currently failing.' |
|
93 | ); |
||
94 | } else { |
||
95 | $this->info($command, 'Check completed with no errors.'); |
||
96 | } |
||
97 | 1 | } |
|
98 | |||
99 | /** |
||
100 | * Format input to textual table. |
||
101 | * |
||
102 | * @param Command|null $command |
||
103 | * @param $columns |
||
104 | * @param \Illuminate\Contracts\Support\Arrayable|array $rows |
||
105 | */ |
||
106 | 1 | private function table($command, $columns, $rows) |
|
107 | { |
||
108 | 1 | if ($command) { |
|
109 | $command->table($columns, $rows); |
||
110 | } |
||
111 | 1 | } |
|
112 | |||
113 | /** |
||
114 | * Write a string as information output. |
||
115 | * |
||
116 | * @param Command|null $command |
||
117 | * @param $string |
||
118 | */ |
||
119 | private function info($command, $string) |
||
120 | { |
||
121 | if ($command) { |
||
122 | $command->info($string); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Write a string as information output. |
||
128 | * |
||
129 | * @param Command|null $command |
||
130 | * @param $string |
||
131 | */ |
||
132 | 1 | private function error($command, $string) |
|
133 | { |
||
134 | 1 | if ($command) { |
|
135 | $command->error($string); |
||
136 | } |
||
137 | 1 | } |
|
138 | |||
139 | /** |
||
140 | * Write a string as information output. |
||
141 | * |
||
142 | * @param Command|null $command |
||
143 | * @param $string |
||
144 | */ |
||
145 | private function warn($command, $string) |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
146 | { |
||
147 | if ($command) { |
||
148 | $command->warn($string); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 |