SetupResult.php 6.1 KB

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