SetupResult.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * @brief Private constructor, use success()/info()/warning()/error() instead
  37. * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity
  38. * @since 28.0.0
  39. */
  40. private function __construct(
  41. private string $severity,
  42. private ?string $description = null,
  43. private ?string $linkToDoc = null,
  44. ) {
  45. }
  46. /**
  47. * @brief Create a success result object
  48. * @since 28.0.0
  49. */
  50. public static function success(?string $description = null, ?string $linkToDoc = null): self {
  51. return new self(self::SUCCESS, $description, $linkToDoc);
  52. }
  53. /**
  54. * @brief Create an info result object
  55. * @since 28.0.0
  56. */
  57. public static function info(?string $description = null, ?string $linkToDoc = null): self {
  58. return new self(self::INFO, $description, $linkToDoc);
  59. }
  60. /**
  61. * @brief Create a warning result object
  62. * @since 28.0.0
  63. */
  64. public static function warning(?string $description = null, ?string $linkToDoc = null): self {
  65. return new self(self::WARNING, $description, $linkToDoc);
  66. }
  67. /**
  68. * @brief Create an error result object
  69. * @since 28.0.0
  70. */
  71. public static function error(?string $description = null, ?string $linkToDoc = null): self {
  72. return new self(self::ERROR, $description, $linkToDoc);
  73. }
  74. /**
  75. * @brief Get the severity for the setup check result
  76. *
  77. * @return self::SUCCESS|self::INFO|self::WARNING|self::ERROR
  78. * @since 28.0.0
  79. */
  80. public function getSeverity(): string {
  81. return $this->severity;
  82. }
  83. /**
  84. * @brief Get the description for the setup check result
  85. *
  86. * @since 28.0.0
  87. */
  88. public function getDescription(): ?string {
  89. return $this->description;
  90. }
  91. /**
  92. * @brief Get a link to the doc for the explanation.
  93. *
  94. * @since 28.0.0
  95. */
  96. public function getLinkToDoc(): ?string {
  97. return $this->linkToDoc;
  98. }
  99. /**
  100. * @brief Get an array representation of the result for API responses
  101. *
  102. * @since 28.0.0
  103. */
  104. public function jsonSerialize(): array {
  105. return [
  106. 'severity' => $this->severity,
  107. 'description' => $this->description,
  108. 'linkToDoc' => $this->linkToDoc,
  109. ];
  110. }
  111. }