Notifier.php 2.2 KB

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