DependencyRowFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\DependencyTable;
6
7
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;
8
use Cushon\HealthBundle\Formatter\Console\StyledOutput\Dependency\Mapper;
9
use Generator;
10
use Symfony\Component\Console\Helper\TableCell;
11
use Symfony\Component\Console\Helper\TableCellStyle;
12
13
final class DependencyRowFactory
14
{
15
    public const DEFAULT_COLOUR_HEALTHY = 'green';
16
    public const DEFAULT_COLOUR_UNHEALTHY = 'red';
17
18
    private string $healthyColour;
19
    private string $unhealthyColour;
20
    private Mapper $dependencyMapper;
21
22
    /**
23
     * @param string $healthyColour
24
     * @param string $unhealthyColour
25
     */
26
    public function __construct(
27
        Mapper $dependencyMapper,
28
        string $healthyColour = self::DEFAULT_COLOUR_HEALTHY,
29
        string $unhealthyColour = self::DEFAULT_COLOUR_UNHEALTHY
30
    ) {
31
        $this->dependencyMapper = $dependencyMapper;
32
        $this->healthyColour = $healthyColour;
33
        $this->unhealthyColour = $unhealthyColour;
34
    }
35
36
    /**
37
     * @param DependencyStatus $dependencyStatus
38
     * @return Generator
39
     */
40
    public function createDependencyRow(DependencyStatus $dependencyStatus): Generator
41
    {
42
        $tableCellStyle = $this->createTableCellStyle($dependencyStatus);
43
        /**
44
         * @var string $name
45
         * @var string $text
46
         */
47
        foreach ($this->getStatusData($dependencyStatus) as $name => $text) {
48
            yield $name => new TableCell($text, ['style' => $tableCellStyle]);
0 ignored issues
show
Bug introduced by
$text of type iterable is incompatible with the type string expected by parameter $value of Symfony\Component\Consol...ableCell::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            yield $name => new TableCell(/** @scrutinizer ignore-type */ $text, ['style' => $tableCellStyle]);
Loading history...
49
        }
50
    }
51
52
    /**
53
     * @param DependencyStatus $dependencyStatus
54
     * @return Generator
55
     */
56
    private function getStatusData(DependencyStatus $dependencyStatus): Generator
57
    {
58
        yield from $this->dependencyMapper->mapDependencyStatus($dependencyStatus);
59
    }
60
61
    /**
62
     * @param DependencyStatus $dependencyStatus
63
     * @return TableCellStyle
64
     */
65
    private function createTableCellStyle(DependencyStatus $dependencyStatus): TableCellStyle
66
    {
67
        return new TableCellStyle([
68
            'fg' => $dependencyStatus->isHealthy() ? $this->healthyColour : $this->unhealthyColour,
69
        ]);
70
    }
71
}
72