• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / monitoring / 12368095125

17 Dec 2024 07:09AM UTC coverage: 97.619%. First build
12368095125

push

github

eliashaeussler
Initial commit

164 of 168 new or added lines in 8 files covered. (97.62%)

164 of 168 relevant lines covered (97.62%)

2.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/Monitoring.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "cpsit/monitoring".
7
 *
8
 * Copyright (C) 2021 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\Monitoring;
25

26
use CPSIT\Monitoring\Provider\ExceptionAwareMonitoringProvider;
27
use CPSIT\Monitoring\Provider\MonitoringProvider;
28
use CPSIT\Monitoring\Provider\StatusInformationAwareMonitoringProvider;
29
use CPSIT\Monitoring\Result\MonitoringProviderResult;
30
use CPSIT\Monitoring\Result\MonitoringResult;
31
use GuzzleHttp\Exception\RequestException;
32

33
/**
34
 * Monitoring.
35
 *
36
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
37
 * @license GPL-3.0-or-later
38
 */
39
final class Monitoring
40
{
41
    private const ERROR_UNKNOWN = 'unknown';
42

43
    /**
44
     * @param MonitoringProvider[] $providers
45
     */
46
    public function checkHealth(array $providers): MonitoringResult
3✔
47
    {
48
        $result = new MonitoringResult();
3✔
49

50
        // Perform health checks
51
        foreach ($providers as $provider) {
3✔
52
            $name = $provider->getName();
2✔
53
            $healthy = $provider->isHealthy();
2✔
54

55
            $providerResult = new MonitoringProviderResult($name, $healthy);
2✔
56
            $result->addProviderResult($providerResult);
2✔
57

58
            if (!$healthy) {
2✔
59
                if ($provider instanceof ExceptionAwareMonitoringProvider) {
1✔
60
                    $providerResult->setErrorMessage($this->getErrorMessage($provider));
1✔
61
                    $providerResult->setErrorCode($this->getErrorCode($provider));
1✔
62
                }
63
            } elseif ($provider instanceof StatusInformationAwareMonitoringProvider) {
2✔
64
                $providerResult->setStatusInformation($provider->getStatusInformation());
1✔
65
            }
66
        }
67

68
        return $result;
3✔
69
    }
70

71
    private function getErrorMessage(ExceptionAwareMonitoringProvider $monitoringProvider): string
1✔
72
    {
73
        $lastException = $monitoringProvider->getLastException();
1✔
74

75
        return null !== $lastException ? $lastException->getMessage() : self::ERROR_UNKNOWN;
1✔
76
    }
77

78
    private function getErrorCode(ExceptionAwareMonitoringProvider $monitoringProvider): int|string
1✔
79
    {
80
        $lastException = $monitoringProvider->getLastException();
1✔
81

82
        if (null === $lastException) {
1✔
83
            return self::ERROR_UNKNOWN;
1✔
84
        }
85

86
        if ($lastException instanceof RequestException && ($response = $lastException->getResponse()) !== null) {
1✔
87
            return $response->getStatusCode();
1✔
88
        }
89

90
        $exceptionCode = $lastException->getCode();
1✔
91

92
        if (is_numeric($exceptionCode)) {
1✔
93
            return $exceptionCode;
1✔
94
        }
95

NEW
96
        return self::ERROR_UNKNOWN;
×
97
    }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc