aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/SetupCheck/SetupResult.php
blob: 7d64b838087ae784de46e654c33bda3230f9cfe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace OCP\SetupCheck;

use OCP\RichObjectStrings\IValidator;

/**
 * @brief This class is used for storing the result of a setup check
 *
 * @since 28.0.0
 */
class SetupResult implements \JsonSerializable {
	/**
	 * @since 28.0.0
	 */
	public const SUCCESS = 'success';

	/**
	 * @since 28.0.0
	 */
	public const INFO = 'info';

	/**
	 * @since 28.0.0
	 */
	public const WARNING = 'warning';

	/**
	 * @since 28.0.0
	 */
	public const ERROR = 'error';

	/**
	 * @param string $name Translated name to display to the user
	 */
	private ?string $name = null;

	/**
	 * @brief Private constructor, use success()/info()/warning()/error() instead
	 * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity
	 * @param array<string, array<string, string>> $descriptionParameters
	 * @throws \OCP\RichObjectStrings\InvalidObjectExeption
	 * @since 28.0.0
	 * @since 28.0.2 Optional parameter ?array $descriptionParameters
	 * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
	 */
	private function __construct(
		private string $severity,
		private ?string $description = null,
		private ?array $descriptionParameters = null,
		private ?string $linkToDoc = null,
	) {
		if ($description !== null && $descriptionParameters !== null) {
			\OCP\Server::get(IValidator::class)->validate($description, $descriptionParameters);
		}
	}

	/**
	 * @brief Create a success result object
	 * @param ?string $description Translated detailed description to display to the user
	 * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
	 * @throws \OCP\RichObjectStrings\InvalidObjectExeption
	 * @since 28.0.0
	 * @since 28.0.2 Optional parameter ?array $descriptionParameters
	 * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
	 */
	public static function success(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
		return new self(self::SUCCESS, $description, $descriptionParameters, $linkToDoc);
	}

	/**
	 * @brief Create an info result object
	 * @param ?string $description Translated detailed description to display to the user
	 * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
	 * @throws \OCP\RichObjectStrings\InvalidObjectExeption
	 * @since 28.0.0
	 * @since 28.0.2 Optional parameter ?array $descriptionParameters
	 * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
	 */
	public static function info(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
		return new self(self::INFO, $description, $descriptionParameters, $linkToDoc);
	}

	/**
	 * @brief Create a warning result object
	 * @param ?string $description Translated detailed description to display to the user
	 * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
	 * @throws \OCP\RichObjectStrings\InvalidObjectExeption
	 * @since 28.0.0
	 * @since 28.0.2 Optional parameter ?array $descriptionParameters
	 * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
	 */
	public static function warning(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
		return new self(self::WARNING, $description, $descriptionParameters, $linkToDoc);
	}

	/**
	 * @brief Create an error result object
	 * @param ?string $description Translated detailed description to display to the user
	 * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
	 * @throws \OCP\RichObjectStrings\InvalidObjectExeption
	 * @since 28.0.0
	 * @since 28.0.2 Optional parameter ?array $descriptionParameters
	 * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
	 */
	public static function error(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
		return new self(self::ERROR, $description, $descriptionParameters, $linkToDoc);
	}

	/**
	 * @brief Get the severity for the setup check result
	 *
	 * @return self::SUCCESS|self::INFO|self::WARNING|self::ERROR
	 * @since 28.0.0
	 */
	public function getSeverity(): string {
		return $this->severity;
	}

	/**
	 * @brief Get the description for the setup check result
	 *
	 * @since 28.0.0
	 */
	public function getDescription(): ?string {
		return $this->description;
	}

	/**
	 * @brief Get the description parameters for the setup check result
	 *
	 * If this returns null, description must not be treated as rich text
	 *
	 * @since 28.0.2
	 */
	public function getDescriptionParameters(): ?array {
		return $this->descriptionParameters;
	}

	/**
	 * @brief Get the name for the setup check
	 *
	 * @since 28.0.0
	 */
	public function getName(): ?string {
		return $this->name;
	}

	/**
	 * @brief Set the name from the setup check
	 *
	 * @since 28.0.0
	 */
	public function setName(string $name): void {
		$this->name = $name;
	}

	/**
	 * @brief Get a link to the doc for the explanation.
	 *
	 * @since 28.0.0
	 */
	public function getLinkToDoc(): ?string {
		return $this->linkToDoc;
	}

	/**
	 * @brief Get an array representation of the result for API responses
	 *
	 * @since 28.0.0
	 */
	public function jsonSerialize(): array {
		return [
			'name' => $this->name,
			'severity' => $this->severity,
			'description' => $this->description,
			'descriptionParameters' => $this->descriptionParameters,
			'linkToDoc' => $this->linkToDoc,
		];
	}
}