SiteStatus.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Opentalent\OtAdmin\Domain\Entity;
  3. /**
  4. * Status of a website as returned by SiteController->getSiteStatusAction()
  5. */
  6. class SiteStatus
  7. {
  8. // Websites statuses
  9. const STATUS_UNKNOWN = 0;
  10. const STATUS_NO_SUCH_WEBSITE = -1;
  11. const STATUS_EXISTING = 5;
  12. const STATUS_EXISTING_DELETED = 2;
  13. const STATUS_EXISTING_HIDDEN = 3;
  14. const STATUS_EXISTING_WITH_WARNINGS = 4;
  15. const STATUS_EXISTING_WITH_CRITICAL_ERRORS = 1;
  16. const STATUSES = [
  17. self::STATUS_UNKNOWN => 'Unknown',
  18. self::STATUS_NO_SUCH_WEBSITE => 'Does not exist',
  19. self::STATUS_EXISTING => 'Existing',
  20. self::STATUS_EXISTING_DELETED => 'Existing, deleted',
  21. self::STATUS_EXISTING_HIDDEN => 'Existing, hidden',
  22. self::STATUS_EXISTING_WITH_WARNINGS => 'Existing with warnings',
  23. self::STATUS_EXISTING_WITH_CRITICAL_ERRORS => 'Existing with critical error(s)',
  24. ];
  25. /**
  26. * Id of the organization owning the website
  27. * @var int
  28. */
  29. protected int $organizationId;
  30. /**
  31. * Status-code of the website
  32. * @var int
  33. */
  34. protected int $statusCode = self::STATUS_UNKNOWN;
  35. /**
  36. * SiteInfos of the website
  37. *
  38. * @var SiteInfos | null
  39. */
  40. protected ?SiteInfos $siteInfos = null;
  41. /**
  42. * Optional list of warnings
  43. *
  44. * @var array|null
  45. */
  46. protected ?array $warnings = null;
  47. public function __construct(
  48. int $organizationId,
  49. int $statusCode = null,
  50. ?SiteInfos $siteInfos = null,
  51. ?array $warnings = null
  52. )
  53. {
  54. $this->organizationId = $organizationId;
  55. if ($statusCode !== null) {
  56. $this->setStatusCode($statusCode);
  57. }
  58. if ($siteInfos !== null) {
  59. $this->setSiteInfos($siteInfos);
  60. }
  61. if ($warnings !== null) {
  62. $this->setWarnings($warnings);
  63. }
  64. }
  65. /**
  66. * Return the site status as a serializable array
  67. *
  68. * @return array
  69. */
  70. public function toArray(): array
  71. {
  72. $infos = [
  73. "Organization Id" => $this->getOrganizationId(),
  74. "Status" => $this->getStatusLabel(),
  75. "Message" => $this->getMessage()
  76. ];
  77. if ($this->getStatusCode() > 0 && $this->getSiteInfos()) {
  78. $infos["Root Uid"] = $this->getSiteInfos()->getRootUid();
  79. $infos["Site's title"] = $this->getSiteInfos()->getSiteTitle();
  80. $infos["Base Url"] = $this->getSiteInfos()->getBaseUrl();
  81. $infos["Deleted"] = (int)$this->getSiteInfos()->isDeleted();
  82. $infos["Hidden or restricted"] = (int)$this->getSiteInfos()->isHiddenOrRestricted();
  83. $infos["Premium"] = (int)$this->getSiteInfos()->isPremium();
  84. $infos["Template"] = $this->getSiteInfos()->getTemplate();
  85. $infos["Preferences"] = $this->getSiteInfos()->getPreferences();
  86. $infos["Matomo id"] = $this->getSiteInfos()->getMatomoId() ?? "-";
  87. $infos["Mounted for user(s): "] = $this->getSiteInfos()->getMountedForBeUsers();
  88. $infos["Mounted for group(s): "] = $this->getSiteInfos()->getMountedForBeGroups();
  89. if ($this->useWarnings()) {
  90. $infos["Warnings"] = $this->getWarnings();
  91. }
  92. }
  93. return $infos;
  94. }
  95. /**
  96. * @return int
  97. */
  98. public function getOrganizationId(): int
  99. {
  100. return $this->organizationId;
  101. }
  102. /**
  103. * @return int
  104. */
  105. public function getStatusCode(): int
  106. {
  107. return $this->statusCode;
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getStatusLabel(): string
  113. {
  114. return self::STATUSES[$this->statusCode];
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getMessage(): string
  120. {
  121. if($this->getStatusCode() == self::STATUS_NO_SUCH_WEBSITE) {
  122. return 'No website were found for organization ' . $this->getOrganizationId();
  123. }
  124. elseif ($this->getStatusCode() == self::STATUS_EXISTING) {
  125. return 'A website exists for organization ' . $this->getOrganizationId();
  126. }
  127. elseif ($this->getStatusCode() == self::STATUS_EXISTING_DELETED) {
  128. return 'A website exists for organization ' . $this->getOrganizationId() . ', but has been deleted';
  129. }
  130. elseif ($this->getStatusCode() == self::STATUS_EXISTING_HIDDEN) {
  131. return 'A website exists for organization ' . $this->getOrganizationId() . ', but is hidden or has a restricted access';
  132. } elseif ($this->getStatusCode() == self::STATUS_EXISTING_WITH_WARNINGS) {
  133. return 'A website exists for organization ' . $this->getOrganizationId() . ', but warnings were raised';
  134. }
  135. return 'Unknown status';
  136. }
  137. /**
  138. * @param int $statusCode
  139. */
  140. public function setStatusCode(int $statusCode): void
  141. {
  142. if (!isset(self::STATUSES[$statusCode])) {
  143. throw new \InvalidArgumentException('Invalid status code : ' . $statusCode);
  144. }
  145. $this->statusCode = $statusCode;
  146. }
  147. /**
  148. * @return SiteInfos|null
  149. */
  150. public function getSiteInfos(): SiteInfos | null
  151. {
  152. return $this->siteInfos;
  153. }
  154. /**
  155. * @param SiteInfos $siteInfos
  156. */
  157. public function setSiteInfos(SiteInfos $siteInfos): void
  158. {
  159. $this->siteInfos = $siteInfos;
  160. }
  161. /**
  162. * @return array|null
  163. */
  164. public function getWarnings(): ?array
  165. {
  166. return $this->warnings;
  167. }
  168. /**
  169. * @param array $warnings
  170. */
  171. public function setWarnings(array $warnings): void
  172. {
  173. $this->warnings = $warnings;
  174. }
  175. /**
  176. * Warnings have been scanned (the warnings array still can be empty)
  177. */
  178. public function useWarnings(): bool
  179. {
  180. return $this->warnings !== null;
  181. }
  182. /**
  183. * Warnings have been logged
  184. */
  185. public function hasWarnings(): bool
  186. {
  187. return count($this->warnings) > 0;
  188. }
  189. /**
  190. * @param string $warning
  191. */
  192. public function addWarnings(string $warning): void
  193. {
  194. $this->warnings[] = $warning;
  195. }
  196. }