1
0

Absence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Db;
  25. use DateTime;
  26. use Exception;
  27. use InvalidArgumentException;
  28. use JsonSerializable;
  29. use OC\User\OutOfOfficeData;
  30. use OCP\AppFramework\Db\Entity;
  31. use OCP\IUser;
  32. use OCP\User\IOutOfOfficeData;
  33. /**
  34. * @method string getUserId()
  35. * @method void setUserId(string $userId)
  36. * @method string getFirstDay()
  37. * @method void setFirstDay(string $firstDay)
  38. * @method string getLastDay()
  39. * @method void setLastDay(string $lastDay)
  40. * @method string getStatus()
  41. * @method void setStatus(string $status)
  42. * @method string getMessage()
  43. * @method void setMessage(string $message)
  44. */
  45. class Absence extends Entity implements JsonSerializable {
  46. protected string $userId = '';
  47. /** Inclusive, formatted as YYYY-MM-DD */
  48. protected string $firstDay = '';
  49. /** Inclusive, formatted as YYYY-MM-DD */
  50. protected string $lastDay = '';
  51. protected string $status = '';
  52. protected string $message = '';
  53. public function __construct() {
  54. $this->addType('userId', 'string');
  55. $this->addType('firstDay', 'string');
  56. $this->addType('lastDay', 'string');
  57. $this->addType('status', 'string');
  58. $this->addType('message', 'string');
  59. }
  60. public function toOutOufOfficeData(IUser $user, string $timezone): IOutOfOfficeData {
  61. if ($user->getUID() !== $this->getUserId()) {
  62. throw new InvalidArgumentException("The user doesn't match the user id of this absence! Expected " . $this->getUserId() . ", got " . $user->getUID());
  63. }
  64. if ($this->getId() === null) {
  65. throw new Exception('Creating out-of-office data without ID');
  66. }
  67. $tz = new \DateTimeZone($timezone);
  68. $startDate = new DateTime($this->getFirstDay(), $tz);
  69. $endDate = new DateTime($this->getLastDay(), $tz);
  70. $endDate->setTime(23, 59);
  71. return new OutOfOfficeData(
  72. (string)$this->getId(),
  73. $user,
  74. $startDate->getTimestamp(),
  75. $endDate->getTimestamp(),
  76. $this->getStatus(),
  77. $this->getMessage(),
  78. );
  79. }
  80. public function jsonSerialize(): array {
  81. return [
  82. 'userId' => $this->userId,
  83. 'firstDay' => $this->firstDay,
  84. 'lastDay' => $this->lastDay,
  85. 'status' => $this->status,
  86. 'message' => $this->message,
  87. ];
  88. }
  89. }