DefaultHealthCheck::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cushon\HealthBundle\Handler\CheckHealth;
6
7
use Cushon\HealthBundle\ApplicationHealth\Dependencies;
8
use Cushon\HealthBundle\ApplicationHealth\Exception\ApplicationHealthError;
9
use Cushon\HealthBundle\Handler\CheckHealth;
10
use Cushon\HealthBundle\Handler\CheckHealth\Exception\ApplicationHealthCheckFailure;
11
use Cushon\HealthBundle\Message\Query\HealthCheck as HealthCheckQuery;
12
use Cushon\HealthBundle\Message\Result\HealthCheck as HealthCheckResult;
13
14
final class DefaultHealthCheck implements CheckHealth
15
{
16
    private Dependencies $dependencies;
17
    private Logger $logger;
18
    private ResultFactory $resultFactory;
19
20
    /**
21
     * @param Dependencies $dependencies
22
     * @param Logger $logger
23
     * @param ResultFactory $resultFactory
24
     */
25
    public function __construct(Dependencies $dependencies, Logger $logger, ResultFactory $resultFactory)
26
    {
27
        $this->dependencies = $dependencies;
28
        $this->logger = $logger;
29
        $this->resultFactory = $resultFactory;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function __invoke(HealthCheckQuery $healthCheck): HealthCheckResult
36
    {
37
        $this->logger->begin($healthCheck);
38
        try {
39
            $health = $this->dependencies->check();
40
        } catch (ApplicationHealthError $exc) {
41
            throw $this->handleApplicationHealthError($exc);
42
        }
43
44
        $result = $this->resultFactory->fromHealth($health);
45
        $this->logger->complete($result);
46
47
        return $result;
48
    }
49
50
    /**
51
     * @param ApplicationHealthError $exc
52
     * @return ApplicationHealthCheckFailure
53
     */
54
    private function handleApplicationHealthError(ApplicationHealthError $exc): ApplicationHealthCheckFailure
55
    {
56
        $failure = ApplicationHealthCheckFailure::fromApplicationHealthError($exc);
57
        $this->logger->error($failure);
58
59
        return $failure;
60
    }
61
}
62