1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\DependencyTable; |
6
|
|
|
|
7
|
|
|
use Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\Mapper; |
8
|
|
|
use Cushon\HealthBundle\Message\Result\HealthCheck; |
9
|
|
|
use Generator; |
10
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
11
|
|
|
use Symfony\Component\Console\Helper\TableCellStyle; |
12
|
|
|
|
13
|
|
|
final class TableHeaderFactory |
14
|
|
|
{ |
15
|
|
|
public const DEFAULT_COLOUR_HEALTHY = 'green'; |
16
|
|
|
public const DEFAULT_COLOUR_UNHEALTHY = 'yellow'; |
17
|
|
|
|
18
|
|
|
private string $healthyColour; |
19
|
|
|
private string $unhealthyColour; |
20
|
|
|
|
21
|
|
|
private Mapper $dependencyMapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Mapper $dependencyMapper |
25
|
|
|
* @param string $healthyColour |
26
|
|
|
* @param string $unhealthyColour |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
Mapper $dependencyMapper, |
30
|
|
|
string $healthyColour = self::DEFAULT_COLOUR_HEALTHY, |
31
|
|
|
string $unhealthyColour = self::DEFAULT_COLOUR_UNHEALTHY |
32
|
|
|
) { |
33
|
|
|
$this->dependencyMapper = $dependencyMapper; |
34
|
|
|
$this->healthyColour = $healthyColour; |
35
|
|
|
$this->unhealthyColour = $unhealthyColour; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param HealthCheck $healthCheck |
40
|
|
|
* @return Generator<string, TableCell, int, void> |
41
|
|
|
*/ |
42
|
|
|
public function createHeaders(HealthCheck $healthCheck): Generator |
43
|
|
|
{ |
44
|
|
|
$tableCellStyle = $this->createTableCellStyle($healthCheck); |
45
|
|
|
|
46
|
|
|
foreach ($this->getHealthCheckHeaders($healthCheck) as $name => $text) { |
47
|
|
|
yield $name => new TableCell($text, ['style' => $tableCellStyle]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param HealthCheck $healthCheck |
53
|
|
|
* @return TableCellStyle |
54
|
|
|
*/ |
55
|
|
|
private function createTableCellStyle(HealthCheck $healthCheck): TableCellStyle |
56
|
|
|
{ |
57
|
|
|
return new TableCellStyle([ |
58
|
|
|
'fg' => $healthCheck->isHealthy() ? $this->healthyColour : $this->unhealthyColour, |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param HealthCheck $healthCheck |
64
|
|
|
* @return iterable<string, string> |
65
|
|
|
*/ |
66
|
|
|
private function getHealthCheckHeaders(HealthCheck $healthCheck): iterable |
67
|
|
|
{ |
68
|
|
|
return $this->dependencyMapper->getHealthCheckHeaders($healthCheck); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|