HealthService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
c 3
b 0
f 0
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getHealth() 0 31 5
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