SimpleDependencies   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromIterable() 0 5 1
A __construct() 0 9 2
A check() 0 12 3
A checkDependencies() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cushon\HealthBundle\ApplicationHealth\Dependencies;
6
7
use Cushon\HealthBundle\ApplicationHealth\Dependencies;
8
use Cushon\HealthBundle\ApplicationHealth\Dependencies\Exception\NoDependencyChecks;
9
use Cushon\HealthBundle\ApplicationHealth\Dependencies\Simpledependencies\HealthReportFactory;
10
use Cushon\HealthBundle\ApplicationHealth\DependencyCheck;
11
use Cushon\HealthBundle\ApplicationHealth\HealthReport;
12
use Cushon\HealthBundle\ApplicationHealth\HealthReport\DependencyStatus;
13
use Ds\Set;
14
use Generator;
15
16
final class SimpleDependencies implements Dependencies
17
{
18
    /**
19
     * @var Set<DependencyCheck>
20
     */
21
    private Set $dependencyChecks;
22
    private HealthReportFactory $healthReportFactory;
23
24
    /**
25
     * @param HealthReportFactory $healthReportFactory
26
     * @param iterable<int, DependencyCheck> $dependencyChecks
27
     * @return static
28
     */
29
    public static function fromIterable(
30
        HealthReportFactory $healthReportFactory,
31
        iterable $dependencyChecks
32
    ): self {
33
        return new self($healthReportFactory, ...$dependencyChecks);
34
    }
35
36
    /**
37
     * @param HealthReportFactory $healthReportFactory
38
     * @param DependencyCheck ...$dependencyChecks
39
     */
40
    public function __construct(
41
        HealthReportFactory $healthReportFactory,
42
        DependencyCheck ...$dependencyChecks
43
    ) {
44
        if (!count($dependencyChecks)) {
45
            throw NoDependencyChecks::create();
46
        }
47
        $this->healthReportFactory = $healthReportFactory;
48
        $this->dependencyChecks = new Set($dependencyChecks);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function check(): HealthReport
55
    {
56
        $healthy = true;
57
        $dependencyStatuses = [];
58
        foreach ($this->checkDependencies() as $dependencyStatus) {
59
            $dependencyStatuses[] = $dependencyStatus;
60
            if (!$dependencyStatus->isHealthy()) {
61
                $healthy = false;
62
            }
63
        }
64
65
        return $this->healthReportFactory->generateReport($healthy, ...$dependencyStatuses);
66
    }
67
68
    /**
69
     * @return Generator
70
     * @psalm-return Generator<int, DependencyStatus, mixed, void>
71
     */
72
    private function checkDependencies(): Generator
73
    {
74
        foreach ($this->dependencyChecks as $dependencyCheck) {
75
            yield $dependencyCheck->check();
76
        }
77
    }
78
}
79