Notifier.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Roger Szabo <roger.szabo@web.de>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Roger Szabo <roger.szabo@web.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\User_LDAP\Notification;
  26. use OCP\L10N\IFactory;
  27. use OCP\Notification\INotification;
  28. use OCP\Notification\INotifier;
  29. class Notifier implements INotifier {
  30. /** @var IFactory */
  31. protected $l10nFactory;
  32. /**
  33. * @param IFactory $l10nFactory
  34. */
  35. public function __construct(\OCP\L10N\IFactory $l10nFactory) {
  36. $this->l10nFactory = $l10nFactory;
  37. }
  38. /**
  39. * Identifier of the notifier, only use [a-z0-9_]
  40. *
  41. * @return string
  42. * @since 17.0.0
  43. */
  44. public function getID(): string {
  45. return 'user_ldap';
  46. }
  47. /**
  48. * Human readable name describing the notifier
  49. *
  50. * @return string
  51. * @since 17.0.0
  52. */
  53. public function getName(): string {
  54. return $this->l10nFactory->get('user_ldap')->t('LDAP User backend');
  55. }
  56. /**
  57. * @param INotification $notification
  58. * @param string $languageCode The code of the language that should be used to prepare the notification
  59. * @return INotification
  60. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  61. */
  62. public function prepare(INotification $notification, string $languageCode): INotification {
  63. if ($notification->getApp() !== 'user_ldap') {
  64. // Not my app => throw
  65. throw new \InvalidArgumentException();
  66. }
  67. // Read the language from the notification
  68. $l = $this->l10nFactory->get('user_ldap', $languageCode);
  69. switch ($notification->getSubject()) {
  70. // Deal with known subjects
  71. case 'pwd_exp_warn_days':
  72. $params = $notification->getSubjectParameters();
  73. $days = (int) $params[0];
  74. if ($days === 2) {
  75. $notification->setParsedSubject($l->t('Your password will expire tomorrow.'));
  76. } else if ($days === 1) {
  77. $notification->setParsedSubject($l->t('Your password will expire today.'));
  78. } else {
  79. $notification->setParsedSubject($l->n(
  80. 'Your password will expire within %n day.',
  81. 'Your password will expire within %n days.',
  82. $days
  83. ));
  84. }
  85. return $notification;
  86. default:
  87. // Unknown subject => Unknown notification => throw
  88. throw new \InvalidArgumentException();
  89. }
  90. }
  91. }