SystemStatus.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV\Xml\Notification;
  4. use Sabre\CalDAV\Plugin;
  5. use Sabre\Xml\Writer;
  6. /**
  7. * SystemStatus notification.
  8. *
  9. * This notification can be used to indicate to the user that the system is
  10. * down.
  11. *
  12. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  13. * @author Evert Pot (http://evertpot.com/)
  14. * @license http://sabre.io/license/ Modified BSD License
  15. */
  16. class SystemStatus implements NotificationInterface
  17. {
  18. const TYPE_LOW = 1;
  19. const TYPE_MEDIUM = 2;
  20. const TYPE_HIGH = 3;
  21. /**
  22. * A unique id.
  23. *
  24. * @var string
  25. */
  26. protected $id;
  27. /**
  28. * The type of alert. This should be one of the TYPE_ constants.
  29. *
  30. * @var int
  31. */
  32. protected $type;
  33. /**
  34. * A human-readable description of the problem.
  35. *
  36. * @var string
  37. */
  38. protected $description;
  39. /**
  40. * A url to a website with more information for the user.
  41. *
  42. * @var string
  43. */
  44. protected $href;
  45. /**
  46. * Notification Etag.
  47. *
  48. * @var string
  49. */
  50. protected $etag;
  51. /**
  52. * Creates the notification.
  53. *
  54. * Some kind of unique id should be provided. This is used to generate a
  55. * url.
  56. *
  57. * @param string $id
  58. * @param string $etag
  59. * @param int $type
  60. * @param string $description
  61. * @param string $href
  62. */
  63. public function __construct($id, $etag, $type = self::TYPE_HIGH, $description = null, $href = null)
  64. {
  65. $this->id = $id;
  66. $this->type = $type;
  67. $this->description = $description;
  68. $this->href = $href;
  69. $this->etag = $etag;
  70. }
  71. /**
  72. * The serialize method is called during xml writing.
  73. *
  74. * It should use the $writer argument to encode this object into XML.
  75. *
  76. * Important note: it is not needed to create the parent element. The
  77. * parent element is already created, and we only have to worry about
  78. * attributes, child elements and text (if any).
  79. *
  80. * Important note 2: If you are writing any new elements, you are also
  81. * responsible for closing them.
  82. */
  83. public function xmlSerialize(Writer $writer)
  84. {
  85. switch ($this->type) {
  86. case self::TYPE_LOW:
  87. $type = 'low';
  88. break;
  89. case self::TYPE_MEDIUM:
  90. $type = 'medium';
  91. break;
  92. default:
  93. case self::TYPE_HIGH:
  94. $type = 'high';
  95. break;
  96. }
  97. $writer->startElement('{'.Plugin::NS_CALENDARSERVER.'}systemstatus');
  98. $writer->writeAttribute('type', $type);
  99. $writer->endElement();
  100. }
  101. /**
  102. * This method serializes the entire notification, as it is used in the
  103. * response body.
  104. */
  105. public function xmlSerializeFull(Writer $writer)
  106. {
  107. $cs = '{'.Plugin::NS_CALENDARSERVER.'}';
  108. switch ($this->type) {
  109. case self::TYPE_LOW:
  110. $type = 'low';
  111. break;
  112. case self::TYPE_MEDIUM:
  113. $type = 'medium';
  114. break;
  115. default:
  116. case self::TYPE_HIGH:
  117. $type = 'high';
  118. break;
  119. }
  120. $writer->startElement($cs.'systemstatus');
  121. $writer->writeAttribute('type', $type);
  122. if ($this->description) {
  123. $writer->writeElement($cs.'description', $this->description);
  124. }
  125. if ($this->href) {
  126. $writer->writeElement('{DAV:}href', $this->href);
  127. }
  128. $writer->endElement(); // systemstatus
  129. }
  130. /**
  131. * Returns a unique id for this notification.
  132. *
  133. * This is just the base url. This should generally be some kind of unique
  134. * id.
  135. *
  136. * @return string
  137. */
  138. public function getId()
  139. {
  140. return $this->id;
  141. }
  142. /*
  143. * Returns the ETag for this notification.
  144. *
  145. * The ETag must be surrounded by literal double-quotes.
  146. *
  147. * @return string
  148. */
  149. public function getETag()
  150. {
  151. return $this->etag;
  152. }
  153. }