• 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

95.83
/src/Result/MonitoringProviderResult.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "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 2 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\Result;
25

26
use InvalidArgumentException;
27
use JsonException;
28
use JsonSerializable;
29

30
/**
31
 * MonitoringProviderResult.
32
 *
33
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
34
 * @license GPL-3.0-or-later
35
 *
36
 * @phpstan-type JsonResult array{
37
 *     status: MonitoringStatus,
38
 *     error?: string,
39
 *     code?: int|string,
40
 *     info?: mixed,
41
 * }
42
 */
43
class MonitoringProviderResult implements JsonSerializable
44
{
45
    protected ?string $errorMessage = null;
46
    protected int|string|null $errorCode = null;
47
    protected mixed $statusInformation = null;
48

49
    /**
50
     * @param non-empty-string $name
51
     */
52
    public function __construct(
10✔
53
        protected string $name,
54
        protected bool $healthy,
55
    ) {}
10✔
56

57
    /**
58
     * @return non-empty-string
59
     */
60
    public function getName(): string
2✔
61
    {
62
        return $this->name;
2✔
63
    }
64

65
    /**
66
     * @param non-empty-string $name
67
     */
68
    public function setName(string $name): self
1✔
69
    {
70
        $this->name = $name;
1✔
71

72
        return $this;
1✔
73
    }
74

75
    public function isHealthy(): bool
4✔
76
    {
77
        return $this->healthy;
4✔
78
    }
79

80
    public function setHealthy(bool $healthy): self
3✔
81
    {
82
        $this->healthy = $healthy;
3✔
83

84
        return $this;
3✔
85
    }
86

87
    public function getErrorMessage(): ?string
1✔
88
    {
89
        return $this->errorMessage;
1✔
90
    }
91

92
    public function setErrorMessage(string $errorMessage): self
2✔
93
    {
94
        $this->errorMessage = $errorMessage;
2✔
95

96
        return $this;
2✔
97
    }
98

99
    public function getErrorCode(): int|string|null
1✔
100
    {
101
        return $this->errorCode;
1✔
102
    }
103

104
    public function setErrorCode(int|string $errorCode): self
2✔
105
    {
106
        $this->errorCode = $errorCode;
2✔
107

108
        return $this;
2✔
109
    }
110

111
    public function getStatusInformation(): mixed
1✔
112
    {
113
        return $this->statusInformation;
1✔
114
    }
115

116
    public function setStatusInformation(mixed $statusInformation): self
3✔
117
    {
118
        $this->statusInformation = $statusInformation;
3✔
119
        $this->validateStatusInformation();
3✔
120

121
        return $this;
2✔
122
    }
123

124
    public function getStatus(): MonitoringStatus
2✔
125
    {
126
        return $this->isHealthy() ? MonitoringStatus::Ok : MonitoringStatus::Error;
2✔
127
    }
128

129
    /**
130
     * @phpstan-return JsonResult
131
     */
132
    public function jsonSerialize(): array
1✔
133
    {
134
        $jsonArray = [
1✔
135
            'status' => $this->getStatus(),
1✔
136
        ];
1✔
137

138
        if (!$this->isHealthy()) {
1✔
139
            if (null !== $this->errorMessage) {
1✔
140
                $jsonArray['error'] = $this->errorMessage;
1✔
141
            }
142
            if (null !== $this->errorCode) {
1✔
143
                $jsonArray['code'] = $this->errorCode;
1✔
144
            }
145
        } elseif (null !== $this->statusInformation) {
1✔
146
            $jsonArray['info'] = $this->statusInformation;
1✔
147
        }
148

149
        return $jsonArray;
1✔
150
    }
151

152
    private function validateStatusInformation(): void
3✔
153
    {
154
        if (is_object($this->statusInformation) && !($this->statusInformation instanceof JsonSerializable)) {
3✔
155
            throw new InvalidArgumentException(sprintf('Status information must be JSON serializable and must therefore implement %s.', JsonSerializable::class), 1642411760);
1✔
156
        }
157

158
        try {
159
            json_encode($this->statusInformation, JSON_THROW_ON_ERROR);
2✔
NEW
160
        } catch (JsonException $exception) {
×
NEW
161
            throw new InvalidArgumentException(sprintf('Status information must be JSON serializable: %s', $exception->getMessage()), 1623144955);
×
162
        }
163
    }
164
}
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