1
0

GuestAvatar.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Michael Weimann <mail@michael-weimann.eu>
  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\Avatar;
  26. use OCP\Files\SimpleFS\InMemoryFile;
  27. use OCP\Files\SimpleFS\ISimpleFile;
  28. use Psr\Log\LoggerInterface;
  29. /**
  30. * This class represents a guest user's avatar.
  31. */
  32. class GuestAvatar extends Avatar {
  33. /**
  34. * GuestAvatar constructor.
  35. *
  36. * @param string $userDisplayName The guest user display name
  37. */
  38. public function __construct(
  39. private string $userDisplayName,
  40. LoggerInterface $logger,
  41. ) {
  42. parent::__construct($logger);
  43. }
  44. /**
  45. * Tests if the user has an avatar.
  46. */
  47. public function exists(): bool {
  48. // Guests always have an avatar.
  49. return true;
  50. }
  51. /**
  52. * Returns the guest user display name.
  53. */
  54. public function getDisplayName(): string {
  55. return $this->userDisplayName;
  56. }
  57. /**
  58. * Setting avatars isn't implemented for guests.
  59. *
  60. * @param \OCP\IImage|resource|string $data
  61. */
  62. public function set($data): void {
  63. // unimplemented for guest user avatars
  64. }
  65. /**
  66. * Removing avatars isn't implemented for guests.
  67. */
  68. public function remove(bool $silent = false): void {
  69. // unimplemented for guest user avatars
  70. }
  71. /**
  72. * Generates an avatar for the guest.
  73. */
  74. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  75. $avatar = $this->generateAvatar($this->userDisplayName, $size, $darkTheme);
  76. return new InMemoryFile('avatar.png', $avatar);
  77. }
  78. /**
  79. * Updates the display name if changed.
  80. *
  81. * @param string $feature The changed feature
  82. * @param mixed $oldValue The previous value
  83. * @param mixed $newValue The new value
  84. */
  85. public function userChanged(string $feature, $oldValue, $newValue): void {
  86. if ($feature === 'displayName') {
  87. $this->userDisplayName = $newValue;
  88. }
  89. }
  90. /**
  91. * Guests don't have custom avatars.
  92. */
  93. public function isCustomAvatar(): bool {
  94. return false;
  95. }
  96. }