Notifier.php 3.0 KB

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