HealthService::getHealth()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 31
rs 9.4222
cc 5
nc 5
nop 1
1
<?php
2
namespace Health\Services;
3
4
use Health\Health;
5
use Health\HealthCheck;
6
use Health\Checks\ErrorCheck;
7
8
class HealthService
9
{
10
11
    /**
12
     *
13
     * @param array $checks
14
     * @return Health
15
     */
16
    public function getHealth(array $checks)
17
    {
18
        $health = new Health();
19
        $health->setState(HealthCheck::STATE_UP);
20
21
        foreach ($checks as $check) {
22
            $class = $check['class'] ?? null;
23
            $params = $check['params'] ?? [];
24
25
            if (! $class || ! class_exists($class)) {
26
27
                /** @var Health\Checks\HealthCheckInterface $healthCheck */
28
                $healthCheck = new ErrorCheck([
29
                    'message' => 'Health Check configuration error. Missing check class. - ' . $class
30
                ]);
31
                $checkResponse = $healthCheck->call();
32
            } else {
33
34
                /** @var Health\Checks\HealthCheckInterface $healthCheck */
35
                $healthCheck = new $class($params);
36
                $checkResponse = $healthCheck->call();
37
            }
38
39
            if ($checkResponse->getState() !== HealthCheck::STATE_UP) {
40
                $health->setState(HealthCheck::STATE_DOWN);
41
            }
42
43
            $health->setCheck($checkResponse);
44
        }
45
46
        return $health;
47
    }
48
}
49