Notifier.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Authentication\Notifications;
  26. use InvalidArgumentException;
  27. use OCP\L10N\IFactory as IL10nFactory;
  28. use OCP\Notification\INotification;
  29. use OCP\Notification\INotifier;
  30. class Notifier implements INotifier {
  31. /** @var IL10nFactory */
  32. private $factory;
  33. public function __construct(IL10nFactory $l10nFactory) {
  34. $this->factory = $l10nFactory;
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. public function prepare(INotification $notification, string $languageCode): INotification {
  40. if ($notification->getApp() !== 'auth') {
  41. // Not my app => throw
  42. throw new InvalidArgumentException();
  43. }
  44. // Read the language from the notification
  45. $l = $this->factory->get('lib', $languageCode);
  46. switch ($notification->getSubject()) {
  47. case 'remote_wipe_start':
  48. $notification->setParsedSubject(
  49. $l->t('Remote wipe started')
  50. )->setParsedMessage(
  51. $l->t('A remote wipe was started on device %s', $notification->getSubjectParameters())
  52. );
  53. return $notification;
  54. case 'remote_wipe_finish':
  55. $notification->setParsedSubject(
  56. $l->t('Remote wipe finished')
  57. )->setParsedMessage(
  58. $l->t('The remote wipe on %s has finished', $notification->getSubjectParameters())
  59. );
  60. return $notification;
  61. default:
  62. // Unknown subject => Unknown notification => throw
  63. throw new InvalidArgumentException();
  64. }
  65. }
  66. /**
  67. * Identifier of the notifier, only use [a-z0-9_]
  68. *
  69. * @return string
  70. * @since 17.0.0
  71. */
  72. public function getID(): string {
  73. return 'auth';
  74. }
  75. /**
  76. * Human readable name describing the notifier
  77. *
  78. * @return string
  79. * @since 17.0.0
  80. */
  81. public function getName(): string {
  82. return $this->factory->get('lib')->t('Authentication');
  83. }
  84. }