StatusCheckerGroup   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 11
eloc 23
c 5
b 1
f 3
dl 0
loc 84
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getResults() 0 3 1
A addCheck() 0 3 1
A getTitle() 0 3 1
A check() 0 12 4
A hasErrors() 0 10 3
1
<?php
2
namespace BretRZaun\StatusPage;
3
4
use BretRZaun\StatusPage\Check\CheckInterface;
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
8
class StatusCheckerGroup implements LoggerAwareInterface
9
{
10
    use LoggerAwareTrait;
11
12
    /** @var string */
13
    protected $title;
14
15
    /** @var CheckInterface[] */
16
    protected $checks = [];
17
18
    /** @var Result[] */
19
    protected $results = [];
20
21
22
    /**
23
     * StatusCheckerGroup constructor.
24
     * @param string $title
25
     */
26
    public function __construct(string $title)
27
    {
28
        $this->title = $title;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getTitle(): string
35
    {
36
        return $this->title;
37
    }
38
39
    /**
40
     * Adds check to the group.
41
     *
42
     * @param CheckInterface $checker
43
     */
44
    public function addCheck(CheckInterface $checker): void
45
    {
46
        $this->checks[] = $checker;
47
    }
48
49
    /**
50
     * Runs all added checks.
51
     */
52
    public function check(): void
53
    {
54
        foreach ($this->checks as $checker) {
55
            $result = $checker->checkStatus();
56
            if ($this->logger) {
57
                if ($result->isSuccess()) {
58
                    $this->logger->info($result->getLabel().': OK', ['details' => $result->getDetails()]);
59
                } else {
60
                    $this->logger->alert($result->getLabel().': '.$result->getError(), ['details' => $result->getDetails()]);
61
                }
62
            }
63
            $this->results[] = $result;
64
        }
65
    }
66
67
    /**
68
     * Returns results of all run checks.
69
     *
70
     * @return Result[]
71
     */
72
    public function getResults(): array
73
    {
74
        return $this->results;
75
    }
76
77
    /**
78
     * Returns if there is an erroneous result in this group.
79
     *
80
     * @return bool
81
     */
82
    public function hasErrors(): bool
83
    {
84
        $error = false;
85
        foreach ($this->results as $result) {
86
            if (!$result->isSuccess()) {
87
                $error = true;
88
                break;
89
            }
90
        }
91
        return $error;
92
    }
93
}
94