SetupResult.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\SetupCheck;
  8. use OCP\RichObjectStrings\IValidator;
  9. /**
  10. * @brief This class is used for storing the result of a setup check
  11. *
  12. * @since 28.0.0
  13. */
  14. class SetupResult implements \JsonSerializable {
  15. /**
  16. * @since 28.0.0
  17. */
  18. public const SUCCESS = 'success';
  19. /**
  20. * @since 28.0.0
  21. */
  22. public const INFO = 'info';
  23. /**
  24. * @since 28.0.0
  25. */
  26. public const WARNING = 'warning';
  27. /**
  28. * @since 28.0.0
  29. */
  30. public const ERROR = 'error';
  31. /**
  32. * @param string $name Translated name to display to the user
  33. */
  34. private ?string $name = null;
  35. /**
  36. * @brief Private constructor, use success()/info()/warning()/error() instead
  37. * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity
  38. * @param array<string, array<string, string>> $descriptionParameters
  39. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  40. * @since 28.0.0
  41. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  42. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  43. */
  44. private function __construct(
  45. private string $severity,
  46. private ?string $description = null,
  47. private ?array $descriptionParameters = null,
  48. private ?string $linkToDoc = null,
  49. ) {
  50. if ($description !== null && $descriptionParameters !== null) {
  51. \OCP\Server::get(IValidator::class)->validate($description, $descriptionParameters);
  52. }
  53. }
  54. /**
  55. * @brief Create a success result object
  56. * @param ?string $description Translated detailed description to display to the user
  57. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  58. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  59. * @since 28.0.0
  60. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  61. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  62. */
  63. public static function success(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  64. return new self(self::SUCCESS, $description, $descriptionParameters, $linkToDoc);
  65. }
  66. /**
  67. * @brief Create an info result object
  68. * @param ?string $description Translated detailed description to display to the user
  69. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  70. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  71. * @since 28.0.0
  72. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  73. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  74. */
  75. public static function info(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  76. return new self(self::INFO, $description, $descriptionParameters, $linkToDoc);
  77. }
  78. /**
  79. * @brief Create a warning result object
  80. * @param ?string $description Translated detailed description to display to the user
  81. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  82. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  83. * @since 28.0.0
  84. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  85. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  86. */
  87. public static function warning(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  88. return new self(self::WARNING, $description, $descriptionParameters, $linkToDoc);
  89. }
  90. /**
  91. * @brief Create an error result object
  92. * @param ?string $description Translated detailed description to display to the user
  93. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  94. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  95. * @since 28.0.0
  96. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  97. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  98. */
  99. public static function error(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  100. return new self(self::ERROR, $description, $descriptionParameters, $linkToDoc);
  101. }
  102. /**
  103. * @brief Get the severity for the setup check result
  104. *
  105. * @return self::SUCCESS|self::INFO|self::WARNING|self::ERROR
  106. * @since 28.0.0
  107. */
  108. public function getSeverity(): string {
  109. return $this->severity;
  110. }
  111. /**
  112. * @brief Get the description for the setup check result
  113. *
  114. * @since 28.0.0
  115. */
  116. public function getDescription(): ?string {
  117. return $this->description;
  118. }
  119. /**
  120. * @brief Get the description parameters for the setup check result
  121. *
  122. * If this returns null, description must not be treated as rich text
  123. *
  124. * @since 28.0.2
  125. */
  126. public function getDescriptionParameters(): ?array {
  127. return $this->descriptionParameters;
  128. }
  129. /**
  130. * @brief Get the name for the setup check
  131. *
  132. * @since 28.0.0
  133. */
  134. public function getName(): ?string {
  135. return $this->name;
  136. }
  137. /**
  138. * @brief Set the name from the setup check
  139. *
  140. * @since 28.0.0
  141. */
  142. public function setName(string $name): void {
  143. $this->name = $name;
  144. }
  145. /**
  146. * @brief Get a link to the doc for the explanation.
  147. *
  148. * @since 28.0.0
  149. */
  150. public function getLinkToDoc(): ?string {
  151. return $this->linkToDoc;
  152. }
  153. /**
  154. * @brief Get an array representation of the result for API responses
  155. *
  156. * @since 28.0.0
  157. */
  158. public function jsonSerialize(): array {
  159. return [
  160. 'name' => $this->name,
  161. 'severity' => $this->severity,
  162. 'description' => $this->description,
  163. 'descriptionParameters' => $this->descriptionParameters,
  164. 'linkToDoc' => $this->linkToDoc,
  165. ];
  166. }
  167. }