1
0

Notifier.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Notification;
  7. use OCP\L10N\IFactory;
  8. use OCP\Notification\INotification;
  9. use OCP\Notification\INotifier;
  10. use OCP\Notification\UnknownNotificationException;
  11. class Notifier implements INotifier {
  12. /**
  13. * @param IFactory $l10nFactory
  14. */
  15. public function __construct(
  16. protected IFactory $l10nFactory,
  17. ) {
  18. }
  19. /**
  20. * Identifier of the notifier, only use [a-z0-9_]
  21. *
  22. * @return string
  23. * @since 17.0.0
  24. */
  25. public function getID(): string {
  26. return 'user_ldap';
  27. }
  28. /**
  29. * Human readable name describing the notifier
  30. *
  31. * @return string
  32. * @since 17.0.0
  33. */
  34. public function getName(): string {
  35. return $this->l10nFactory->get('user_ldap')->t('LDAP User backend');
  36. }
  37. /**
  38. * @param INotification $notification
  39. * @param string $languageCode The code of the language that should be used to prepare the notification
  40. * @return INotification
  41. * @throws UnknownNotificationException When the notification was not prepared by a notifier
  42. */
  43. public function prepare(INotification $notification, string $languageCode): INotification {
  44. if ($notification->getApp() !== 'user_ldap') {
  45. // Not my app => throw
  46. throw new UnknownNotificationException();
  47. }
  48. // Read the language from the notification
  49. $l = $this->l10nFactory->get('user_ldap', $languageCode);
  50. switch ($notification->getSubject()) {
  51. // Deal with known subjects
  52. case 'pwd_exp_warn_days':
  53. $params = $notification->getSubjectParameters();
  54. $days = (int)$params[0];
  55. if ($days === 2) {
  56. $notification->setParsedSubject($l->t('Your password will expire tomorrow.'));
  57. } elseif ($days === 1) {
  58. $notification->setParsedSubject($l->t('Your password will expire today.'));
  59. } else {
  60. $notification->setParsedSubject($l->n(
  61. 'Your password will expire within %n day.',
  62. 'Your password will expire within %n days.',
  63. $days
  64. ));
  65. }
  66. return $notification;
  67. default:
  68. // Unknown subject => Unknown notification => throw
  69. throw new UnknownNotificationException();
  70. }
  71. }
  72. }