SetupResult.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  26. * @brief This class is used for storing the result of a setup check
  27. *
  28. * @since 28.0.0
  29. */
  30. class SetupResult implements \JsonSerializable {
  31. public const SUCCESS = 'success';
  32. public const INFO = 'info';
  33. public const WARNING = 'warning';
  34. public const ERROR = 'error';
  35. /**
  36. * @param string $name Translated name to display to the user
  37. */
  38. private ?string $name = null;
  39. /**
  40. * @brief Private constructor, use success()/info()/warning()/error() instead
  41. * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity
  42. * @since 28.0.0
  43. */
  44. private function __construct(
  45. private string $severity,
  46. private ?string $description = null,
  47. private ?string $linkToDoc = null,
  48. ) {
  49. }
  50. /**
  51. * @brief Create a success result object
  52. * @param ?string $description Translated detailed description to display to the user
  53. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  54. * @since 28.0.0
  55. */
  56. public static function success(?string $description = null, ?string $linkToDoc = null): self {
  57. return new self(self::SUCCESS, $description, $linkToDoc);
  58. }
  59. /**
  60. * @brief Create an info result object
  61. * @param ?string $description Translated detailed description to display to the user
  62. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  63. * @since 28.0.0
  64. */
  65. public static function info(?string $description = null, ?string $linkToDoc = null): self {
  66. return new self(self::INFO, $description, $linkToDoc);
  67. }
  68. /**
  69. * @brief Create a warning result object
  70. * @param ?string $description Translated detailed description to display to the user
  71. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  72. * @since 28.0.0
  73. */
  74. public static function warning(?string $description = null, ?string $linkToDoc = null): self {
  75. return new self(self::WARNING, $description, $linkToDoc);
  76. }
  77. /**
  78. * @brief Create an error result object
  79. * @param ?string $description Translated detailed description to display to the user
  80. * @param ?string $linkToDoc URI of related relevent documentation, be it from Nextcloud or another project
  81. * @since 28.0.0
  82. */
  83. public static function error(?string $description = null, ?string $linkToDoc = null): self {
  84. return new self(self::ERROR, $description, $linkToDoc);
  85. }
  86. /**
  87. * @brief Get the severity for the setup check result
  88. *
  89. * @return self::SUCCESS|self::INFO|self::WARNING|self::ERROR
  90. * @since 28.0.0
  91. */
  92. public function getSeverity(): string {
  93. return $this->severity;
  94. }
  95. /**
  96. * @brief Get the description for the setup check result
  97. *
  98. * @since 28.0.0
  99. */
  100. public function getDescription(): ?string {
  101. return $this->description;
  102. }
  103. /**
  104. * @brief Get the name for the setup check
  105. *
  106. * @since 28.0.0
  107. */
  108. public function getName(): ?string {
  109. return $this->name;
  110. }
  111. /**
  112. * @brief Set the name from the setup check
  113. *
  114. * @since 28.0.0
  115. */
  116. public function setName(string $name): void {
  117. $this->name = $name;
  118. }
  119. /**
  120. * @brief Get a link to the doc for the explanation.
  121. *
  122. * @since 28.0.0
  123. */
  124. public function getLinkToDoc(): ?string {
  125. return $this->linkToDoc;
  126. }
  127. /**
  128. * @brief Get an array representation of the result for API responses
  129. *
  130. * @since 28.0.0
  131. */
  132. public function jsonSerialize(): array {
  133. return [
  134. 'name' => $this->name,
  135. 'severity' => $this->severity,
  136. 'description' => $this->description,
  137. 'linkToDoc' => $this->linkToDoc,
  138. ];
  139. }
  140. }