1
0

Absence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Db;
  8. use DateTime;
  9. use Exception;
  10. use InvalidArgumentException;
  11. use JsonSerializable;
  12. use OC\User\OutOfOfficeData;
  13. use OCP\AppFramework\Db\Entity;
  14. use OCP\IUser;
  15. use OCP\User\IOutOfOfficeData;
  16. /**
  17. * @method string getUserId()
  18. * @method void setUserId(string $userId)
  19. * @method string getFirstDay()
  20. * @method void setFirstDay(string $firstDay)
  21. * @method string getLastDay()
  22. * @method void setLastDay(string $lastDay)
  23. * @method string getStatus()
  24. * @method void setStatus(string $status)
  25. * @method string getMessage()
  26. * @method void setMessage(string $message)
  27. * @method string getReplacementUserId()
  28. * @method void setReplacementUserId(?string $replacementUserId)
  29. * @method string getReplacementUserDisplayName()
  30. * @method void setReplacementUserDisplayName(?string $replacementUserDisplayName)
  31. */
  32. class Absence extends Entity implements JsonSerializable {
  33. protected string $userId = '';
  34. /** Inclusive, formatted as YYYY-MM-DD */
  35. protected string $firstDay = '';
  36. /** Inclusive, formatted as YYYY-MM-DD */
  37. protected string $lastDay = '';
  38. protected string $status = '';
  39. protected string $message = '';
  40. protected ?string $replacementUserId = null;
  41. protected ?string $replacementUserDisplayName = null;
  42. public function __construct() {
  43. $this->addType('userId', 'string');
  44. $this->addType('firstDay', 'string');
  45. $this->addType('lastDay', 'string');
  46. $this->addType('status', 'string');
  47. $this->addType('message', 'string');
  48. $this->addType('replacementUserId', 'string');
  49. $this->addType('replacementUserDisplayName', 'string');
  50. }
  51. public function toOutOufOfficeData(IUser $user, string $timezone): IOutOfOfficeData {
  52. if ($user->getUID() !== $this->getUserId()) {
  53. throw new InvalidArgumentException("The user doesn't match the user id of this absence! Expected " . $this->getUserId() . ', got ' . $user->getUID());
  54. }
  55. if ($this->getId() === null) {
  56. throw new Exception('Creating out-of-office data without ID');
  57. }
  58. $tz = new \DateTimeZone($timezone);
  59. $startDate = new DateTime($this->getFirstDay(), $tz);
  60. $endDate = new DateTime($this->getLastDay(), $tz);
  61. $endDate->setTime(23, 59);
  62. return new OutOfOfficeData(
  63. (string)$this->getId(),
  64. $user,
  65. $startDate->getTimestamp(),
  66. $endDate->getTimestamp(),
  67. $this->getStatus(),
  68. $this->getMessage(),
  69. $this->getReplacementUserId(),
  70. $this->getReplacementUserDisplayName(),
  71. );
  72. }
  73. public function jsonSerialize(): array {
  74. return [
  75. 'userId' => $this->userId,
  76. 'firstDay' => $this->firstDay,
  77. 'lastDay' => $this->lastDay,
  78. 'status' => $this->status,
  79. 'message' => $this->message,
  80. 'replacementUserId' => $this->replacementUserId,
  81. 'replacementUserDisplayName' => $this->replacementUserDisplayName,
  82. ];
  83. }
  84. }