SiteStatus.php 5.8 KB

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