src/App/Entity/NotificationSetting.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\NotificationService;
  4. use App\Utility\GuidUtility;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Exception;
  7. /**
  8. * @ORM\Entity
  9. *
  10. * @ORM\Table(
  11. * name="notification_settings",
  12. * uniqueConstraints={
  13. *
  14. * @ORM\UniqueConstraint(name="notification_type_setting_type_user_idx", columns={"notification_type", "setting_type", "users_id"})
  15. * }
  16. * )
  17. *
  18. * If required, this entity can grow using different value types ("setting_int_value", "setting_string_value" etc.), but
  19. * for now we keep it simple
  20. */
  21. class NotificationSetting
  22. {
  23. public const SETTING_TYPE_CANCELED = 0;
  24. /**
  25. * @var string
  26. *
  27. * @ORM\GeneratedValue(strategy="CUSTOM")
  28. *
  29. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  30. *
  31. * @ORM\Column(name="id", type="guid")
  32. *
  33. * @ORM\Id
  34. */
  35. protected $id;
  36. public function setId(string $id): void
  37. {
  38. GuidUtility::validOrThrow($id);
  39. $this->id = $id;
  40. }
  41. public function getId()
  42. {
  43. return $this->id;
  44. }
  45. /**
  46. * @var User
  47. *
  48. * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="notificationSettings", cascade={"persist"})
  49. *
  50. * @ORM\JoinColumn(name="users_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  51. */
  52. protected $user;
  53. public function setUser(User $user): void
  54. {
  55. $this->user = $user;
  56. }
  57. public function getUser(): User
  58. {
  59. return $this->user;
  60. }
  61. /**
  62. * @var int
  63. *
  64. * @ORM\Column(name="notification_type", type="smallint", nullable=false)
  65. */
  66. protected $notificationType;
  67. /**
  68. * @throws Exception
  69. */
  70. public function setNotificationType(int $notificationType): void
  71. {
  72. if (!in_array($notificationType, NotificationService::NOTIFICATION_TYPES)) {
  73. throw new Exception('Value ' . $notificationType . ' not allowed for notificationType.');
  74. }
  75. $this->notificationType = $notificationType;
  76. }
  77. public function getNotificationType(): int
  78. {
  79. return $this->notificationType;
  80. }
  81. /**
  82. * @var int
  83. *
  84. * @ORM\Column(name="setting_type", type="smallint", nullable=false)
  85. */
  86. protected $settingType;
  87. public function setSettingType(int $settingType): void
  88. {
  89. if ($settingType != self::SETTING_TYPE_CANCELED) {
  90. throw new Exception('Value ' . $settingType . ' not allowed for settingType.');
  91. }
  92. $this->settingType = $settingType;
  93. }
  94. public function getSettingType(): int
  95. {
  96. return $this->settingType;
  97. }
  98. }