1
0

GuestAvatar.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Avatar;
  8. use OCP\Files\SimpleFS\InMemoryFile;
  9. use OCP\Files\SimpleFS\ISimpleFile;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * This class represents a guest user's avatar.
  13. */
  14. class GuestAvatar extends Avatar {
  15. /**
  16. * GuestAvatar constructor.
  17. *
  18. * @param string $userDisplayName The guest user display name
  19. */
  20. public function __construct(
  21. private string $userDisplayName,
  22. LoggerInterface $logger,
  23. ) {
  24. parent::__construct($logger);
  25. }
  26. /**
  27. * Tests if the user has an avatar.
  28. */
  29. public function exists(): bool {
  30. // Guests always have an avatar.
  31. return true;
  32. }
  33. /**
  34. * Returns the guest user display name.
  35. */
  36. public function getDisplayName(): string {
  37. return $this->userDisplayName;
  38. }
  39. /**
  40. * Setting avatars isn't implemented for guests.
  41. *
  42. * @param \OCP\IImage|resource|string $data
  43. */
  44. public function set($data): void {
  45. // unimplemented for guest user avatars
  46. }
  47. /**
  48. * Removing avatars isn't implemented for guests.
  49. */
  50. public function remove(bool $silent = false): void {
  51. // unimplemented for guest user avatars
  52. }
  53. /**
  54. * Generates an avatar for the guest.
  55. */
  56. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  57. $avatar = $this->generateAvatar($this->userDisplayName, $size, $darkTheme);
  58. return new InMemoryFile('avatar.png', $avatar);
  59. }
  60. /**
  61. * Updates the display name if changed.
  62. *
  63. * @param string $feature The changed feature
  64. * @param mixed $oldValue The previous value
  65. * @param mixed $newValue The new value
  66. */
  67. public function userChanged(string $feature, $oldValue, $newValue): void {
  68. if ($feature === 'displayName') {
  69. $this->userDisplayName = $newValue;
  70. }
  71. }
  72. /**
  73. * Guests don't have custom avatars.
  74. */
  75. public function isCustomAvatar(): bool {
  76. return false;
  77. }
  78. }