SetupResult.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  33. * @since 28.0.0
  34. */
  35. public const SUCCESS = 'success';
  36. /**
  37. * @since 28.0.0
  38. */
  39. public const INFO = 'info';
  40. /**
  41. * @since 28.0.0
  42. */
  43. public const WARNING = 'warning';
  44. /**
  45. * @since 28.0.0
  46. */
  47. public const ERROR = 'error';
  48. /**
  49. * @param string $name Translated name to display to the user
  50. */
  51. private ?string $name = null;
  52. /**
  53. * @brief Private constructor, use success()/info()/warning()/error() instead
  54. * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity
  55. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  56. * @since 28.0.0
  57. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  58. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  59. */
  60. private function __construct(
  61. private string $severity,
  62. private ?string $description = null,
  63. private ?array $descriptionParameters = null,
  64. private ?string $linkToDoc = null,
  65. ) {
  66. if ($description !== null && $descriptionParameters !== null) {
  67. \OCP\Server::get(IValidator::class)->validate($description, $descriptionParameters);
  68. }
  69. }
  70. /**
  71. * @brief Create a success 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 success(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  80. return new self(self::SUCCESS, $description, $descriptionParameters, $linkToDoc);
  81. }
  82. /**
  83. * @brief Create an info 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 info(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  92. return new self(self::INFO, $description, $descriptionParameters, $linkToDoc);
  93. }
  94. /**
  95. * @brief Create a warning 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 warning(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  104. return new self(self::WARNING, $description, $descriptionParameters, $linkToDoc);
  105. }
  106. /**
  107. * @brief Create an error result object
  108. * @param ?string $description Translated detailed description to display to the user
  109. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  110. * @throws \OCP\RichObjectStrings\InvalidObjectExeption
  111. * @since 28.0.0
  112. * @since 28.0.2 Optional parameter ?array $descriptionParameters
  113. * @since 28.0.2 throws \OCP\RichObjectStrings\InvalidObjectExeption
  114. */
  115. public static function error(?string $description = null, ?string $linkToDoc = null, ?array $descriptionParameters = null): self {
  116. return new self(self::ERROR, $description, $descriptionParameters, $linkToDoc);
  117. }
  118. /**
  119. * @brief Get the severity for the setup check result
  120. *
  121. * @return self::SUCCESS|self::INFO|self::WARNING|self::ERROR
  122. * @since 28.0.0
  123. */
  124. public function getSeverity(): string {
  125. return $this->severity;
  126. }
  127. /**
  128. * @brief Get the description for the setup check result
  129. *
  130. * @since 28.0.0
  131. */
  132. public function getDescription(): ?string {
  133. return $this->description;
  134. }
  135. /**
  136. * @brief Get the description parameters for the setup check result
  137. *
  138. * If this returns null, description must not be treated as rich text
  139. *
  140. * @since 28.0.2
  141. */
  142. public function getDescriptionParameters(): ?array {
  143. return $this->descriptionParameters;
  144. }
  145. /**
  146. * @brief Get the name for the setup check
  147. *
  148. * @since 28.0.0
  149. */
  150. public function getName(): ?string {
  151. return $this->name;
  152. }
  153. /**
  154. * @brief Set the name from the setup check
  155. *
  156. * @since 28.0.0
  157. */
  158. public function setName(string $name): void {
  159. $this->name = $name;
  160. }
  161. /**
  162. * @brief Get a link to the doc for the explanation.
  163. *
  164. * @since 28.0.0
  165. */
  166. public function getLinkToDoc(): ?string {
  167. return $this->linkToDoc;
  168. }
  169. /**
  170. * @brief Get an array representation of the result for API responses
  171. *
  172. * @since 28.0.0
  173. */
  174. public function jsonSerialize(): array {
  175. return [
  176. 'name' => $this->name,
  177. 'severity' => $this->severity,
  178. 'description' => $this->description,
  179. 'descriptionParameters' => $this->descriptionParameters,
  180. 'linkToDoc' => $this->linkToDoc,
  181. ];
  182. }
  183. }