SiteStatus.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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["Template"] = $this->getSiteInfos()->getTemplate();
  80. $infos["Preferences"] = $this->getSiteInfos()->getPreferences();
  81. $infos["Matomo id"] = $this->getSiteInfos()->getMatomoId();
  82. $infos["Mounted for: "] = $this->getSiteInfos()->getMountedForBeUsers();
  83. if ($this->useWarnings()) {
  84. $infos["Warnings"] = $this->getWarnings();
  85. }
  86. }
  87. return $infos;
  88. }
  89. /**
  90. * @return int
  91. */
  92. public function getOrganizationId(): int
  93. {
  94. return $this->organizationId;
  95. }
  96. /**
  97. * @return int
  98. */
  99. public function getStatusCode(): int
  100. {
  101. return $this->statusCode;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getStatusLabel(): string
  107. {
  108. return self::STATUSES[$this->statusCode];
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getMessage(): string
  114. {
  115. if($this->getStatusCode() == self::STATUS_NO_SUCH_WEBSITE) {
  116. return 'No website were found for organization ' . $this->getOrganizationId();
  117. }
  118. elseif ($this->getStatusCode() == self::STATUS_EXISTING) {
  119. return 'A website exists for organization ' . $this->getOrganizationId();
  120. }
  121. elseif ($this->getStatusCode() == self::STATUS_EXISTING_DELETED) {
  122. return 'A website exists for organization ' . $this->getOrganizationId() . ', but has been deleted';
  123. }
  124. elseif ($this->getStatusCode() == self::STATUS_EXISTING_HIDDEN) {
  125. return 'A website exists for organization ' . $this->getOrganizationId() . ', but is hidden or has a restricted access';
  126. } elseif ($this->getStatusCode() == self::STATUS_EXISTING_WITH_WARNINGS) {
  127. return 'A website exists for organization ' . $this->getOrganizationId() . ', but warnings were raised';
  128. }
  129. }
  130. /**
  131. * @param int $statusCode
  132. */
  133. public function setStatusCode(int $statusCode): void
  134. {
  135. if (!isset(self::STATUSES[$statusCode])) {
  136. throw new \InvalidArgumentException('Invalid status code : ' . $statusCode);
  137. }
  138. $this->statusCode = $statusCode;
  139. }
  140. /**
  141. * @return SiteInfos
  142. */
  143. public function getSiteInfos(): SiteInfos
  144. {
  145. return $this->siteInfos;
  146. }
  147. /**
  148. * @param SiteInfos $siteInfos
  149. */
  150. public function setSiteInfos(SiteInfos $siteInfos): void
  151. {
  152. $this->siteInfos = $siteInfos;
  153. }
  154. /**
  155. * @return array|null
  156. */
  157. public function getWarnings(): ?array
  158. {
  159. return $this->warnings;
  160. }
  161. /**
  162. * @param array $warnings
  163. */
  164. public function setWarnings(array $warnings): void
  165. {
  166. $this->warnings = $warnings;
  167. }
  168. /**
  169. * Warnings have been scanned (the warnings array still can be empty)
  170. */
  171. public function useWarnings(): bool
  172. {
  173. return $this->warnings !== null;
  174. }
  175. /**
  176. * Warnings have been logged
  177. */
  178. public function hasWarnings(): bool
  179. {
  180. return count($this->warnings) > 0;
  181. }
  182. /**
  183. * @param string $warning
  184. */
  185. public function addWarnings(string $warning): void
  186. {
  187. $this->warnings[] = $warning;
  188. }
  189. }