1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\Mapper; |
6
|
|
|
|
7
|
|
|
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus; |
8
|
|
|
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus\SimpleStatus; |
9
|
|
|
use Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency; |
10
|
|
|
use Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\Mapper; |
11
|
|
|
use Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\Mapper\Exception\UnableToMapDependency; |
12
|
|
|
use Cushon\HealthBundle\Message\Result\HealthCheck; |
13
|
|
|
use Generator; |
14
|
|
|
|
15
|
|
|
final class SimpleMapper implements Mapper |
16
|
|
|
{ |
17
|
|
|
public const KEY_NAME = 'name'; |
18
|
|
|
public const KEY_HEALTH = 'health'; |
19
|
|
|
public const KEY_INFO = 'info'; |
20
|
|
|
|
21
|
|
|
private string $name; |
22
|
|
|
private string $health; |
23
|
|
|
private string $info; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string $health |
28
|
|
|
* @param string $info |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $name = 'Name', string $health = 'Status', string $info = 'Info') |
31
|
|
|
{ |
32
|
|
|
$this->name = $name; |
33
|
|
|
$this->health = $health; |
34
|
|
|
$this->info = $info; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
public function getHealthCheckHeaders(HealthCheck $healthCheck): iterable |
41
|
|
|
{ |
42
|
|
|
yield from $this->yieldMap($this->name, $this->health, $this->info); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function mapDependencyStatus(DependencyStatus $dependencyStatus): iterable |
49
|
|
|
{ |
50
|
|
|
if (!$dependencyStatus instanceof SimpleStatus) { |
51
|
|
|
throw UnableToMapDependency::create($this, $dependencyStatus); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->mapSimpleDependency($dependencyStatus); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param SimpleStatus $simpleStatus |
59
|
|
|
* @return Generator<string, string, int, void> |
60
|
|
|
*/ |
61
|
|
|
private function mapSimpleDependency(SimpleStatus $simpleStatus): Generator |
62
|
|
|
{ |
63
|
|
|
$info = (string) $simpleStatus->getInfo(); |
64
|
|
|
yield from $this->yieldMap( |
65
|
|
|
$simpleStatus->getName(), |
66
|
|
|
$this->getStatusHealth($simpleStatus), |
67
|
|
|
$info |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @param string $health |
74
|
|
|
* @param string $info |
75
|
|
|
* @return iterable<string, string> |
76
|
|
|
*/ |
77
|
|
|
private function yieldMap(string $name, string $health, string $info): iterable |
78
|
|
|
{ |
79
|
|
|
yield self::KEY_NAME => $name; |
80
|
|
|
yield self::KEY_HEALTH => $health; |
81
|
|
|
yield self::KEY_INFO => $info; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param DependencyStatus $status |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private function getStatusHealth(DependencyStatus $status): string |
89
|
|
|
{ |
90
|
|
|
return ($status->isHealthy()) ? Dependency::STATUS_HEALTHY : Dependency::STATUS_UNHEALTHY; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|